Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +31 -27
streamlit_app.py
CHANGED
|
@@ -1,54 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import requests
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Page setup
|
| 7 |
-
st.set_page_config(page_title="π§ Excel SQL Assistant
|
| 8 |
-
st.title("π Excel to SQL with Together AI
|
| 9 |
|
| 10 |
-
#
|
| 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
|
| 15 |
st.stop()
|
| 16 |
|
| 17 |
-
# File
|
| 18 |
-
uploaded_file = st.file_uploader("
|
|
|
|
|
|
|
| 19 |
user_query = st.text_input("π¬ Ask a question about your dataset")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
if st.button("
|
| 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
|
| 32 |
|
| 33 |
Data preview:
|
| 34 |
{preview}
|
| 35 |
|
| 36 |
Question:
|
| 37 |
{user_query}
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
with st.spinner("π§
|
| 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":
|
| 49 |
"temperature": 0.3,
|
| 50 |
"messages": [
|
| 51 |
-
{"role": "system", "content": "You
|
| 52 |
{"role": "user", "content": prompt}
|
| 53 |
]
|
| 54 |
}
|
|
@@ -60,21 +65,20 @@ Question:
|
|
| 60 |
)
|
| 61 |
|
| 62 |
if response.status_code != 200:
|
| 63 |
-
raise ValueError(f"API
|
| 64 |
|
| 65 |
-
|
| 66 |
-
st.code(
|
| 67 |
|
| 68 |
-
#
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.dataframe(local_vars["df"])
|
| 74 |
except Exception as e:
|
| 75 |
-
st.
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
st.error(f"β
|
| 79 |
else:
|
| 80 |
-
st.warning("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import requests
|
| 4 |
+
from pandasql import sqldf
|
| 5 |
|
| 6 |
+
# π Page setup
|
| 7 |
+
st.set_page_config(page_title="π§ Excel SQL Assistant", layout="centered")
|
| 8 |
+
st.title("π Excel to SQL with Together AI")
|
| 9 |
|
| 10 |
+
# π Load API Key securely
|
| 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 under Secrets on Hugging Face.")
|
| 15 |
st.stop()
|
| 16 |
|
| 17 |
+
# π File upload
|
| 18 |
+
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx"])
|
| 19 |
+
|
| 20 |
+
# π¬ User input
|
| 21 |
user_query = st.text_input("π¬ Ask a question about your dataset")
|
| 22 |
|
| 23 |
+
# π On click
|
| 24 |
+
if st.button("Generate & Run SQL"):
|
| 25 |
if uploaded_file is not None and user_query.strip():
|
| 26 |
try:
|
|
|
|
| 27 |
df = pd.read_excel(uploaded_file)
|
| 28 |
st.success("β
File loaded successfully")
|
| 29 |
+
st.dataframe(df.head(3))
|
| 30 |
|
| 31 |
+
# π Preview for prompt
|
| 32 |
preview = df.head(5).to_string(index=False)
|
| 33 |
prompt = f"""
|
| 34 |
+
You are an AI assistant. Generate an SQL query for a pandas DataFrame named 'df' based on the user's question.
|
| 35 |
|
| 36 |
Data preview:
|
| 37 |
{preview}
|
| 38 |
|
| 39 |
Question:
|
| 40 |
{user_query}
|
| 41 |
+
|
| 42 |
+
Return ONLY the SQL query. Do not include any explanations or Python code.
|
| 43 |
"""
|
| 44 |
|
| 45 |
+
# π Together API request
|
| 46 |
+
with st.spinner("π§ Thinking..."):
|
| 47 |
headers = {
|
| 48 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 49 |
"Content-Type": "application/json"
|
| 50 |
}
|
| 51 |
payload = {
|
| 52 |
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 53 |
+
"max_tokens": 256,
|
| 54 |
"temperature": 0.3,
|
| 55 |
"messages": [
|
| 56 |
+
{"role": "system", "content": "You generate SQL queries from natural language questions."},
|
| 57 |
{"role": "user", "content": prompt}
|
| 58 |
]
|
| 59 |
}
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
if response.status_code != 200:
|
| 68 |
+
raise ValueError(f"API Error: {response.status_code} - {response.text}")
|
| 69 |
|
| 70 |
+
sql_query = response.json()['choices'][0]['message']['content'].strip()
|
| 71 |
+
st.code(sql_query, language='sql')
|
| 72 |
|
| 73 |
+
# π§ͺ Execute SQL
|
| 74 |
try:
|
| 75 |
+
result_df = sqldf(sql_query, {"df": df})
|
| 76 |
+
st.success("β
SQL query executed successfully!")
|
| 77 |
+
st.dataframe(result_df)
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
+
st.error(f"β οΈ Failed to run SQL query:\n{e}")
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
+
st.error(f"β Something went wrong: {e}")
|
| 83 |
else:
|
| 84 |
+
st.warning("π’ Please upload a file and enter your question.")
|