Upload 3 files
Browse files- app.py +59 -0
- requirements.txt +3 -0
- sqlite.py +32 -0
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
|
3 |
+
load_dotenv()
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
import os
|
8 |
+
import sqlite3
|
9 |
+
import google.generativeai as genai
|
10 |
+
|
11 |
+
genai.configure(api_key = os.getenv("GOOGLE_API_KEY"))
|
12 |
+
|
13 |
+
def get_gemini_reponse(question,prompt):
|
14 |
+
model = genai.GenerativeModel("gemini-pro")
|
15 |
+
response = model.generate_content([prompt[0],question])
|
16 |
+
return response.text
|
17 |
+
|
18 |
+
## functio to retreive query
|
19 |
+
|
20 |
+
def read_sql_query(sql,db):
|
21 |
+
conn = sqlite3.connect(db)
|
22 |
+
cur = conn.cursor()
|
23 |
+
cur.execute(sql)
|
24 |
+
rows = cur.fetchall()
|
25 |
+
for row in rows:
|
26 |
+
print(row)
|
27 |
+
return rows
|
28 |
+
|
29 |
+
## dedfine prompt
|
30 |
+
|
31 |
+
prompt = [
|
32 |
+
"""
|
33 |
+
you are expert in converting english questions to SQL query!
|
34 |
+
The SQL database has the name STUDENT and has the following columns - NAME,CLASS
|
35 |
+
,SECTION \n\n For example , \nExample 1 - how many entries of records are present ?,
|
36 |
+
the sql command will be something like this SELECT COUNT(*)from student;
|
37 |
+
\nExample 2 - tell me all the name of students study in data sciense class?,
|
38 |
+
the sql command will be something like this SELECT * from student where class="data science" from student;
|
39 |
+
also the sql code should not have ```in begining or end and sql word in output
|
40 |
+
|
41 |
+
"""
|
42 |
+
]
|
43 |
+
|
44 |
+
|
45 |
+
st.set_page_config(page_title = "I can retrieve any sql query")
|
46 |
+
st.header(" gemini app to retrieve sql data")
|
47 |
+
|
48 |
+
question = st.text_input("input: ",key = "input")
|
49 |
+
|
50 |
+
submit= st.button("ask the question")
|
51 |
+
|
52 |
+
if submit :
|
53 |
+
response = get_gemini_reponse(question,prompt)
|
54 |
+
print(response)
|
55 |
+
response = read_sql_query(response,"test.db")
|
56 |
+
st.subheader(" the response is")
|
57 |
+
for row in response:
|
58 |
+
print(row)
|
59 |
+
st.header(row)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
google-generativeai
|
2 |
+
streamlit
|
3 |
+
python-dotenv
|
sqlite.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
## connect to sqlite
|
3 |
+
connection = sqlite3.connect("test.db")
|
4 |
+
|
5 |
+
# Create a cursor object to insert record , create table
|
6 |
+
|
7 |
+
|
8 |
+
cursor = connection.cursor()
|
9 |
+
|
10 |
+
## create the table
|
11 |
+
|
12 |
+
table_info = """
|
13 |
+
Create table STUDENT(NAME VARCHAR(25),CLASS VARCHAR(25),
|
14 |
+
SECTION VARCHAR(25)
|
15 |
+
);
|
16 |
+
"""
|
17 |
+
cursor.execute(table_info)
|
18 |
+
|
19 |
+
cursor.execute('''Insert Into STUDENT values('sushil','data science','A')''')
|
20 |
+
cursor.execute('''Insert Into STUDENT values('ravi','data science','B')''')
|
21 |
+
cursor.execute('''Insert Into STUDENT values('sumit','data science','A')''')
|
22 |
+
cursor.execute('''Insert Into STUDENT values('rahul','devops','A')''')
|
23 |
+
cursor.execute('''Insert Into STUDENT values('vikram','devops','A')''')
|
24 |
+
|
25 |
+
print("the inserted records are")
|
26 |
+
|
27 |
+
data = cursor.execute('''Select * from student''')
|
28 |
+
for row in data:
|
29 |
+
print(row)
|
30 |
+
|
31 |
+
connection.commit()
|
32 |
+
connection.close()
|