Jaichandran1984
commited on
Commit
•
15d88cc
1
Parent(s):
85bb8a4
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,69 @@
|
|
1 |
-
from dotenv import load_dotenv
|
2 |
-
load_dotenv()
|
3 |
-
|
4 |
-
import streamlit as st
|
5 |
-
import os
|
6 |
-
import sqlite3
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
rows
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
st.
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv() # Load all the environment variables
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
import os
|
6 |
+
import sqlite3
|
7 |
+
import google.generativeai as genai
|
8 |
+
|
9 |
+
# Configure Genai Key
|
10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
+
|
12 |
+
# Function to load Google Gemini Model and provide queries as response
|
13 |
+
def get_gemini_response(question, prompt):
|
14 |
+
model = genai.GenerativeModel('gemini-pro')
|
15 |
+
response = model.generate_content([prompt[0], question])
|
16 |
+
return response.text
|
17 |
+
|
18 |
+
# Function to retrieve query from the database
|
19 |
+
def read_sql_query(sql, db):
|
20 |
+
conn = sqlite3.connect(db)
|
21 |
+
cur = conn.cursor()
|
22 |
+
cur.execute(sql)
|
23 |
+
rows = cur.fetchall()
|
24 |
+
conn.commit()
|
25 |
+
conn.close()
|
26 |
+
return rows
|
27 |
+
|
28 |
+
# Define Your Prompt
|
29 |
+
prompt = [
|
30 |
+
"""
|
31 |
+
You are an expert in converting English questions to SQL query!
|
32 |
+
The SQL database has the name COMPANY and has the following columns - FIRST_NAME, LAST_NAME, DEPT,
|
33 |
+
SALARY, AGE \n\nFor example,\nExample 1 - How many entries of records are present?,
|
34 |
+
the SQL command will be something like this SELECT COUNT(*) FROM COMPANY ;
|
35 |
+
\nExample 2 - Tell me all the students studying in Data Science class?,
|
36 |
+
the SQL command will be something like this SELECT * FROM COMPANY
|
37 |
+
where DEPT="Data Science";
|
38 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
39 |
+
"""
|
40 |
+
]
|
41 |
+
|
42 |
+
# Streamlit App
|
43 |
+
st.set_page_config(page_title="Retrieve Any SQL query")
|
44 |
+
st.header("App To Generate SQL & Data")
|
45 |
+
|
46 |
+
question = st.text_input("Input: ", key="input")
|
47 |
+
|
48 |
+
submit = st.button("Ask the question")
|
49 |
+
|
50 |
+
# if submit is clicked
|
51 |
+
if submit:
|
52 |
+
response = get_gemini_response(question, prompt)
|
53 |
+
st.subheader("Generated SQL Query")
|
54 |
+
st.code(response, language='sql')
|
55 |
+
|
56 |
+
try:
|
57 |
+
data = read_sql_query(response, "COMPANY.db")
|
58 |
+
st.subheader("The Response is")
|
59 |
+
for row in data:
|
60 |
+
st.write(row)
|
61 |
+
except sqlite3.Error as e:
|
62 |
+
st.error(f"Error: {e}")
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|