Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import sqlite3
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
import streamlit as st
|
6 |
+
import google.generativeai as genai
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
+
|
12 |
+
|
13 |
+
def get_gemini_response(question, prompt):
|
14 |
+
model = genai.GenerativeModel("gemini-1.5-pro-latest")
|
15 |
+
response = model.generate_content([prompt[0], question])
|
16 |
+
return response.text
|
17 |
+
|
18 |
+
|
19 |
+
def read_sql_query(response, db):
|
20 |
+
conn = sqlite3.connect(db)
|
21 |
+
cur = conn.cursor()
|
22 |
+
|
23 |
+
# Extract SQL query from the response
|
24 |
+
sql_match = re.search(r'```sql\n(.*?)\n```', response, re.DOTALL)
|
25 |
+
if sql_match:
|
26 |
+
sql = sql_match.group(1).strip()
|
27 |
+
else:
|
28 |
+
# If no SQL code block is found, try to find a SQL-like statement
|
29 |
+
sql_match = re.search(r'SELECT.*?FROM.*?;', response, re.DOTALL | re.IGNORECASE)
|
30 |
+
if sql_match:
|
31 |
+
sql = sql_match.group(0).strip()
|
32 |
+
else:
|
33 |
+
st.header("Error: No valid SQL query found in the response.\nPlease write more clear retrieval query.")
|
34 |
+
exit()
|
35 |
+
try:
|
36 |
+
cur.execute(sql)
|
37 |
+
rows = cur.fetchall()
|
38 |
+
conn.close()
|
39 |
+
return rows
|
40 |
+
except sqlite3.Error as e:
|
41 |
+
conn.close()
|
42 |
+
return f"SQLite error: {e}"
|
43 |
+
|
44 |
+
|
45 |
+
prompt = [
|
46 |
+
"""
|
47 |
+
You are an expert in converting English questions to SQL code.
|
48 |
+
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
49 |
+
SECTION \n\nFor example, \nExample 1: How many entries of records are present?,
|
50 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
51 |
+
\nExample 2: What are the names of students who study in AI?, the SQL command will be
|
52 |
+
something like this SELECT NAME FROM STUDENT WHERE CLASS = "AI";
|
53 |
+
also the sql code should not have ''' in beginning or end or a 'sql' word in output
|
54 |
+
"""
|
55 |
+
]
|
56 |
+
|
57 |
+
st.set_page_config(page_title="SQL Retrieval App")
|
58 |
+
st.info("This is a demo the default db is a student database containing a table of student name, class and section")
|
59 |
+
st.header("Gemini App - Retrieve SQL Data")
|
60 |
+
|
61 |
+
|
62 |
+
question = st.text_input("Input a retrieval question about the database (e.g. get all student names and class) ", key="input")
|
63 |
+
|
64 |
+
|
65 |
+
submit = st.button("Ask SQL")
|
66 |
+
|
67 |
+
if submit:
|
68 |
+
response = get_gemini_response(question, prompt)
|
69 |
+
print(response)
|
70 |
+
response = read_sql_query(response, "student.db")
|
71 |
+
st.subheader("Response: ")
|
72 |
+
for row in response:
|
73 |
+
print(row)
|
74 |
+
st.header(row)
|