assathe/data-analyst-agent2
0
1---2title: "Data Analyst Agent"3thumbnail: "๐ง "4emoji: "๐"5colorFrom: "indigo"6colorTo: "blue"7sdk: "docker"8app_file: "main.py"9license: "mit"10tags:11 - fastapi12 - data-analysis13 - python14 - agent15 - docker16 - huggingface17 - api18model: ""19space_holder: true20---21 22 23 24# ๐ง Data Analyst Agent - FastAPI25 26This is a **FastAPI-based Data Analyst Agent** that can perform **data analysis, web scraping, and visualization** through API calls. 27It integrates with **Anthropic Claude** for automated Python code generation and executes the code securely.28 29The app is designed for deployment on **[Hugging Face Spaces](https://huggingface.co/spaces)** using **Docker**.30 31---32 33## ๐ Features34- **Web scraping** with `requests` + `BeautifulSoup`35- **Data analysis** with `pandas` & `duckdb`36- **Visualizations** with `matplotlib`, `seaborn`, and `plotly`37- **File uploads** for CSV, JSON, TXT datasets38- **Automated Python code generation via Claude**39- **CORS enabled** for web integration40- API response formats support JSON, images (Base64), or text41 42---43 44## ๐ Project Structure45โโโ main.py # FastAPI app entry point46โโโ claude_client.py # Claude API integration47โโโ code_executor.py # User Python code execution environment48โโโ requirements.txt # Python dependencies49โโโ Dockerfile # Hugging Face Docker deployment file50โโโ .env # Local environment secrets (ignored in git)51โโโ README.md # Documentation (this file)52 53---54 55## โก Local Development56 57### 1๏ธโฃ Install dependencies58pip install -r requirements.txt59 60### 2๏ธโฃ Run the app61uvicorn main:app --host 0.0.0.0 --port 8000 --reload62 63Then visit:64- **Swagger Docs**: [http://localhost:8000/docs](http://localhost:8000/docs)65- **Health Check**: [http://localhost:8000/health](http://localhost:8000/health)66 67---68 69## ๐ Deploying to Hugging Face Spaces (Docker)70 71Hugging Face Spaces requires apps to listen on **port 7860** for Docker-based deployments.72 73### 1๏ธโฃ `requirements.txt`74 75---76 77### 2๏ธโฃ `Dockerfile`78 79**Important:** Port **7860** is required on Hugging Face Docker Spaces.80 81---82 83## ๐ก API Endpoints84 85https://assathe-data-analyst-agent2.hf.space/86to check the root status.87 88https://assathe-data-analyst-agent2.hf.space/health89for the health endpoint.90 91https://assathe-data-analyst-agent2.hf.space/docs92for the Swagger docs and API playground 93 94 95### `POST /api/`96curl -X POST "https://assathe-data-analyst-agent2.hf.space/api/" \97 -F "questions=@questions.txt" \98 -F "files=@edges.csv" # (optional)99 100Send a question and optional files for analysis.101- **Form fields**:102 - `questions` โ text file containing the question103 - `files` โ one or more data files (CSV, JSON, TXT)104- **Returns**: JSON with results or Base64-encoded image105 106Example:107curl -X POST "https://assathe-data-analyst-agent2.hf.space/api/" \108-F "questions=@question.txt"109-F "files=@data.csv # (optional)110 111---112 113### `GET /health`114[Detailed health probe.](https://assathe-data-analyst-agent2.hf.space/health115for the health endpoint.)116 117---118 119## ๐ Environment Variables120In your `.env` or Hugging Face Secrets panel:121- `CLAUDE_API_KEY` โ Your Anthropic Claude API key122- Any other secrets needed for custom integrations123 124---125 126## ๐ Troubleshooting127- **Build failure**: Check Docker build logs in Hugging Face Spaces.128- **Missing modules**: Ensure all dependencies are in `requirements.txt`.129- **Port issues**: Ensure Dockerfile runs on port **7860**.130- **Claude API errors**: Verify `CLAUDE_API_KEY` is set correctly in Hugging Face secrets.131 132---133 134## ๐ค Contributing135Pull requests are welcome. Please ensure:136- **Code is tested locally**137- **Dependencies are updated in `requirements.txt`**138- **Secrets are never committed to the repo**139 140---141 142## ๐ License143MIT License 144 145---146 147## ๐ Notes for Hugging Face Deployment148- Create a **new Space** and select **Docker** as SDK 149- Push the repo with `git push` to the provided Space repo link 150- Set **Secrets** in the Space settings (e.g., `CLAUDE_API_KEY`) 151- Hugging Face will automatically build and start the container152 153 154 155 156 