Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 12 |
-
TOGETHER_API_KEY = os.
|
|
|
|
|
|
|
|
|
|
| 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"""
|
|
@@ -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 |
-
#
|
| 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 |
-
#
|
| 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"
|
| 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 |
-
#
|
| 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()
|