Delete app.py
Browse files
app.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
-
|
| 3 |
-
load_dotenv() # Load All the environment variables
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import os
|
| 6 |
-
import sqlite3
|
| 7 |
-
import google.generativeai as genai
|
| 8 |
-
import pandas as pd
|
| 9 |
-
from custom_table import get_dataframe
|
| 10 |
-
from utils import remove_triple_quotes
|
| 11 |
-
|
| 12 |
-
# configure genai key
|
| 13 |
-
|
| 14 |
-
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 15 |
-
|
| 16 |
-
# Function to load Google Gemini Model and provide query as response
|
| 17 |
-
|
| 18 |
-
def get_gemini_response(question, prompt):
|
| 19 |
-
model=genai.GenerativeModel('gemini-pro')
|
| 20 |
-
response=model.generate_content([prompt[0], question])
|
| 21 |
-
return response.text
|
| 22 |
-
|
| 23 |
-
# function to retrieve query from the database
|
| 24 |
-
|
| 25 |
-
def read_sql_query(sql, db):
|
| 26 |
-
conn = sqlite3.connect(db)
|
| 27 |
-
cur = conn.cursor()
|
| 28 |
-
cur.execute(sql)
|
| 29 |
-
rows = cur.fetchall()
|
| 30 |
-
# Convert results to pandas DataFrame
|
| 31 |
-
sql_output_df = pd.DataFrame(rows, columns=[description[0] for description in cur.description])
|
| 32 |
-
conn.commit()
|
| 33 |
-
conn.close()
|
| 34 |
-
for row in rows:
|
| 35 |
-
print(row)
|
| 36 |
-
return rows, sql_output_df
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
## Define your prompt
|
| 40 |
-
prompt = [
|
| 41 |
-
""" You are an expert in converting English questions to SQL query!
|
| 42 |
-
The SQL database has the name test_db and the SQL table name is tbl_customer
|
| 43 |
-
and has the following columns -
|
| 44 |
-
CustomerKey,GeographyKey,CustomerAlternateKey,Title,FirstName,MiddleName,
|
| 45 |
-
LastName,NameStyle,BirthDate,MaritalStatus,Suffix,Gender,EmailAddress,
|
| 46 |
-
YearlyIncome,TotalChildren,NumberChildrenAtHome,EnglishEducation,
|
| 47 |
-
SpanishEducation,FrenchEducation,EnglishOccupation,SpanishOccupation,
|
| 48 |
-
FrenchOccupation,HouseOwnerFlag,NumberCarsOwned,AddressLine1,AddressLine2,
|
| 49 |
-
Phone,DateFirstPurchase,CommuteDistance.
|
| 50 |
-
|
| 51 |
-
\n\nFor example, \nExample 1 - How many entries of records are present?,
|
| 52 |
-
the SQL command will be something like this SELECT COUNT(*) FROM tbl_customer ;
|
| 53 |
-
|
| 54 |
-
\nExample 2 - Tell me all the customers who has CustomerKey greater than 11000?,
|
| 55 |
-
the SQL command will be something like this SELECT * FROM tbl_customer where CustomerKey > 11000";
|
| 56 |
-
also the sql code should NOT have ``` in beginning or end and sql word in output
|
| 57 |
-
also there should NOT be sql keyword at the beginning of the output """
|
| 58 |
-
]
|
| 59 |
-
|
| 60 |
-
st.set_page_config(page_title="I can retrieve any SQL query")
|
| 61 |
-
st.header("Gemini App to Retrieve SQL data")
|
| 62 |
-
|
| 63 |
-
question = st.text_input("Input: ", key=input)
|
| 64 |
-
submit=st.button("Ask the Question :")
|
| 65 |
-
|
| 66 |
-
# # If Submit is clicked...
|
| 67 |
-
# if submit:
|
| 68 |
-
# generated_sql_query = get_gemini_response(question, prompt)
|
| 69 |
-
# print(generated_sql_query)
|
| 70 |
-
# response = read_sql_query(generated_sql_query, "student.db")
|
| 71 |
-
# st.subheader("The Response is :")
|
| 72 |
-
# for row in response:
|
| 73 |
-
# print(row)
|
| 74 |
-
# st.header(row)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# Display DataFrame in Streamlit
|
| 78 |
-
df = get_dataframe()
|
| 79 |
-
st.write('##### This is the sample table...')
|
| 80 |
-
st.write(df.head(5))
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# If Submit is clicked...
|
| 84 |
-
if submit:
|
| 85 |
-
generated_sql_query = get_gemini_response(question, prompt)
|
| 86 |
-
generated_sql_query = remove_triple_quotes(generated_sql_query)
|
| 87 |
-
print("The generated sql_quey is :")
|
| 88 |
-
print(generated_sql_query)
|
| 89 |
-
response, sql_output_df = read_sql_query(generated_sql_query, "test_db.db")
|
| 90 |
-
st.write('##### used SQL query is:')
|
| 91 |
-
st.write(generated_sql_query)
|
| 92 |
-
st.write("#### The Response is :")
|
| 93 |
-
# Display the DataFrame in Streamlit
|
| 94 |
-
st.write(sql_output_df)
|
| 95 |
-
|
| 96 |
-
# for row in response:
|
| 97 |
-
# print(row)
|
| 98 |
-
|
| 99 |
-
# st.write(row)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|