Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,11 @@ import re
|
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
# β
|
| 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.
|
| 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"""
|
|
@@ -23,7 +21,6 @@ User question: "{prompt}"
|
|
| 23 |
|
| 24 |
Write a valid SQL query using the 'df' table. Return only the SQL code.
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
url = "https://api.together.xyz/v1/chat/completions"
|
| 28 |
headers = {
|
| 29 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
|
@@ -41,7 +38,6 @@ Write a valid SQL query using the 'df' table. Return only the SQL code.
|
|
| 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,7 +46,6 @@ def clean_sql_for_duckdb(sql, 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,7 +56,6 @@ def chatbot_interface(file, question):
|
|
| 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)")
|
|
@@ -71,6 +65,5 @@ with gr.Blocks() as demo:
|
|
| 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()
|
|
|
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# β
READ API KEY from Hugging Face Secret
|
| 10 |
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY")
|
|
|
|
| 11 |
if not TOGETHER_API_KEY:
|
| 12 |
+
raise RuntimeError("β TOGETHER_API_KEY not found. Set it in Hugging Face > Settings > Secrets.")
|
| 13 |
|
|
|
|
| 14 |
def generate_sql_from_prompt(prompt, df):
|
| 15 |
schema = ", ".join([f"{col} ({str(dtype)})" for col, dtype in df.dtypes.items()])
|
| 16 |
full_prompt = f"""
|
|
|
|
| 21 |
|
| 22 |
Write a valid SQL query using the 'df' table. Return only the SQL code.
|
| 23 |
"""
|
|
|
|
| 24 |
url = "https://api.together.xyz/v1/chat/completions"
|
| 25 |
headers = {
|
| 26 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
|
|
|
| 38 |
result = response.json()
|
| 39 |
return result['choices'][0]['message']['content'].strip("```sql").strip("```").strip()
|
| 40 |
|
|
|
|
| 41 |
def clean_sql_for_duckdb(sql, df_columns):
|
| 42 |
sql = sql.replace("`", '"')
|
| 43 |
for col in df_columns:
|
|
|
|
| 46 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 47 |
return sql
|
| 48 |
|
|
|
|
| 49 |
def chatbot_interface(file, question):
|
| 50 |
try:
|
| 51 |
df = pd.read_excel(file)
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
return f"β Error: {str(e)}", pd.DataFrame()
|
| 58 |
|
|
|
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
gr.Markdown("## π Excel SQL Chatbot with Together API")
|
| 61 |
file_input = gr.File(label="π Upload Excel File (.xlsx)")
|
|
|
|
| 65 |
result_table = gr.Dataframe()
|
| 66 |
submit.click(fn=chatbot_interface, inputs=[file_input, question], outputs=[sql_output, result_table])
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|