Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +70 -0
- requirements.txt +3 -0
- student.db +0 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
import google.generativeai as genai
|
9 |
+
|
10 |
+
## Configure Genai Key
|
11 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
+
|
13 |
+
## Function to Load Google Gemini Model and provide queries as response
|
14 |
+
|
15 |
+
def get_gemini_response(question,prompt):
|
16 |
+
model = genai.GenerativeModel('gemini-pro')
|
17 |
+
response = model.generate_content([prompt[0],question])
|
18 |
+
return response.text
|
19 |
+
|
20 |
+
|
21 |
+
## Function to retrieve query from the database
|
22 |
+
|
23 |
+
def read_sql_query(sql,db):
|
24 |
+
conn = sqlite3.connect(db)
|
25 |
+
cur = conn.cursor()
|
26 |
+
cur.execute(sql)
|
27 |
+
rows = cur.fetchall()
|
28 |
+
conn.commit()
|
29 |
+
conn.close()
|
30 |
+
for row in rows:
|
31 |
+
print(row)
|
32 |
+
return rows
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
## Define Your Prompt
|
37 |
+
prompt=[
|
38 |
+
"""
|
39 |
+
You are an expert in converting English questions to SQL query!
|
40 |
+
The SQL database has the name STUDENT and has the following column - NAME, CLASS, SECTION
|
41 |
+
\n\n For example, \nExample 1 - How many entries of records are present?,
|
42 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
43 |
+
\n Example 2 - Tell me all the students studying in Data Science class?,
|
44 |
+
the SQL command will be something like this SELECT * FROM STUDENT
|
45 |
+
where CLASS="Data Science";
|
46 |
+
also the sql code should not have ``` in begining or end and sql word in output
|
47 |
+
|
48 |
+
"""
|
49 |
+
]
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
## SetUp streamlit App
|
54 |
+
|
55 |
+
st.set_page_config(page_title="I can Retrieve Any SQL query")
|
56 |
+
st.header("Gemini App to Retrieve SQL query")
|
57 |
+
|
58 |
+
question = st.text_input("Input: ",key="input")
|
59 |
+
|
60 |
+
submit = st.button("Ask the question")
|
61 |
+
|
62 |
+
# if submit is clicked
|
63 |
+
if submit:
|
64 |
+
response = get_gemini_response(question,prompt)
|
65 |
+
print(response)
|
66 |
+
response = read_sql_query(response,"student.db")
|
67 |
+
st.subheader("The Response is ")
|
68 |
+
for row in response:
|
69 |
+
# print(row)
|
70 |
+
st.header(row)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.34.0
|
2 |
+
google-generativeai==0.5.2
|
3 |
+
python-dotenv==1.0.1
|
student.db
ADDED
Binary file (8.19 kB). View file
|
|