Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +70 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,76 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import duckdb
|
| 4 |
+
import requests
|
| 5 |
+
import re
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
st.set_page_config(page_title="π§ SQL Chatbot with Groq", layout="centered")
|
| 9 |
+
|
| 10 |
+
# π Load Groq API Key from Hugging Face secret or env var
|
| 11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 12 |
+
|
| 13 |
+
if not GROQ_API_KEY:
|
| 14 |
+
st.error("β GROQ_API_KEY not found. Please set it in Streamlit Secrets or environment.")
|
| 15 |
+
st.stop()
|
| 16 |
+
|
| 17 |
+
# π§ Generate SQL using Groq API
|
| 18 |
+
def generate_sql(prompt, df):
|
| 19 |
+
schema = ", ".join([f"{col} ({dtype})" for col, dtype in df.dtypes.items()])
|
| 20 |
+
full_prompt = f"""
|
| 21 |
+
You are a SQL expert. The table 'df' has the following columns and types:
|
| 22 |
+
{schema}
|
| 23 |
|
| 24 |
+
User question: "{prompt}"
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
Write a valid SQL query using the 'df' table. Return only the SQL.
|
| 27 |
"""
|
| 28 |
|
| 29 |
+
headers = {
|
| 30 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 31 |
+
"Content-Type": "application/json"
|
| 32 |
+
}
|
| 33 |
+
payload = {
|
| 34 |
+
"model": "llama3-70b-8192",
|
| 35 |
+
"messages": [{"role": "user", "content": full_prompt}],
|
| 36 |
+
"temperature": 0.2,
|
| 37 |
+
"max_tokens": 300
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 41 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
result = response.json()
|
| 44 |
+
return result["choices"][0]["message"]["content"].strip()
|
| 45 |
+
|
| 46 |
+
# π§½ Clean SQL
|
| 47 |
+
def clean_sql(sql, df_columns):
|
| 48 |
+
sql = sql.replace("`", '"')
|
| 49 |
+
for col in df_columns:
|
| 50 |
+
if " " in col and f'"{col}"' not in sql:
|
| 51 |
+
pattern = r'\b' + re.escape(col) + r'\b'
|
| 52 |
+
sql = re.sub(pattern, f'"{col}"', sql)
|
| 53 |
+
return sql
|
| 54 |
+
|
| 55 |
+
# π₯οΈ UI
|
| 56 |
+
st.title("π Excel SQL Chatbot (Groq + Streamlit)")
|
| 57 |
+
uploaded_file = st.file_uploader("π Upload your Excel file", type=["xlsx"])
|
| 58 |
+
user_prompt = st.text_input("π§ Ask a question about your data")
|
| 59 |
+
|
| 60 |
+
if st.button("π Generate & Run SQL"):
|
| 61 |
+
if uploaded_file and user_prompt:
|
| 62 |
+
try:
|
| 63 |
+
df = pd.read_excel(uploaded_file)
|
| 64 |
+
sql = generate_sql(user_prompt, df)
|
| 65 |
+
cleaned_sql = clean_sql(sql, df.columns)
|
| 66 |
+
result = duckdb.query(cleaned_sql).to_df()
|
| 67 |
+
|
| 68 |
+
st.markdown(f"### π§Ύ Generated SQL")
|
| 69 |
+
st.code(sql, language="sql")
|
| 70 |
+
st.markdown("### π Query Results")
|
| 71 |
+
st.dataframe(result)
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.error(f"β Error: {str(e)}")
|
| 75 |
+
else:
|
| 76 |
+
st.warning("β οΈ Please upload a file and enter a question.")
|