Spaces:
Sleeping
Sleeping
Uploaded Required Files
Browse files- app.py +80 -0
- requirements.txt +3 -0
- student.db +0 -0
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
load_dotenv()
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import sqlite3
|
7 |
+
import google.generativeai as genai
|
8 |
+
|
9 |
+
|
10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
+
model = genai.GenerativeModel('gemini-pro')
|
12 |
+
chat = model.start_chat(history=[])
|
13 |
+
|
14 |
+
# Sending the question and getting response from gemini
|
15 |
+
def get_response(question,prompt):
|
16 |
+
response = chat.send_message([prompt,question])
|
17 |
+
return response.text
|
18 |
+
|
19 |
+
# Initialize session state for chat history if it doesn't exist
|
20 |
+
if 'chat_history' not in st.session_state:
|
21 |
+
st.session_state['chat_history'] = []
|
22 |
+
|
23 |
+
st.title('Text-SQL Bot')
|
24 |
+
st.sidebar.title("Chat History")
|
25 |
+
|
26 |
+
# To retreive query from db
|
27 |
+
def read_sql_query(sql_query,db):
|
28 |
+
connect = sqlite3.connect(db)
|
29 |
+
curs = connect.cursor()
|
30 |
+
curs.execute(sql_query)
|
31 |
+
rows = curs.fetchall()
|
32 |
+
connect.commit()
|
33 |
+
connect.close()
|
34 |
+
for row in rows:
|
35 |
+
print(row)
|
36 |
+
return rows
|
37 |
+
|
38 |
+
# Defining the prompt
|
39 |
+
prompt=[
|
40 |
+
"""
|
41 |
+
You are an expert in converting English questions to SQL query!
|
42 |
+
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
43 |
+
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
|
44 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
45 |
+
\nExample 2 - Tell me all the students studying in Data Science class?,
|
46 |
+
the SQL command will be something like this SELECT * FROM STUDENT
|
47 |
+
where CLASS="Data Science";
|
48 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
49 |
+
"""
|
50 |
+
]
|
51 |
+
|
52 |
+
st.header('Bot to Retrive SQL Data')
|
53 |
+
question = st.text_input("A",key='input_question',label_visibility='hidden')
|
54 |
+
|
55 |
+
submit = st.button('Retrive SQL')
|
56 |
+
|
57 |
+
if submit:
|
58 |
+
response = get_response(question,prompt[0])
|
59 |
+
|
60 |
+
st.session_state['chat_history'].append(("You", question))
|
61 |
+
|
62 |
+
st.subheader('Your SQL Query is ...')
|
63 |
+
print(response)
|
64 |
+
st.write(response)
|
65 |
+
|
66 |
+
db_records = read_sql_query(response, "student.db")
|
67 |
+
print(db_records)
|
68 |
+
|
69 |
+
st.session_state['chat_history'].append(("Bot", db_records))
|
70 |
+
|
71 |
+
st.subheader('The Records are ...')
|
72 |
+
for row in db_records:
|
73 |
+
print(row)
|
74 |
+
st.write(row)
|
75 |
+
|
76 |
+
for role, text in st.session_state['chat_history']:
|
77 |
+
# Limit display to a maximum of 100 words with an ellipsis for longer messages
|
78 |
+
truncated_text = ''.join(str(item) for item in text[:30]) + ('...' if len(text) > 30 else '')
|
79 |
+
st.sidebar.text(f"{role}: {truncated_text}")
|
80 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv #For loading environment variables
|
student.db
ADDED
Binary file (8.19 kB). View file
|
|