This project is an intelligent E-commerce assistant that helps you analyze your product ad performance and sales data using natural language questions. It uses the power of Google's Gemini AI to generate SQL queries, human-readable explanations, and visualizations based on your SQLite product ads database.
Ecommerce-Data-Analysis-Agent/
├── app.py # Streamlit frontend
├── main.py # FastAPI backend
├── .env # Environment variables (GEMINI_API_KEY)
├── requirements.txt # Python dependencies
├── setup.py # Placeholder for setup (DB creation not implemented yet)
├── data/
│ ├── csv_files/ # Placeholder for any CSV data
│ └── product_ads.db # SQLite database file
├── src/
│ └── handlers/
│ ├── database_handler.py # Handles SQLite operations
│ └── llm.py # Handles Gemini LLM interactions
- ✅ Natural language to SQL translation using Gemini
- ✅ Execution of dynamic SQL queries on sqlite database
- ✅ Explanation generation for SQL query results
- ✅ Intelligent chart generation (line, bar, scatter) based on query output
- ✅ Web-based UI built with Streamlit
- ✅ API backend using FastAPI
git clone https://github.com/nithinrvs/Ecommerce-Data-Analysis-Agent.gitpip install -r requirements.txtCreate a .env file in the root directory:
GEMINI_API_KEY=your_gemini_api_key_hereEnsure the file data/product_ads.db exists. If not, you must create it and populate the following tables:
product_eligibilitydaily_ad_metricsdaily_total_sales
setup.py creates the database. It should be executed once after cloning the project
uvicorn main:app --reloadBy default, this runs at: http://localhost:8000
streamlit run app.pyThis will open a web interface in your browser.
| Column | Type |
|---|---|
| eligibility_datetime_utc | TEXT |
| item_id | INTEGER |
| eligibility | BOOLEAN |
| message | TEXT |
| Column | Type |
|---|---|
| date | TEXT |
| item_id | INTEGER |
| ad_sales | REAL |
| impressions | INTEGER |
| ad_spend | REAL |
| clicks | INTEGER |
| units_sold | INTEGER |
| Column | Type |
|---|---|
| date | TEXT |
| item_id | INTEGER |
| total_sales | REAL |
| total_units_ordered | INTEGER |
Request JSON:
{
"question": "Which item had the highest ad spend last week?"
}Response JSON:
{
"sql": "...", // Generated SQL query
"results": [...], // List of rows (dictionaries)
"explanation": "...", // Natural language summary
"chart": "base64string" // Optional base64 PNG chart (if applicable)
}- "Which item had the highest ad spend last week?"
- "Show total sales and units ordered per item"
- "What were the daily ad impressions for item 101?"
streamlitrequestsfastapiuvicornpython-dotenvpydanticgoogle-generativeaimatplotlibseabornpandas
- You must have an active Google Gemini API Key to use this app.
- Streamlit and FastAPI are separate processes — ensure both are running concurrently.
- Charts are auto-generated only if Gemini suggests one based on the result data.
- Built with ❤️ using Python, Gemini, Streamlit, and FastAPI
- For educational or internal usage only — production hardening needed