This is an intelligent web application that allows users to convert natural language questions into SQL queries. The app integrates the Google Gemini 1.5 Pro API to generate accurate SQL queries based on user input and retrieve data from an SQLite database. Built using Python, Streamlit, and SQLite, the app provides a simple and interactive interface to query data from a database using conversational language.
- Natural Language to SQL Conversion: Converts user input (natural language questions) into SQL queries to interact with an SQLite database.
- Real-Time Data Retrieval: Executes the AI-generated SQL queries on an SQLite database to retrieve real-time results.
- Streamlit Interface: A clean and intuitive web interface to input questions and view results in real-time.
- Optimized Prompts: Engineered precise prompts for the Google Gemini 1.5 Pro model to ensure accurate query generation under different scenarios.
- Streamlit: For building the web application interface.
- Google Gemini 1.5 Pro API: Used to generate SQL queries from natural language input.
- SQLite: Database used for storing and retrieving data.
- Python: Programming language for the backend logic.
- Prompt Engineering: Fine-tuned prompts for generating accurate SQL queries.
-
Clone the repository:
git clone https://github.com/yourusername/ai-sql-query-generator.git
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required dependencies:
pip install -r requirements.txt
-
Set up the
.envfile with your Google Gemini API key:- Create a
.envfile in the root directory and add your API key:
GOOGLE_API_KEY=your_google_gemini_api_key - Create a
-
For using existing database entries u can run this file:
python sql.py
-
Start the Streamlit app:
streamlit run app.py
-
Open your browser and navigate to
http://localhost:8501to interact with the app.
- Input a Question: In the web app, type a natural language question related to the SQLite database (e.g., "How many students are there in class A?").
- Get SQL Query: The app will generate a corresponding SQL query using the Google Gemini 1.5 Pro model.
- View Results: The app will execute the generated SQL query on the SQLite database and display the results.
Question: "What is the average marks of students in Data Science class?"
- Generated SQL Query:
SELECT AVG(MARKS) FROM STUDENT WHERE CLASS = 'Data Science'; - Result: Displays the average marks of students in the Data Science class from the SQLite database.
"Show me the names of students who scored above the average marks in their respective classes, along with their course name, section, and the name of the teacher instructing that course, student marks in descending order."
SELECT
s.NAME,
c.COURSE_NAME,
s.SECTION,
t.NAME AS TEACHER_NAME,
s.MARKS
FROM
STUDENT s
INNER JOIN
COURSE c ON s.CLASS_ID = c.ID
INNER JOIN
TEACHER t ON c.INSTRUCTOR_ID = t.ID
WHERE
s.MARKS > (
SELECT AVG(MARKS)
FROM STUDENT s2
WHERE s2.CLASS_ID = s.CLASS_ID
)
ORDER BY
s.MARKS DESC;