AliInamdar commited on
Commit
a824eae
Β·
verified Β·
1 Parent(s): 07f734b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # app.py
2
-
3
  import gradio as gr
4
  import pandas as pd
5
  import duckdb
@@ -8,10 +6,13 @@ import re
8
  import io
9
  import os
10
 
11
- # Load Together API Key from environment variable or fallback
12
- TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY", "tgp_v1_QxHjcs582Y4kSGPd5a2VyrKDD6S81ctp-M-rT_ioDNE") # Replace with HF Secret
 
 
 
13
 
14
- # Function to generate SQL from a prompt
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"""
@@ -40,7 +41,7 @@ Write a valid SQL query using the 'df' table. Return only the SQL code.
40
  result = response.json()
41
  return result['choices'][0]['message']['content'].strip("```sql").strip("```").strip()
42
 
43
- # Function to clean SQL for DuckDB
44
  def clean_sql_for_duckdb(sql, df_columns):
45
  sql = sql.replace("`", '"')
46
  for col in df_columns:
@@ -49,7 +50,7 @@ def clean_sql_for_duckdb(sql, df_columns):
49
  sql = re.sub(pattern, f'"{col}"', sql)
50
  return sql
51
 
52
- # Combined Gradio function
53
  def chatbot_interface(file, question):
54
  try:
55
  df = pd.read_excel(file)
@@ -60,18 +61,16 @@ def chatbot_interface(file, question):
60
  except Exception as e:
61
  return f"❌ Error: {str(e)}", pd.DataFrame()
62
 
63
- # Gradio UI
64
  with gr.Blocks() as demo:
65
  gr.Markdown("## πŸ“Š Excel SQL Chatbot with Together API")
66
  file_input = gr.File(label="πŸ“‚ Upload Excel File (.xlsx)")
67
- question = gr.Textbox(label="🧠 Ask a question about your data", placeholder="e.g., Show me the average sales by region")
68
  submit = gr.Button("πŸš€ Generate & Query")
69
-
70
  sql_output = gr.Markdown()
71
  result_table = gr.Dataframe()
72
-
73
  submit.click(fn=chatbot_interface, inputs=[file_input, question], outputs=[sql_output, result_table])
74
 
75
- # Launch app
76
  if __name__ == "__main__":
77
  demo.launch()
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import duckdb
 
6
  import io
7
  import os
8
 
9
+ # βœ… Read API Key
10
+ TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY")
11
+
12
+ if not TOGETHER_API_KEY:
13
+ raise RuntimeError("❌ TOGETHER_API_KEY not found. Please set it in Hugging Face Secrets tab.")
14
 
15
+ # πŸ” SQL generation
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"""
 
41
  result = response.json()
42
  return result['choices'][0]['message']['content'].strip("```sql").strip("```").strip()
43
 
44
+ # 🧽 SQL cleanup
45
  def clean_sql_for_duckdb(sql, df_columns):
46
  sql = sql.replace("`", '"')
47
  for col in df_columns:
 
50
  sql = re.sub(pattern, f'"{col}"', sql)
51
  return sql
52
 
53
+ # πŸ”„ Main Gradio function
54
  def chatbot_interface(file, question):
55
  try:
56
  df = pd.read_excel(file)
 
61
  except Exception as e:
62
  return f"❌ Error: {str(e)}", pd.DataFrame()
63
 
64
+ # 🧱 Gradio UI
65
  with gr.Blocks() as demo:
66
  gr.Markdown("## πŸ“Š Excel SQL Chatbot with Together API")
67
  file_input = gr.File(label="πŸ“‚ Upload Excel File (.xlsx)")
68
+ question = gr.Textbox(label="🧠 Ask a question about your data")
69
  submit = gr.Button("πŸš€ Generate & Query")
 
70
  sql_output = gr.Markdown()
71
  result_table = gr.Dataframe()
 
72
  submit.click(fn=chatbot_interface, inputs=[file_input, question], outputs=[sql_output, result_table])
73
 
74
+ # ▢️ Run the app
75
  if __name__ == "__main__":
76
  demo.launch()