Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +9 -8
src/streamlit_app.py
CHANGED
|
@@ -4,14 +4,15 @@ import duckdb
|
|
| 4 |
import requests
|
| 5 |
import re
|
| 6 |
import io
|
|
|
|
| 7 |
|
| 8 |
-
# π
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
TOGETHER_API_KEY = st.text_input("π Enter Together API Key", type="password")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def generate_sql_from_prompt(prompt, df):
|
| 16 |
schema = ", ".join([f"{col} ({str(dtype)})" for col, dtype in df.dtypes.items()])
|
| 17 |
full_prompt = f"""
|
|
@@ -39,7 +40,7 @@ Write a valid SQL query using the 'df' table. Return only the SQL code.
|
|
| 39 |
result = response.json()
|
| 40 |
return result['choices'][0]['message']['content'].strip("```sql").strip("```").strip()
|
| 41 |
|
| 42 |
-
#
|
| 43 |
def clean_sql_for_duckdb(sql, df_columns):
|
| 44 |
sql = sql.replace("`", '"')
|
| 45 |
for col in df_columns:
|
|
@@ -48,10 +49,10 @@ def clean_sql_for_duckdb(sql, df_columns):
|
|
| 48 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 49 |
return sql
|
| 50 |
|
| 51 |
-
# ===
|
| 52 |
st.set_page_config(page_title="π§ Excel SQL Chatbot", layout="centered")
|
| 53 |
st.title("π Excel SQL Chatbot with LLM")
|
| 54 |
-
st.markdown("Upload your **Excel file**, ask a question in natural language, and get results
|
| 55 |
|
| 56 |
uploaded_file = st.file_uploader("π Upload Excel file", type=["xlsx"])
|
| 57 |
|
|
|
|
| 4 |
import requests
|
| 5 |
import re
|
| 6 |
import io
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# === π Safe API key input (streamlit secrets optional) ===
|
| 10 |
+
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY", "") # Preferred for Hugging Face or env deployment
|
| 11 |
+
|
| 12 |
+
if not TOGETHER_API_KEY:
|
| 13 |
TOGETHER_API_KEY = st.text_input("π Enter Together API Key", type="password")
|
| 14 |
|
| 15 |
+
# === SQL Generator Function ===
|
| 16 |
def generate_sql_from_prompt(prompt, df):
|
| 17 |
schema = ", ".join([f"{col} ({str(dtype)})" for col, dtype in df.dtypes.items()])
|
| 18 |
full_prompt = f"""
|
|
|
|
| 40 |
result = response.json()
|
| 41 |
return result['choices'][0]['message']['content'].strip("```sql").strip("```").strip()
|
| 42 |
|
| 43 |
+
# === SQL Cleaner ===
|
| 44 |
def clean_sql_for_duckdb(sql, df_columns):
|
| 45 |
sql = sql.replace("`", '"')
|
| 46 |
for col in df_columns:
|
|
|
|
| 49 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 50 |
return sql
|
| 51 |
|
| 52 |
+
# === UI ===
|
| 53 |
st.set_page_config(page_title="π§ Excel SQL Chatbot", layout="centered")
|
| 54 |
st.title("π Excel SQL Chatbot with LLM")
|
| 55 |
+
st.markdown("Upload your **Excel file**, ask a question in natural language, and get results via generated SQL.")
|
| 56 |
|
| 57 |
uploaded_file = st.file_uploader("π Upload Excel file", type=["xlsx"])
|
| 58 |
|