Commit ·
7e8eba4
1
Parent(s): 93c945e
initial setup
Browse files- .gitignore +2 -0
- app.py +65 -0
- requirements.txt +3 -0
- sqlite.py +36 -0
- student.db +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv
|
| 2 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv() ## load all the env valriable
|
| 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 |
+
|
| 12 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
+
|
| 14 |
+
## function to load google gemini model
|
| 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 |
+
## function to retrive 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 prompt
|
| 35 |
+
prompt=[
|
| 36 |
+
"""
|
| 37 |
+
You are an expert in connecting 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 DS class?,
|
| 42 |
+
the SQL command will be something like this SELECT * FROM Student
|
| 43 |
+
where Class="DS";
|
| 44 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
| 45 |
+
"""
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
## streamlit app
|
| 49 |
+
|
| 50 |
+
st.set_page_config(page_title="I can retrive any SQL query")
|
| 51 |
+
st.header("Gemini App To Retrive SQL Data")
|
| 52 |
+
|
| 53 |
+
question=st.text_input("Input: ",key="input")
|
| 54 |
+
submit=st.button("Ask the question")
|
| 55 |
+
|
| 56 |
+
## if submit is clicked
|
| 57 |
+
if submit:
|
| 58 |
+
response=get_gemini_response(question,prompt)
|
| 59 |
+
print(response)
|
| 60 |
+
response = read_sql_query(response, "student.db")
|
| 61 |
+
st.subheader("The response is")
|
| 62 |
+
for row in response:
|
| 63 |
+
print(row)
|
| 64 |
+
st.header(row)
|
| 65 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
python-dotenv
|
sqlite.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
## connect to sqlite
|
| 4 |
+
connection=sqlite3.connect("student.db")
|
| 5 |
+
|
| 6 |
+
## create a cursor object to inset record,create table
|
| 7 |
+
|
| 8 |
+
cursor = connection.cursor()
|
| 9 |
+
|
| 10 |
+
## create the table
|
| 11 |
+
table_info="""
|
| 12 |
+
create table Student(
|
| 13 |
+
Name varchar(50),
|
| 14 |
+
Class varchar(25),
|
| 15 |
+
Section varchar(25),
|
| 16 |
+
Mark int
|
| 17 |
+
);
|
| 18 |
+
"""
|
| 19 |
+
cursor.execute(table_info)
|
| 20 |
+
|
| 21 |
+
## insert some records
|
| 22 |
+
|
| 23 |
+
cursor.execute('''insert into Student values('Test1','DS','A',99)''')
|
| 24 |
+
cursor.execute('''insert into Student values('Test2','DS','B',95)''')
|
| 25 |
+
cursor.execute('''insert into Student values('Test3','DS','C',90)''')
|
| 26 |
+
cursor.execute('''insert into Student values('Test4','DO','A',80)''')
|
| 27 |
+
cursor.execute('''insert into Student values('Test5','DO','B',85)''')
|
| 28 |
+
|
| 29 |
+
## Display all the records
|
| 30 |
+
|
| 31 |
+
print("The inserted records are")
|
| 32 |
+
data = cursor.execute('''Select * from Student''')
|
| 33 |
+
for row in data:
|
| 34 |
+
print(row)
|
| 35 |
+
connection.commit()
|
| 36 |
+
connection.close()
|
student.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|