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