--- title: Reflection With Code Execuation Chart Generation emoji: 🌍 colorFrom: green colorTo: green sdk: gradio sdk_version: 6.5.1 app_file: app.py pinned: false license: apache-2.0 --- # Chart Generation with LLM Agents A Gradio app that uses LLM agents with reflection pattern to generate and improve data visualizations. ## Features - 🤖 **LLM-Powered Chart Generation**: Uses GPT-4 or Claude to generate matplotlib code - 🔄 **Reflection Pattern**: Automatically improves charts by analyzing V1 and generating V2 - 📊 **Coffee Sales Dataset**: Pre-loaded dataset for demonstration - 🎨 **Clean UI**: User-friendly Gradio interface ## How It Works 1. **Generate V1**: LLM creates initial chart code based on your instruction 2. **Execute V1**: Code runs and generates first chart 3. **Reflect**: LLM analyzes V1 chart image and original code 4. **Generate V2**: LLM creates improved code based on feedback 5. **Execute V2**: Improved code runs and generates final chart ## Setup ### Local Development 1. Install dependencies: ```bash pip install -r requirements.txt ``` 2. Set up environment variables: ```bash export OPENAI_API_KEY="your-key-here" # OR export ANTHROPIC_API_KEY="your-key-here" ``` 3. Run the app: ```bash python app.py ``` ### HuggingFace Spaces Deployment 1. Create a new Space on HuggingFace 2. Upload all files: - `app.py` - `utils.py` - `coffee_sales_local.csv` - `requirements.txt` - `README.md` (this file) 3. Add secrets in Space settings: - `OPENAI_API_KEY` (if using OpenAI models) - `ANTHROPIC_API_KEY` (if using Anthropic models) 4. Set Space SDK to **Gradio** 5. The app will automatically deploy! ## Usage 1. Enter your chart instruction (e.g., "Create a plot comparing Q1 coffee sales in 2024 and 2025") 2. Select models for generation (V1) and reflection (V2) 3. Click "Generate Charts" 4. Compare V1 and V2 charts side by side 5. Review the reflection feedback and code ## Dataset Schema The coffee sales dataset includes: - `date` (M/D/YY) - `time` (HH:MM) - `cash_type` (card or cash) - `card` (string) - `price` (number) - `coffee_name` (string) - `quarter` (1-4) - auto-derived - `month` (1-12) - auto-derived - `year` (YYYY) - auto-derived ## Model Recommendations - **Generation (V1)**: Fast models like `gpt-4o-mini` or `gpt-4o` - **Reflection (V2)**: Strong reasoning models like `o1-mini`, `o1-preview`, or `claude-3-5-sonnet-20241022` ## Requirements - Python 3.8+ - OpenAI API key OR Anthropic API key - See `requirements.txt` for full dependency list ## License Educational project for demonstrating LLM agents with reflection pattern. Note: The code is from Deepleraning.ai Agnetic AI Course