Spaces:
Sleeping
Sleeping
File size: 6,348 Bytes
0f52b20 7154ae4 0f52b20 c1b226b 6657e77 c1b226b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | ---
title: Autonomous Data Analyst
emoji: π»
colorFrom: purple
colorTo: gray
sdk: gradio
sdk_version: 6.5.1
app_file: app.py
pinned: false
---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
# Multi-Source Agentic RAG
A local AI agent with an intelligent **Router** that directs traffic between:
- **Neon PostgreSQL** β structured/numerical data (sales, counts, aggregations)
- **ChromaDB** β policies, documentation (company_policies collection)
Both paths converge at a **synthesis node** that produces natural language answers.
## Architecture
- **Router**: LLM analyzes intent β routes to `sql` or `vector_store`
- **SQL path**: explore β generate β HITL approval β execute β self-correct β visualize
- **Vector path**: retrieve from ChromaDB `company_policies`
- **Synthesis**: `generate_answer` produces a clear natural language response
```
βββββββββββββββ
β Router β
ββββββββ¬βββββββ
β
βββββββββββββββββΌββββββββββββββββ
βΌ β βΌ
βββββββββββββββ β βββββββββββββββ
β SQL path β β β Vector path β
β (Neon) β β β (ChromaDB) β
ββββββββ¬βββββββ β ββββββββ¬βββββββ
β β β
βββββββββββββββββΌββββββββββββββββ
βΌ
βββββββββββββββ
β generate_ β
β answer β
βββββββββββββββ
```
## Free hosting (Hugging Face Spaces)
You can host the Gradio app for free on [Hugging Face Spaces](https://huggingface.co/spaces). The app will use **Groq** (free LLM API) and **HuggingFace embeddings** in the cloudβno Ollama required.
See **[DEPLOY.md](DEPLOY.md)** for step-by-step instructions (Groq API key, Space secrets, and Neon connection string).
## Setup
### 1. Create a Neon database
1. Sign up at [neon.tech](https://neon.tech) and create a project.
2. Copy the connection string from the dashboard (Connection string β URI).
### 2. Configure connection
```bash
export NEON_DATABASE_URL='postgresql://user:password@ep-xxx.region.aws.neon.tech/neondb?sslmode=require'
```
Or copy `.env.example` to `.env` and set `NEON_DATABASE_URL` there.
### 3. Install Python dependencies
```bash
cd "AGENTIC _AI"
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
```
### 4. Seed the database (once)
```bash
python3 seed_neon.py
```
This creates **7 tables** with different kinds of data:
| Table | Kind | Description |
|-------|------|-------------|
| **regions** | Lookup | Geographic (name, country) |
| **product_categories** | Lookup | Category names |
| **customers** | Entity | Name, email, region_id, created_at |
| **products** | Catalog | Name, category_id, base_price |
| **sales** | Transactions | customer_id, product_id, quantity, amount, sale_date |
| **returns** | Events | sale_id, amount, reason, return_date |
| **promotions** | Time-bound | code, discount_pct, start_date, end_date |
Then load ~100k rows of synthetic data (with edge cases):
```bash
python3 generate_synthetic_sales.py
```
### 5. Seed the vector store (once)
```bash
ollama pull nomic-embed-text # embedding model
python3 seed_vector_store.py
```
### 6. Install Ollama and pull models
```bash
ollama pull deepseek-coder-v2 # for routing + SQL + synthesis
ollama pull nomic-embed-text # for ChromaDB embeddings
# Run ollama serve if not using the app
```
## Usage
### CLI
```bash
# SQL path (structured data)
python3 main.py "What are total sales by category?"
python3 main.py "Show sales by region"
# Vector path (policies/docs)
python3 main.py "What is the expense policy?"
python3 main.py "What is the remote work policy?"
```
### Streamlit UI
```bash
streamlit run app.py
```
- Chat interface with `st.chat_message` and `st.chat_input`
- **Agent Reasoning** expander shows datasource, SQL, errors
- `st.dataframe()` for SQL results, `st.image()` for charts
- **HITL**: high-impact queries pause with Approve / Reject buttons
- Sidebar: Neon connection string, Clear Chat
### Gradio UI
```bash
python3 app_gradio.py
```
- Opens at **http://localhost:7860**
- Chat interface with config (Neon URL, Ollama model) in an accordion
- Sample-question buttons; results table and chart below chat when applicable
- **HITL**: Approve / Reject buttons appear when a high-impact SQL query is proposed
The agent will:
1. Explore the PostgreSQL schema
2. Generate a SQL query
3. **Human-in-the-Loop (HITL):** If the query is high-impact (UPDATE, DELETE, INSERT, DROP, etc.), the program pauses and prompts you to approve or reject before execution
4. Execute it (and retry with fixes if it fails)
5. Infer the best chart type and save to `data/outputs/analysis_chart.png`
## Project Structure
```
AGENTIC _AI/
βββ main.py # Entry point (uses NEON_DATABASE_URL)
βββ seed_neon.py # One-time: create tables and seed data on Neon
βββ .env.example # Example env vars (NEON_DATABASE_URL)
βββ requirements.txt
βββ data/
β βββ outputs/ # Saved charts
βββ src/
βββ agent.py # LangGraph agent + self-correction loop
βββ models.py # Pydantic schemas
βββ state.py # Agent state
βββ tools.py # Schema explorer, SQL executor
βββ visualizer.py # Pandas/Matplotlib/Seaborn pipeline
```
## Concepts Covered
- **Local LLM orchestration** with Ollama
- **Stateful agent loops** with LangGraph
- **Self-correction** when SQL fails
- **Structured outputs** with Pydantic
- **Function-style tools** for schema and execution
- **Data lifecycle** from raw SQL to visualization
|