Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +70 -0
- requirements.txt +4 -0
- student_records.db +0 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import streamlit as st
|
3 |
+
import sqlite3
|
4 |
+
from langchain_openai import ChatOpenAI
|
5 |
+
from langchain.prompts import ChatPromptTemplate
|
6 |
+
from langchain.schema.output_parser import StrOutputParser
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# function to retrieve data from the database
|
11 |
+
def read_sql_query(sql, db):
|
12 |
+
print(sql)
|
13 |
+
conn = sqlite3.connect(db)
|
14 |
+
cursor = conn.cursor()
|
15 |
+
rows = cursor.execute(sql)
|
16 |
+
# for row in rows:
|
17 |
+
# print(row)
|
18 |
+
return rows
|
19 |
+
|
20 |
+
model = ChatOpenAI(model='gpt-4o-mini')
|
21 |
+
|
22 |
+
system_prompt = """
|
23 |
+
You are an expert in converting English questions to SQL code!
|
24 |
+
the sql database consists of table 'student' which has columns
|
25 |
+
'name', 'class' and 'section'.
|
26 |
+
|
27 |
+
Example 1:
|
28 |
+
Input: How many entries of records are present?
|
29 |
+
Output: SELECT COUNT(*) FROM student;
|
30 |
+
|
31 |
+
Example 2:
|
32 |
+
Input: List all the students in the frontend class.
|
33 |
+
Output: SELECT * FROM student WHERE class='frontend';
|
34 |
+
|
35 |
+
also, sql code should not have ``` in neither beginning nor end.
|
36 |
+
|
37 |
+
also, if given query cannot be converted to sql, return
|
38 |
+
"Given query cannot be converted to SQL"
|
39 |
+
"""
|
40 |
+
|
41 |
+
prompt_template = ChatPromptTemplate.from_messages(
|
42 |
+
[
|
43 |
+
('system', system_prompt),
|
44 |
+
('human', '{input}')
|
45 |
+
]
|
46 |
+
)
|
47 |
+
|
48 |
+
chain = prompt_template | model | StrOutputParser()
|
49 |
+
|
50 |
+
# Streamlit app
|
51 |
+
st.set_page_config(
|
52 |
+
page_title='I will retrieve any SQL query'
|
53 |
+
)
|
54 |
+
st.header('Retrieve SQL data in plain English')
|
55 |
+
|
56 |
+
question = st.text_input(label='Query the database', placeholder='Enter your query in plain english here')
|
57 |
+
submit = st.button('Query')
|
58 |
+
|
59 |
+
if submit:
|
60 |
+
if question:
|
61 |
+
sql = chain.invoke({'input': question})
|
62 |
+
if sql == 'Given query cannot be converted to SQL':
|
63 |
+
st.subheader('Given query cannot be translated to SQL')
|
64 |
+
else:
|
65 |
+
st.subheader('Generated SQL')
|
66 |
+
st.write(sql)
|
67 |
+
result = read_sql_query(sql, 'student_records.db')
|
68 |
+
st.subheader('Retrieved Data')
|
69 |
+
for row in result:
|
70 |
+
st.write(row)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
python-dotenv
|
3 |
+
langchain_community
|
4 |
+
langchain_openai
|
student_records.db
ADDED
Binary file (8.19 kB). View file
|
|