Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import find_dotenv, load_dotenv
|
2 |
+
load_dotenv(find_dotenv())
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
import streamlit as st
|
6 |
+
from langchain_core.prompts import PromptTemplate
|
7 |
+
from pandasql import sqldf
|
8 |
+
from groq import Groq
|
9 |
+
|
10 |
+
|
11 |
+
template = """You are a powerful text-to-SQL model. Your job is to answer questions about a database. You are given a question and context regarding one or more tables. Dont add \n characters.
|
12 |
+
Do not include "SELECT short\_name, long\_name" this type of queries which have backslash in them.
|
13 |
+
You must output the SQL query that answers the question in a single line.
|
14 |
+
|
15 |
+
### Input:
|
16 |
+
`{question}`
|
17 |
+
|
18 |
+
### Context:
|
19 |
+
`{context}`
|
20 |
+
|
21 |
+
### Response:
|
22 |
+
"""
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
prompt = PromptTemplate.from_template(template=template)
|
27 |
+
|
28 |
+
client = Groq(
|
29 |
+
api_key=os.getenv("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"),
|
30 |
+
)
|
31 |
+
|
32 |
+
def groq_infer(prompt):
|
33 |
+
chat_completion = client.chat.completions.create(
|
34 |
+
messages=[
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": prompt,
|
38 |
+
}
|
39 |
+
],
|
40 |
+
model="mixtral-8x7b-32768",
|
41 |
+
)
|
42 |
+
print(chat_completion.choices[0].message.content)
|
43 |
+
return chat_completion.choices[0].message.content
|
44 |
+
|
45 |
+
# 1. Create cache_resource - To load the model
|
46 |
+
# infer - pipeline -> pipe()
|
47 |
+
def main():
|
48 |
+
st.set_page_config(page_title="Ask anything about database", page_icon="📊", layout="wide")
|
49 |
+
st.title("SQL Engineer")
|
50 |
+
|
51 |
+
col1, col2 = st.columns([2, 3])
|
52 |
+
|
53 |
+
with col1:
|
54 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
|
55 |
+
if uploaded_file is not None:
|
56 |
+
df = pd.read_csv(uploaded_file, encoding="latin1")
|
57 |
+
df.columns = df.columns.str.replace(r"[^a-zA-Z0-9_]", "", regex=True)
|
58 |
+
st.write("Here's a preview of your uploaded file:")
|
59 |
+
st.dataframe(df)
|
60 |
+
|
61 |
+
context = pd.io.sql.get_schema(df.reset_index(), "df").replace('"', "")
|
62 |
+
st.write("SQL Schema:")
|
63 |
+
st.code(context)
|
64 |
+
|
65 |
+
with col2:
|
66 |
+
if uploaded_file is not None:
|
67 |
+
question = st.text_input("Write a question about the data", key="question")
|
68 |
+
|
69 |
+
if st.button("Get Answer", key="get_answer"):
|
70 |
+
if question:
|
71 |
+
attempt = 0
|
72 |
+
max_attempts = 5
|
73 |
+
while attempt < max_attempts:
|
74 |
+
try:
|
75 |
+
input = {"context": context, "question": question}
|
76 |
+
formatted_prompt = prompt.invoke(input=input).text
|
77 |
+
response = groq_infer(formatted_prompt)
|
78 |
+
final = response.replace("`", "").replace("sql", "").strip()
|
79 |
+
st.text("Query performed")
|
80 |
+
st.code(final)
|
81 |
+
result = sqldf(final, locals())
|
82 |
+
st.write("Answer:")
|
83 |
+
st.dataframe(result)
|
84 |
+
break
|
85 |
+
except Exception as e:
|
86 |
+
attempt += 1
|
87 |
+
st.error(
|
88 |
+
f"Attempt {attempt}/{max_attempts} failed. Retrying..."
|
89 |
+
)
|
90 |
+
if attempt == max_attempts:
|
91 |
+
st.error(
|
92 |
+
"Unable to get the correct query, refresh app or try again later."
|
93 |
+
)
|
94 |
+
continue
|
95 |
+
|
96 |
+
else:
|
97 |
+
st.warning("Please enter a question before clicking 'Get Answer'.")
|
98 |
+
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
main()
|