Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +54 -40
streamlit_app.py
CHANGED
|
@@ -1,64 +1,78 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import os
|
| 4 |
-
from groq import Groq
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
st.set_page_config(page_title="
|
| 8 |
-
st.title("
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
st.error("β GROQ API key not found. Please set it in your Hugging Face `secrets` tab.")
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
client = Groq(api_key=GROQ_API_KEY)
|
| 20 |
-
|
| 21 |
-
# Upload Excel file
|
| 22 |
uploaded_file = st.file_uploader("π Upload your Excel file", type=["xlsx"])
|
| 23 |
-
st.
|
| 24 |
-
|
| 25 |
-
# Text input for user query
|
| 26 |
-
user_query = st.text_input("π¬ Ask a question about your data")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
if st.button("π Generate & Run SQL"):
|
| 30 |
-
if uploaded_file is not None and user_query.strip()
|
| 31 |
try:
|
|
|
|
| 32 |
df = pd.read_excel(uploaded_file)
|
| 33 |
-
st.success("β
File successfully
|
| 34 |
|
| 35 |
-
# Create schema-like string from the DataFrame
|
| 36 |
preview = df.head(5).to_string(index=False)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
st.error(f"β Error processing file: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
+
# Page setup
|
| 7 |
+
st.set_page_config(page_title="π§ Excel SQL Assistant (Together AI)", layout="centered")
|
| 8 |
+
st.title("π Excel to SQL with Together AI + Streamlit")
|
| 9 |
|
| 10 |
+
# API key from Hugging Face Secrets
|
| 11 |
+
TOGETHER_API_KEY = st.secrets.get("TOGETHER_API_KEY", None)
|
| 12 |
|
| 13 |
+
if not TOGETHER_API_KEY:
|
| 14 |
+
st.error("β Together API key not found. Please add it in your Hugging Face Secrets.")
|
|
|
|
| 15 |
st.stop()
|
| 16 |
|
| 17 |
+
# File and input
|
|
|
|
|
|
|
|
|
|
| 18 |
uploaded_file = st.file_uploader("π Upload your Excel file", type=["xlsx"])
|
| 19 |
+
user_query = st.text_input("π¬ Ask a question about your dataset")
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Process on button click
|
| 22 |
if st.button("π Generate & Run SQL"):
|
| 23 |
+
if uploaded_file is not None and user_query.strip():
|
| 24 |
try:
|
| 25 |
+
# Load DataFrame
|
| 26 |
df = pd.read_excel(uploaded_file)
|
| 27 |
+
st.success("β
File loaded successfully")
|
| 28 |
|
|
|
|
| 29 |
preview = df.head(5).to_string(index=False)
|
| 30 |
+
prompt = f"""
|
| 31 |
+
You are an expert data analyst. Given this sample dataset, write a Python pandas code snippet to answer the question.
|
| 32 |
+
|
| 33 |
+
Data preview:
|
| 34 |
+
{preview}
|
| 35 |
|
| 36 |
+
Question:
|
| 37 |
+
{user_query}
|
| 38 |
+
"""
|
| 39 |
|
| 40 |
+
# Make request to Together AI
|
| 41 |
+
with st.spinner("π§ Generating response from Together AI..."):
|
| 42 |
+
headers = {
|
| 43 |
+
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 44 |
+
"Content-Type": "application/json"
|
| 45 |
+
}
|
| 46 |
+
payload = {
|
| 47 |
+
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 48 |
+
"max_tokens": 512,
|
| 49 |
+
"temperature": 0.3,
|
| 50 |
+
"messages": [
|
| 51 |
+
{"role": "system", "content": "You are a helpful AI that writes pandas code from data questions."},
|
| 52 |
+
{"role": "user", "content": prompt}
|
| 53 |
]
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
response = requests.post(
|
| 57 |
+
"https://api.together.xyz/v1/chat/completions",
|
| 58 |
+
headers=headers,
|
| 59 |
+
json=payload
|
| 60 |
)
|
| 61 |
|
| 62 |
+
if response.status_code != 200:
|
| 63 |
+
raise ValueError(f"API error: {response.status_code} - {response.text}")
|
| 64 |
+
|
| 65 |
+
answer = response.json()['choices'][0]['message']['content']
|
| 66 |
+
st.code(answer, language='python')
|
| 67 |
|
| 68 |
+
# Try running the generated code
|
| 69 |
+
try:
|
| 70 |
+
local_vars = {"df": df.copy()}
|
| 71 |
+
exec(answer, {}, local_vars)
|
| 72 |
+
if "df" in local_vars:
|
| 73 |
+
st.dataframe(local_vars["df"])
|
| 74 |
+
except Exception as e:
|
| 75 |
+
st.warning(f"β οΈ Couldn't execute generated code:\n{e}")
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
st.error(f"β Error processing file: {e}")
|