This project is a chat-based application that allows users to query a Postgres database in natural language and view the retrieved data in a tabular format. The application leverages Google's Gemini LLM to convert user queries into SQL statements, execute them on the database, and display results in an interactive UI using Gradio. The application is deployed using FastAPI.
- Natural Language Queries: Users can input queries in plain English.
- SQL Query Generation: The LLM generates SQL queries dynamically based on user input.
- Postgres Integration: Executes the generated SQL on a Postgres database.
- Tabular Results: The queried data is displayed in a clean, tabular format.
- Interactive UI: Gradio-based UI for a user-friendly experience.
- FastAPI Backend: API endpoints for query handling and data retrieval.
The project is structured into two main components:
-
Backend (FastAPI): Handles model implementation, database connection, and query execution.
- File:
pipeline.py
- Responsibilities:
- Connect to the Postgres database.
- Use the Gemini model to generate SQL queries.
- Execute the queries and fetch results.
- Expose endpoints to interact with the backend.
- File:
-
Frontend (Gradio): Provides a simple chat-based UI for users.
- File:
main.py
- Responsibilities:
- Build a Gradio interface for user interaction.
- Fetch results from the FastAPI backend and display them in tabular form.
- File:
project-root/
├── pipeline.py # Backend: Model implementation, DB connection, FastAPI endpoints
├── main.py # Frontend: Gradio UI implementation
├── requirements.txt # Python dependencies
└── README.md # Project documentation
- Python 3.8+
- PostgreSQL Database
- Uvicorn (ASGI server for FastAPI)
-
Clone the repository:
git clone cd your_project_name
-
Set up the environment: Install required Python packages:
pip install -r requirements.txt
-
Configure Database Connection: Update the database connection details in
pipeline.py
:DB_CONFIG = { 'host': 'your_db_host', 'port': 'your_db_port', 'database': 'your_db_name', 'user': 'your_db_user', 'password': 'your_db_password' }
-
Run the FastAPI Backend: Start the FastAPI server using Uvicorn:
uvicorn pipeline:app --reload
The API will be available at:
http://localhost:8000
-
Launch the Gradio UI: Run the
main.py
file:python main.py
The Gradio UI will be available in your browser.
- Run the Application: Follow the steps above to launch the FastAPI backend and Gradio UI.
- Access the Gradio Interface: Open the Gradio URL displayed in the terminal.
- Ask Questions: Type a query in natural language (e.g., "Show me all employees in the Sales department").
- View Results: The data will be fetched from the database and displayed in tabular format.
- User enters: "List all customers who made purchases in August."
- The LLM generates the SQL query:
SELECT * FROM customers WHERE purchase_date BETWEEN '2023-08-01' AND '2023-08-31';
- The query is executed on the Postgres database.
- Results are displayed in the Gradio UI.
- Backend: FastAPI
- LLM: Google Gemini
- Database: PostgreSQL
- Frontend: Gradio
- Deployment: Uvicorn
- Add authentication and authorization.
- Support for multiple database backends.
- Implement caching for frequently asked queries.
- Add error handling for invalid SQL or natural language inputs.
Feel free to fork this repository and submit pull requests. For major changes, please open an issue first to discuss what you'd like to change.
For any questions or feedback:
- Name: Thahseer
- Email: your_email@example.com
- GitHub: https://github.com/your_username
Enjoy querying your database effortlessly with natural language! 🚀