Spaces:
Sleeping
Sleeping
chandu0802
commited on
Commit
•
8d674a3
1
Parent(s):
6f8d957
Upload 5 files
Browse files- .env +1 -0
- app.py +67 -0
- requirements.txt +3 -0
- sql.py +50 -0
- student.db +0 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GOOGLE_API_KEY="AIzaSyA7ruwOEoF2Rnj4qVp2-cH53PDu4uzhJ6U"
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv() ## load all the environemnt variables
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
import os
|
6 |
+
import sqlite3
|
7 |
+
|
8 |
+
import google.generativeai as genai
|
9 |
+
## Configure Genai Key
|
10 |
+
|
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 |
+
## Fucntion To retrieve query from the database
|
21 |
+
|
22 |
+
def read_sql_query(sql,db):
|
23 |
+
conn=sqlite3.connect(db)
|
24 |
+
cur=conn.cursor()
|
25 |
+
cur.execute(sql)
|
26 |
+
rows=cur.fetchall()
|
27 |
+
conn.commit()
|
28 |
+
conn.close()
|
29 |
+
for row in rows:
|
30 |
+
print(row)
|
31 |
+
return rows
|
32 |
+
|
33 |
+
## Define Your Prompt
|
34 |
+
prompt=[
|
35 |
+
"""
|
36 |
+
You are an expert in converting English questions to SQL query!
|
37 |
+
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
38 |
+
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
|
39 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
40 |
+
\nExample 2 - Tell me all the students studying in Data Science class?,
|
41 |
+
the SQL command will be something like this SELECT * FROM STUDENT
|
42 |
+
where CLASS="Data Science";
|
43 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
44 |
+
|
45 |
+
"""
|
46 |
+
|
47 |
+
|
48 |
+
]
|
49 |
+
|
50 |
+
## Streamlit App
|
51 |
+
|
52 |
+
st.set_page_config(page_title="Query Master App")
|
53 |
+
st.header("Query Master App")
|
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.header(row)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
sql.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
|
3 |
+
## Connectt to SQlite
|
4 |
+
connection=sqlite3.connect("student.db")
|
5 |
+
|
6 |
+
# Create a cursor object to insert record,create table
|
7 |
+
|
8 |
+
cursor=connection.cursor()
|
9 |
+
|
10 |
+
## create the table
|
11 |
+
table_info="""
|
12 |
+
Create table STUDENT(NAME VARCHAR(25),CLASS VARCHAR(25),
|
13 |
+
SECTION VARCHAR(25),MARKS INT);
|
14 |
+
|
15 |
+
"""
|
16 |
+
cursor.execute(table_info)
|
17 |
+
|
18 |
+
## Insert Some more records
|
19 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Chandu', 'Data Science', 'A', 100)''')
|
20 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Darius', 'Data Science', 'B', 85)''')
|
21 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Sudhanshu', 'Devops', 'C', 78)''')
|
22 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vikash', 'Data Science', 'C', 92)''')
|
23 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Yateesh', 'Data Science', 'A', 88)''')
|
24 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Dinesh', 'Data Science', 'B', 86)''')
|
25 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Sushanth', 'Devops', 'C', 79)''')
|
26 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vinay', 'Data Science', 'C', 91)''')
|
27 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Krishna', 'Data Science', 'A', 89)''')
|
28 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Dileep', 'Data Science', 'B', 87)''')
|
29 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Sudheer', 'Devops', 'C', 75)''')
|
30 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vishal', 'Data Science', 'C', 93)''')
|
31 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Kishore', 'Data Science', 'A', 94)''')
|
32 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Gopi', 'Data Science', 'B', 83)''')
|
33 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Kushal', 'Devops', 'C', 77)''')
|
34 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vamsi', 'Data Science', 'C', 90)''')
|
35 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('srinu', 'Data Science', 'A', 85)''')
|
36 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('David', 'Data Science', 'B', 80)''')
|
37 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Sumanth', 'Devops', 'C', 82)''')
|
38 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vineesh', 'Data Science', 'C', 89)''')
|
39 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vivek', 'Data Science', 'A', 87)''')
|
40 |
+
|
41 |
+
## Disspaly ALl the records
|
42 |
+
|
43 |
+
print("The isnerted records are")
|
44 |
+
data=cursor.execute('''Select * from STUDENT''')
|
45 |
+
for row in data:
|
46 |
+
print(row)
|
47 |
+
|
48 |
+
## Commit your changes int he databse
|
49 |
+
connection.commit()
|
50 |
+
connection.close()
|
student.db
ADDED
Binary file (8.19 kB). View file
|
|