Spaces:
Sleeping
Sleeping
sekpona kokou commited on
Commit ·
e2fccf2
1
Parent(s): cf3b8a9
Rewrite README in English
Browse files
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: Data Analyst
|
| 3 |
emoji: 🚀
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: purple
|
|
@@ -7,58 +7,58 @@ sdk: gradio
|
|
| 7 |
sdk_version: 6.12.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
short_description:
|
| 11 |
---
|
| 12 |
|
| 13 |
# Data Analyst agent with MCP
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
##
|
| 18 |
|
| 19 |
-
-
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
|
| 24 |
-
##
|
| 25 |
|
| 26 |
- Python 3.11+
|
| 27 |
- [Gradio 6.12.0](https://www.gradio.app/)
|
| 28 |
-
- `smolagents` +
|
| 29 |
-
- Azure OpenAI (
|
| 30 |
-
- `matplotlib` + `seaborn`
|
| 31 |
|
| 32 |
-
##
|
| 33 |
|
| 34 |
```text
|
| 35 |
.
|
| 36 |
-
├── app.py #
|
| 37 |
-
├── tools.py #
|
| 38 |
-
├── prompt.py #
|
| 39 |
-
├── charts/ #
|
| 40 |
├── requirements.txt
|
| 41 |
└── README.md
|
| 42 |
```
|
| 43 |
|
| 44 |
-
##
|
| 45 |
|
| 46 |
-
-
|
| 47 |
-
-
|
| 48 |
-
-
|
| 49 |
|
| 50 |
-
##
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
- `MCP_SERVER_URL` (
|
| 55 |
- `AZURE_OPENAI_ENDPOINT`
|
| 56 |
- `AZURE_OPENAI_API_KEY`
|
| 57 |
-
- `AZURE_OPENAI_API_VERSION` (
|
| 58 |
-
- `AZURE_OPENAI_MODEL` (
|
| 59 |
-
- `PORT` (
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
```env
|
| 64 |
MCP_SERVER_URL=https://your-mcp-server/gradio_api/mcp/sse
|
|
@@ -68,7 +68,7 @@ AZURE_OPENAI_API_VERSION=2024-12-01-preview
|
|
| 68 |
AZURE_OPENAI_MODEL=gpt-4o
|
| 69 |
```
|
| 70 |
|
| 71 |
-
##
|
| 72 |
|
| 73 |
```bash
|
| 74 |
python3 -m venv venv
|
|
@@ -77,83 +77,83 @@ pip install -r requirements.txt
|
|
| 77 |
python app.py
|
| 78 |
```
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
-
##
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
1.
|
| 89 |
-
2. Commit
|
| 90 |
-
3. Push
|
| 91 |
|
| 92 |
```bash
|
| 93 |
git push origin main
|
| 94 |
```
|
| 95 |
|
| 96 |
-
|
| 97 |
|
| 98 |
-
##
|
| 99 |
|
| 100 |
-
1.
|
| 101 |
-
2. `ToolCallingAgent`
|
| 102 |
-
3.
|
| 103 |
-
4.
|
| 104 |
|
| 105 |
-
## Prompting /
|
| 106 |
|
| 107 |
-
|
| 108 |
|
| 109 |
-
-
|
| 110 |
|
| 111 |
-
|
| 112 |
|
| 113 |
-
##
|
| 114 |
|
| 115 |
-
- `
|
| 116 |
- `Sub-options per question`
|
| 117 |
- `Distribution by question type`
|
| 118 |
- `Top 5 questions by number of sub options`
|
| 119 |
-
- `
|
| 120 |
|
| 121 |
-
##
|
| 122 |
|
| 123 |
### 1) `SSE Stream ended` / `httpx.ConnectError`
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
|
| 129 |
-
- `MCP_SERVER_URL` correct
|
| 130 |
-
- endpoint
|
| 131 |
-
-
|
| 132 |
|
| 133 |
-
### 2)
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
-
-
|
| 138 |
-
-
|
| 139 |
|
| 140 |
-
|
| 141 |
|
| 142 |
-
### 3)
|
| 143 |
|
| 144 |
-
|
| 145 |
|
| 146 |
-
### 4)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
##
|
| 152 |
|
| 153 |
-
-
|
| 154 |
-
- `generate_chart`
|
| 155 |
-
-
|
| 156 |
|
| 157 |
-
##
|
| 158 |
|
| 159 |
-
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Data Analyst MCP
|
| 3 |
emoji: 🚀
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: purple
|
|
|
|
| 7 |
sdk_version: 6.12.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
short_description: Analyze survey data and generate stats and charts from SQL
|
| 11 |
---
|
| 12 |
|
| 13 |
# Data Analyst agent with MCP
|
| 14 |
|
| 15 |
+
A data analysis assistant focused on the `surveyData` table, with automatic chart generation.
|
| 16 |
|
| 17 |
+
## What this app does
|
| 18 |
|
| 19 |
+
- Answers natural-language questions about survey data.
|
| 20 |
+
- Runs data queries through MCP tools.
|
| 21 |
+
- Generates charts (`bar`, `pie`, `line`) when useful.
|
| 22 |
+
- Displays results in a Gradio UI that works well on Hugging Face Spaces.
|
| 23 |
|
| 24 |
+
## Tech stack
|
| 25 |
|
| 26 |
- Python 3.11+
|
| 27 |
- [Gradio 6.12.0](https://www.gradio.app/)
|
| 28 |
+
- `smolagents` + MCP client (SSE transport)
|
| 29 |
+
- Azure OpenAI (configurable model)
|
| 30 |
+
- `matplotlib` + `seaborn` for charts
|
| 31 |
|
| 32 |
+
## Project structure
|
| 33 |
|
| 34 |
```text
|
| 35 |
.
|
| 36 |
+
├── app.py # Gradio UI + agent orchestration
|
| 37 |
+
├── tools.py # Local tool: generate_chart(...)
|
| 38 |
+
├── prompt.py # Agent system instructions
|
| 39 |
+
├── charts/ # Generated PNG files
|
| 40 |
├── requirements.txt
|
| 41 |
└── README.md
|
| 42 |
```
|
| 43 |
|
| 44 |
+
## Prerequisites
|
| 45 |
|
| 46 |
+
- An MCP endpoint reachable over SSE.
|
| 47 |
+
- An Azure OpenAI deployment.
|
| 48 |
+
- A `.env` file (currently loaded from the parent directory of this project).
|
| 49 |
|
| 50 |
+
## Environment variables
|
| 51 |
|
| 52 |
+
Used by `app.py`:
|
| 53 |
|
| 54 |
+
- `MCP_SERVER_URL` (example: `https://.../gradio_api/mcp/sse`)
|
| 55 |
- `AZURE_OPENAI_ENDPOINT`
|
| 56 |
- `AZURE_OPENAI_API_KEY`
|
| 57 |
+
- `AZURE_OPENAI_API_VERSION` (default: `2024-12-01-preview`)
|
| 58 |
+
- `AZURE_OPENAI_MODEL` (default: `gpt-4o`)
|
| 59 |
+
- `PORT` (default: `7860`)
|
| 60 |
|
| 61 |
+
Minimal `.env` example:
|
| 62 |
|
| 63 |
```env
|
| 64 |
MCP_SERVER_URL=https://your-mcp-server/gradio_api/mcp/sse
|
|
|
|
| 68 |
AZURE_OPENAI_MODEL=gpt-4o
|
| 69 |
```
|
| 70 |
|
| 71 |
+
## Run locally
|
| 72 |
|
| 73 |
```bash
|
| 74 |
python3 -m venv venv
|
|
|
|
| 77 |
python app.py
|
| 78 |
```
|
| 79 |
|
| 80 |
+
Then open `http://localhost:7860`.
|
| 81 |
|
| 82 |
+
## Deploy to Hugging Face Spaces
|
| 83 |
|
| 84 |
+
This repository is already configured as a Gradio Space through the front-matter at the top of this README.
|
| 85 |
|
| 86 |
+
Simple workflow:
|
| 87 |
|
| 88 |
+
1. Make your code changes.
|
| 89 |
+
2. Commit on `main`.
|
| 90 |
+
3. Push to the Space remote:
|
| 91 |
|
| 92 |
```bash
|
| 93 |
git push origin main
|
| 94 |
```
|
| 95 |
|
| 96 |
+
The Space rebuilds automatically.
|
| 97 |
|
| 98 |
+
## Runtime flow (high level)
|
| 99 |
|
| 100 |
+
1. User asks a question.
|
| 101 |
+
2. `ToolCallingAgent` calls MCP tools (and `generate_chart` when relevant).
|
| 102 |
+
3. If a chart is generated, it is encoded in base64 and rendered inline.
|
| 103 |
+
4. The assistant answer is shown in chat.
|
| 104 |
|
| 105 |
+
## Prompting / important data rule
|
| 106 |
|
| 107 |
+
In `prompt.py`, one strict rule is enforced:
|
| 108 |
|
| 109 |
+
- the SQL table is always **`"surveyData"`** (capital `D`)
|
| 110 |
|
| 111 |
+
If your SQL backend changes, update this part first.
|
| 112 |
|
| 113 |
+
## Useful example questions
|
| 114 |
|
| 115 |
+
- `How many questions do we have?`
|
| 116 |
- `Sub-options per question`
|
| 117 |
- `Distribution by question type`
|
| 118 |
- `Top 5 questions by number of sub options`
|
| 119 |
+
- `Percentage distribution of questions`
|
| 120 |
|
| 121 |
+
## Troubleshooting
|
| 122 |
|
| 123 |
### 1) `SSE Stream ended` / `httpx.ConnectError`
|
| 124 |
|
| 125 |
+
Common cause: MCP endpoint is unreachable (wrong URL, sleeping service, network issue).
|
| 126 |
|
| 127 |
+
Check:
|
| 128 |
|
| 129 |
+
- `MCP_SERVER_URL` is correct
|
| 130 |
+
- endpoint is reachable from the deployment environment
|
| 131 |
+
- MCP server is actually running
|
| 132 |
|
| 133 |
+
### 2) Gradio startup errors
|
| 134 |
|
| 135 |
+
Examples already encountered:
|
| 136 |
|
| 137 |
+
- unsupported `type` argument on `gr.Chatbot`
|
| 138 |
+
- unsupported `bubble_full_width` argument
|
| 139 |
|
| 140 |
+
If this happens again, confirm the Gradio version in `requirements.txt` and align component parameters accordingly.
|
| 141 |
|
| 142 |
+
### 3) UI page growing infinitely on Spaces
|
| 143 |
|
| 144 |
+
The app now uses a native Gradio layout + minimal CSS to avoid iframe-related layout issues.
|
| 145 |
|
| 146 |
+
### 4) Missing Azure key
|
| 147 |
|
| 148 |
+
Without `AZURE_OPENAI_API_KEY`, the assistant cannot answer.
|
| 149 |
+
Set it in Space Settings > Secrets.
|
| 150 |
|
| 151 |
+
## Maintenance notes
|
| 152 |
|
| 153 |
+
- Generated charts are stored in `charts/`.
|
| 154 |
+
- `generate_chart` saves PNG files with a title-derived filename.
|
| 155 |
+
- If needed, clean old files in `charts/` periodically.
|
| 156 |
|
| 157 |
+
## Helpful reference
|
| 158 |
|
| 159 |
+
- Spaces config docs: https://huggingface.co/docs/hub/spaces-config-reference
|