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