Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,72 @@
|
|
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 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
return
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
- The SQL
|
41 |
-
-
|
42 |
-
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
""
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
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 |
+
api_key = st.secrets["GOOGLE_API_KEY"]
|
14 |
+
|
15 |
+
# Function to load Gemini model and generate SQL query
|
16 |
+
def get_gemini_response(question, prompt):
|
17 |
+
model = genai.GenerativeModel('gemini-pro')
|
18 |
+
full_prompt = prompt + "\n\nUser Query: " + question # Better structuring
|
19 |
+
response = model.generate_content(full_prompt)
|
20 |
+
sql_query = response.text.strip() # Clean the response
|
21 |
+
return sql_query
|
22 |
+
|
23 |
+
# Function to retrieve query results from the database
|
24 |
+
def read_sql_query(sql, db):
|
25 |
+
try:
|
26 |
+
conn = sqlite3.connect(db)
|
27 |
+
cur = conn.cursor()
|
28 |
+
cur.execute(sql)
|
29 |
+
rows = cur.fetchall()
|
30 |
+
conn.close()
|
31 |
+
return rows
|
32 |
+
except Exception as e:
|
33 |
+
return [("Error:", str(e))] # Return error message if query fails
|
34 |
+
|
35 |
+
# Define prompt
|
36 |
+
prompt = """
|
37 |
+
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.
|
38 |
+
|
39 |
+
Instructions:
|
40 |
+
- The SQL database schema will be provided.
|
41 |
+
- Generate a syntactically correct SQL query based on the input question.
|
42 |
+
- The SQL query should be optimized and free from unnecessary clauses.
|
43 |
+
- Do not include SQL keywords or formatting like triple backticks (```) in the response.
|
44 |
+
- If the question is ambiguous, generate the most probable SQL query.
|
45 |
+
|
46 |
+
Example:
|
47 |
+
|
48 |
+
Input: "How many students are in the database?"
|
49 |
+
Output: SELECT COUNT(*) FROM STUDENT_INFO;
|
50 |
+
|
51 |
+
Input: "List all students in CLASS 10 section A."
|
52 |
+
Output: SELECT * FROM STUDENT_INFO WHERE CLASS = '10' AND SECTION = 'A';
|
53 |
+
|
54 |
+
Input: "Show the names of students in Data Science Section."
|
55 |
+
Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
|
56 |
+
"""
|
57 |
+
|
58 |
+
# Streamlit App
|
59 |
+
st.set_page_config(page_title="SQL Query Generator")
|
60 |
+
st.header("Gemini App To Retrieve SQL Data")
|
61 |
+
|
62 |
+
question = st.text_input("Enter your question:", key="input")
|
63 |
+
submit = st.button("Generate SQL Query")
|
64 |
+
|
65 |
+
# If submit is clicked
|
66 |
+
if submit:
|
67 |
+
sql_query = get_gemini_response(question, prompt)
|
68 |
+
st.subheader("Generated SQL Query")
|
69 |
+
st.code(sql_query, language="sql") # Show SQL query
|
70 |
+
|
71 |
+
response = read_sql_query(sql_query, "student.db")
|
72 |
+
|