This Python application enables users to interact with a SQL bot through a Streamlit UI. The bot answers queries related to sales data stored in an SQLite database, utilizing LangChain and the HuggingFace API for natural language processing.
- Sample Sales Data Generation: Automatically populates an SQLite database (
sales_data.db) with sample sales data. - User Query Handling: Accepts natural language queries about the sales data.
- AI-Powered SQL Generation: Uses the
Mistral-7Bmodel from Hugging Face to translate user queries into SQL statements. - Query Execution: Executes the generated SQL queries on the SQLite database.
- Results Display: Presents query results within the Streamlit interface.
chat_excel_bot/
├── .env # Environment variables
├── requirements.txt # Dependencies
├── app.py # Main Python application
├── sales_data.db # SQLite database with sample sales data
- Python 3.x installed (Download Python)
- Visual Studio Code (Download VS Code)
- Open VS Code.
- Navigate to Extensions (
Ctrl + Shift + X). - Install the following extensions:
- Python (Microsoft)
- Pylance
- Jupyter (if using notebooks)
- autopep8 (for code formatting)
git clone https://github.com/palanikumarmsc/chat_excel_bot.git
cd chat_excel_botpython -m venv venvActivate the virtual environment:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
Ensure your requirements.txt includes:
streamlit
pandas
python-dotenv
langchain
huggingface_hub
Then install the dependencies:
pip install -r requirements.txtTo use Hugging Face models, you need an API token:
- Go to https://huggingface.co
- Sign up or log in
- Go to your profile → Settings → Access Tokens
- Create a new token with "Read" access
- In the root of your project, create a
.envfile with:
Create a .env file and add:
MODEL_NAME='mistralai/Mistral-7B-Instruct-v0.3'
HUGGINGFACEHUB_API_TOKEN='your_huggingface_api_key'
Replace 'your_huggingface_api_key' with your actual Hugging Face API token.
streamlit run app.pyAccess the application in your browser at:
http://localhost:8501
- Navigate to Run → Add Configuration → Select Python.
- Add the following configuration in
.vscode/launch.json:{ "version": "0.2.0", "configurations": [ { "name": "Python: Streamlit", "type": "python", "request": "launch", "program": "${workspaceFolder}/app.py", "console": "integratedTerminal" } ] } - Press F5 or click Run and Debug to start debugging.
- User Query:
How many Product A sales were made this month? - Generated SQL Query:
SELECT SUM(Quantity) FROM Sales WHERE Product = 'Product A' AND strftime('%Y-%m', Date) = '2025-03';
- Result Displayed in Streamlit UI 🎉
- List all sales records.
SELECT * FROM Sales;
- Show the total quantity of products sold in March 2025.
SELECT SUM(Quantity) FROM Sales WHERE strftime('%Y-%m', Date) = '2025-03';
- Retrieve all sales for the product 'Laptop' in February 2024.
SELECT * FROM Sales WHERE Product = 'Laptop' AND strftime('%Y-%m', Date) = '2024-02';
- Get the average price of products sold in 2023.
SELECT AVG(Price) FROM Sales WHERE strftime('%Y', Date) = '2023';
- Find the total revenue (Quantity * Price) for each product in the current month.
SELECT Product, SUM(Quantity * Price) AS TotalRevenue FROM Sales WHERE strftime('%Y-%m', Date) = '2025-03' GROUP BY Product;
- List distinct products sold in the last 7 days.
SELECT DISTINCT Product FROM Sales WHERE Date >= date('now', '-7 days');
- Show all sales transactions made on '2025-03-15'.
SELECT * FROM Sales WHERE Date = '2025-03-15';
- Retrieve the total number of sales transactions in the year 2024.
SELECT COUNT(*) FROM Sales WHERE strftime('%Y', Date) = '2024';
- List all sales where more than 10 units were sold in a single transaction.
SELECT * FROM Sales WHERE Quantity > 10;
- Find the top 5 best-selling products based on quantity sold.
SELECT Product, SUM(Quantity) AS TotalSold FROM Sales GROUP BY Product ORDER BY TotalSold DESC LIMIT 5;
- Enhance Query Parsing: Improve the bot's ability to understand and process complex queries.
- User Query Analytics: Implement functionality to store and analyze user queries and responses.
- Custom Data Upload: Allow users to upload and interact with their own sales data.