Upload 5 files
Browse files- .env +1 -0
- requirements.txt +3 -0
- sql.py +70 -0
- sqlite.py +36 -0
- student.db +0 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GOOGLE_API_KEY = "AIzaSyBZhxdQ0BFLAeHAp9LOc2nqJCgddXq89rU"
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
sql.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
import sqlite3
|
5 |
+
import google.generativeai as genai
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Configure Gemini API
|
11 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
+
|
13 |
+
# Function to load Gemini model and generate SQL query
|
14 |
+
def get_gemini_response(question, prompt):
|
15 |
+
model = genai.GenerativeModel('gemini-pro')
|
16 |
+
full_prompt = prompt + "\n\nUser Query: " + question # Better structuring
|
17 |
+
response = model.generate_content(full_prompt)
|
18 |
+
sql_query = response.text.strip() # Clean the response
|
19 |
+
return sql_query
|
20 |
+
|
21 |
+
# Function to retrieve query results from the database
|
22 |
+
def read_sql_query(sql, db):
|
23 |
+
try:
|
24 |
+
conn = sqlite3.connect(db)
|
25 |
+
cur = conn.cursor()
|
26 |
+
cur.execute(sql)
|
27 |
+
rows = cur.fetchall()
|
28 |
+
conn.close()
|
29 |
+
return rows
|
30 |
+
except Exception as e:
|
31 |
+
return [("Error:", str(e))] # Return error message if query fails
|
32 |
+
|
33 |
+
# Define prompt
|
34 |
+
prompt = """
|
35 |
+
You are an expert in SQL query generation. Your task is to convert natural language questions into valid SQL queries based on the given database schema.
|
36 |
+
|
37 |
+
Instructions:
|
38 |
+
- The SQL database schema will be provided.
|
39 |
+
- Generate a syntactically correct SQL query based on the input question.
|
40 |
+
- The SQL query should be optimized and free from unnecessary clauses.
|
41 |
+
- Do not include SQL keywords or formatting like triple backticks (```) in the response.
|
42 |
+
- If the question is ambiguous, generate the most probable SQL query.
|
43 |
+
|
44 |
+
Example:
|
45 |
+
|
46 |
+
Input: "How many students are in the database?"
|
47 |
+
Output: SELECT COUNT(*) FROM STUDENT_INFO;
|
48 |
+
|
49 |
+
Input: "List all students in CLASS 10 section A."
|
50 |
+
Output: SELECT * FROM STUDENT_INFO WHERE CLASS = '10' AND SECTION = 'A';
|
51 |
+
|
52 |
+
Input: "Show the names of students in Data Science Section."
|
53 |
+
Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
|
54 |
+
"""
|
55 |
+
|
56 |
+
# Streamlit App
|
57 |
+
st.set_page_config(page_title="SQL Query Generator")
|
58 |
+
st.header("Gemini App To Retrieve SQL Data")
|
59 |
+
|
60 |
+
question = st.text_input("Enter your question:", key="input")
|
61 |
+
submit = st.button("Generate SQL Query")
|
62 |
+
|
63 |
+
# If submit is clicked
|
64 |
+
if submit:
|
65 |
+
sql_query = get_gemini_response(question, prompt)
|
66 |
+
st.subheader("Generated SQL Query")
|
67 |
+
st.code(sql_query, language="sql") # Show SQL query
|
68 |
+
|
69 |
+
response = read_sql_query(sql_query, "student.db")
|
70 |
+
|
sqlite.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
|
3 |
+
# Establish a connection to SQLite
|
4 |
+
connection=sqlite3.connect("student.db")
|
5 |
+
|
6 |
+
# Create a cursor object to insert record, create table
|
7 |
+
cursor=connection.cursor()
|
8 |
+
|
9 |
+
## Create the table
|
10 |
+
table_info = """
|
11 |
+
CREATE TABLE STUDENT_INFO(NAME VARCHAR(25), CLASS VARCHAR(25),
|
12 |
+
SECTION VARCHAR(25));
|
13 |
+
"""
|
14 |
+
|
15 |
+
cursor.execute(table_info)
|
16 |
+
|
17 |
+
# Insert Some more records
|
18 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Ashish', 'Data Scientist', 'A')''')
|
19 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Devesh', 'Software Developer', 'A')''')
|
20 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Tanuja', 'Cyber Security', 'A')''')
|
21 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Hardeep', 'UX Design', 'A')''')
|
22 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Rahul', 'Data Analyst', 'A')''')
|
23 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Pawan', 'Data Scientist', 'A')''')
|
24 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Neeraj', 'Backend Developer', 'A')''')
|
25 |
+
cursor.execute('''Insert Into STUDENT_INFO values('Neeraja', 'Data Scientist', 'A')''')
|
26 |
+
|
27 |
+
|
28 |
+
# Display All the records
|
29 |
+
print("The Inserted records are")
|
30 |
+
data=cursor.execute('''Select * from STUDENT_INFO''')
|
31 |
+
for row in data:
|
32 |
+
print(row)
|
33 |
+
|
34 |
+
# Commit your changes in the Database
|
35 |
+
connection.commit()
|
36 |
+
connection.close()
|
student.db
ADDED
Binary file (8.19 kB). View file
|
|