Upload 3 files
Browse files- App.py +65 -0
- sqllite.py +38 -0
- student.db +0 -0
App.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
|
| 3 |
+
load_dotenv() # load all env variables
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import os
|
| 7 |
+
import sqlite3
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
|
| 10 |
+
#configure GenAi key
|
| 11 |
+
genai.configure(api_key="AIzaSyAzD179LQXUgTZJf3i7XnUWJjmlskehOo4")
|
| 12 |
+
|
| 13 |
+
#func to load gemini model
|
| 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 |
+
#func to retervie query from database
|
| 21 |
+
|
| 22 |
+
def read_sql_query(sql,db):
|
| 23 |
+
conn=sqlite3.connect(db)
|
| 24 |
+
curr=conn.cursor()
|
| 25 |
+
curr.execute(sql)
|
| 26 |
+
rows=curr.fetchall()
|
| 27 |
+
conn.commit()
|
| 28 |
+
conn.close()
|
| 29 |
+
for row in rows:
|
| 30 |
+
print(row)
|
| 31 |
+
return rows
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
#Define your Prompt
|
| 35 |
+
prompt=[
|
| 36 |
+
"""
|
| 37 |
+
you are an expert in converting English questions to SQL query!
|
| 38 |
+
database has the name STUDENT and has the following columns - NAME, CLASS,
|
| 39 |
+
SECTION \n\n for 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 * FRON STUDENT 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 |
+
#streamlit app
|
| 48 |
+
|
| 49 |
+
st.set_page_config(page_title="I can retrieve any sql query")
|
| 50 |
+
st.header("GEMINI APP To Retervie SQL Data")
|
| 51 |
+
|
| 52 |
+
question=st.text_input("Input: ",key="input")
|
| 53 |
+
|
| 54 |
+
submit=st.button("Ask the question")
|
| 55 |
+
|
| 56 |
+
#if submit is clicked
|
| 57 |
+
|
| 58 |
+
if submit:
|
| 59 |
+
response=get_gemini_response(question,prompt)
|
| 60 |
+
print(response)
|
| 61 |
+
response=read_sql_query(response,"student.db")
|
| 62 |
+
st.subheader("The Response is ")
|
| 63 |
+
for row in response:
|
| 64 |
+
print(row)
|
| 65 |
+
st.header(row)
|
sqllite.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
#connect to SQLlite
|
| 4 |
+
connection=sqlite3.connect("student.db")
|
| 5 |
+
|
| 6 |
+
#create a cursor to insert record,create table
|
| 7 |
+
|
| 8 |
+
cursor=connection.cursor()
|
| 9 |
+
|
| 10 |
+
#create a table
|
| 11 |
+
table_info="""
|
| 12 |
+
Create table STUDENT(NAME VARCHAR(25),CLASS VARCHAR(25),
|
| 13 |
+
SECTION VARCHAR(25));
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
#creating the table
|
| 17 |
+
cursor.execute(table_info)
|
| 18 |
+
|
| 19 |
+
#insert some more records
|
| 20 |
+
|
| 21 |
+
cursor.execute("""Insert Into STUDENT values("Krish","Data Science","A")""")
|
| 22 |
+
cursor.execute("""Insert Into STUDENT values("kayal","DevOps","A")""")
|
| 23 |
+
cursor.execute("""Insert Into STUDENT values("david","Data Science","B")""")
|
| 24 |
+
cursor.execute("""Insert Into STUDENT values("bala","DevOps","A")""")
|
| 25 |
+
cursor.execute("""Insert Into STUDENT values("praveen","Data Science","B")""")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
#Display the records
|
| 29 |
+
|
| 30 |
+
print("The inerted records are")
|
| 31 |
+
data=cursor.execute("""select * from STUDENT""")
|
| 32 |
+
|
| 33 |
+
for row in data:
|
| 34 |
+
print(row)
|
| 35 |
+
|
| 36 |
+
#commit the changes in db
|
| 37 |
+
connection.commit()
|
| 38 |
+
connection.close()
|
student.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|