Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +89 -0
- requirements.txt +3 -0
- student.db +0 -0
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv()#take environment variables from .env.
|
| 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 |
+
|
| 13 |
+
genai.configure(api_key=os.getenv("GENAI_API_KEY"))
|
| 14 |
+
|
| 15 |
+
#Function to load Google Gemini Model
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_gemini_Response(question,promopt):
|
| 19 |
+
model=genai.GenerativeModel("gemini-pro")
|
| 20 |
+
response=model.generate_content([promopt[0],question])
|
| 21 |
+
return response.text
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
## function to retrive query form the db
|
| 25 |
+
|
| 26 |
+
def read_sql_query(sql,db):
|
| 27 |
+
conn=sqlite3.connect(db)
|
| 28 |
+
cur=conn.cursor()
|
| 29 |
+
cur.execute(sql)
|
| 30 |
+
rows=cur.fetchall()
|
| 31 |
+
conn.commit()
|
| 32 |
+
conn.close()
|
| 33 |
+
for row in rows:
|
| 34 |
+
print(row)
|
| 35 |
+
st.write(row)
|
| 36 |
+
return rows
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
##Define Your Prompt
|
| 41 |
+
|
| 42 |
+
prompt=[
|
| 43 |
+
"""
|
| 44 |
+
You are an expert in a converting english questions to SQL query !
|
| 45 |
+
The SQL database has the name STUDENT and has the following columns - NAME,CLASS,SECTION
|
| 46 |
+
\n\n
|
| 47 |
+
|
| 48 |
+
Example 1- How many entries of reacords are there in the table STUDENT ?,
|
| 49 |
+
The sql command will be something like SELECT COUNT(*) FROM STUDENT;
|
| 50 |
+
\n
|
| 51 |
+
Example 2-tell me all the Students studying in Data Science class ?,
|
| 52 |
+
The sql command will be something like SELECT * FROM STUDENT where CLASS='Data Science';
|
| 53 |
+
|
| 54 |
+
also the sql code should not have ``` in the beginning and end and sql word in output
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
##Streamlit APplication
|
| 63 |
+
|
| 64 |
+
st.set_page_config(page_title="SQL Query Generator",page_icon="🔍",layout="wide")
|
| 65 |
+
|
| 66 |
+
st.header("Gemini App to return SQL data from the database")
|
| 67 |
+
|
| 68 |
+
question=st.text_input("Input: ",key="input")
|
| 69 |
+
|
| 70 |
+
submit=st.button("Ask the question")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
#if submit is cliked
|
| 74 |
+
|
| 75 |
+
if submit:
|
| 76 |
+
response = get_gemini_Response(question, prompt)
|
| 77 |
+
print(response)
|
| 78 |
+
data = read_sql_query(response, "student.db")
|
| 79 |
+
print(data)
|
| 80 |
+
st.subheader("Response is : ")
|
| 81 |
+
for row in data:
|
| 82 |
+
print(row)
|
| 83 |
+
st.header(row)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
python-dotenv
|
student.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|