AliInamdar commited on
Commit
1a983bf
Β·
verified Β·
1 Parent(s): 1da6fc2

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # πŸ” Handle Together API Key (from secrets or user input)
9
- if "TOGETHER_API_KEY" in st.secrets:
10
- TOGETHER_API_KEY = st.secrets["TOGETHER_API_KEY"]
11
- else:
12
  TOGETHER_API_KEY = st.text_input("πŸ” Enter Together API Key", type="password")
13
 
14
- # 🧠 Generate SQL using Together API
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
- # 🧽 Clean SQL for DuckDB
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
- # === Streamlit UI ===
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 from SQL queries generated by an LLM.")
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