Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,79 +5,75 @@ import requests
|
|
| 5 |
import re
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
def
|
| 10 |
-
key = os.
|
| 11 |
-
if key
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
def
|
| 20 |
-
schema = ", ".join([f"{col} ({
|
| 21 |
-
full_prompt = f"""
|
| 22 |
-
You are a SQL expert. Here is a table called 'df' with the following schema:
|
| 23 |
-
{schema}
|
| 24 |
|
| 25 |
User question: "{prompt}"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"""
|
| 29 |
|
| 30 |
url = "https://api.together.xyz/v1/completions"
|
| 31 |
headers = {
|
| 32 |
-
"Authorization": f"Bearer {
|
| 33 |
"Content-Type": "application/json"
|
| 34 |
}
|
| 35 |
payload = {
|
| 36 |
-
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 37 |
"prompt": full_prompt,
|
| 38 |
-
"max_tokens":
|
| 39 |
-
"temperature": 0.
|
| 40 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
result = response.json()
|
| 45 |
-
return result['choices'][0]['text'].strip("```sql").strip("```").strip()
|
| 46 |
-
|
| 47 |
-
# π§½ Clean SQL for DuckDB compatibility
|
| 48 |
-
def clean_sql_for_duckdb(sql, df_columns):
|
| 49 |
sql = sql.replace("`", '"')
|
| 50 |
-
for
|
| 51 |
-
if " " in
|
| 52 |
-
|
| 53 |
-
sql = re.sub(pattern, f'"{col}"', sql)
|
| 54 |
return sql
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
def
|
| 58 |
try:
|
| 59 |
df = pd.read_excel(file)
|
| 60 |
-
sql =
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
# ποΈ Gradio UI
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
-
gr.Markdown("##
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
submit.click(fn=chatbot_interface, inputs=[file_input, question], outputs=[sql_output, result_table])
|
| 80 |
|
| 81 |
-
# π Launch the app
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|
|
|
|
| 5 |
import re
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Load API Key
|
| 9 |
+
def get_api_key():
|
| 10 |
+
key = os.getenv("TOGETHER_API_KEY")
|
| 11 |
+
print("π API KEY:", "FOUND" if key else "NOT FOUND")
|
| 12 |
+
if not key:
|
| 13 |
+
raise RuntimeError("π΄ MISSING TOGETHER_API_KEY!")
|
| 14 |
+
return key
|
| 15 |
|
| 16 |
+
API_KEY = get_api_key()
|
| 17 |
|
| 18 |
+
# Generate SQL with detailed logging
|
| 19 |
+
def generate_sql(prompt, df):
|
| 20 |
+
schema = ", ".join([f"{col} ({dtype})" for col, dtype in df.dtypes.items()])
|
| 21 |
+
full_prompt = f"""You are a SQL expert. Table 'df' schema: {schema}
|
|
|
|
|
|
|
| 22 |
|
| 23 |
User question: "{prompt}"
|
| 24 |
+
Return only valid SQL."""
|
| 25 |
+
print("π’ FULL PROMPT:\n", full_prompt)
|
|
|
|
| 26 |
|
| 27 |
url = "https://api.together.xyz/v1/completions"
|
| 28 |
headers = {
|
| 29 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 30 |
"Content-Type": "application/json"
|
| 31 |
}
|
| 32 |
payload = {
|
| 33 |
+
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 34 |
"prompt": full_prompt,
|
| 35 |
+
"max_tokens": 200,
|
| 36 |
+
"temperature": 0.2
|
| 37 |
}
|
| 38 |
+
print("π‘ Calling Together API:", url, payload)
|
| 39 |
+
resp = requests.post(url, headers=headers, json=payload)
|
| 40 |
+
print("π’ HTTP Status:", resp.status_code)
|
| 41 |
+
print("π§Ύ Response Body:", resp.text)
|
| 42 |
+
resp.raise_for_status()
|
| 43 |
+
j = resp.json()
|
| 44 |
+
if "choices" in j:
|
| 45 |
+
return j["choices"][0]["text"].strip()
|
| 46 |
+
return j.get("output", "")
|
| 47 |
|
| 48 |
+
# Clean SQL
|
| 49 |
+
def clean_sql(sql, cols):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
sql = sql.replace("`", '"')
|
| 51 |
+
for c in cols:
|
| 52 |
+
if " " in c and f'"{c}"' not in sql:
|
| 53 |
+
sql = re.sub(rf'\b{re.escape(c)}\b', f'"{c}"', sql)
|
|
|
|
| 54 |
return sql
|
| 55 |
|
| 56 |
+
# Main function
|
| 57 |
+
def chat_interface(file, q):
|
| 58 |
try:
|
| 59 |
df = pd.read_excel(file)
|
| 60 |
+
sql = generate_sql(q, df)
|
| 61 |
+
sql = clean_sql(sql, df.columns)
|
| 62 |
+
print("β
Final SQL:", sql)
|
| 63 |
+
df_res = duckdb.query(sql).to_df()
|
| 64 |
+
return f"π SQL:\n```sql\n{sql}\n```", df_res
|
| 65 |
except Exception as e:
|
| 66 |
+
print("π₯ EXCEPTION:", repr(e))
|
| 67 |
+
return f"β Error: {e}", pd.DataFrame()
|
| 68 |
|
|
|
|
|
|
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## SQL Chatbot β Debug Mode")
|
| 71 |
+
file_in = gr.File(label="Upload Excel")
|
| 72 |
+
q_in = gr.Textbox(label="Question")
|
| 73 |
+
bt = gr.Button("Run")
|
| 74 |
+
md = gr.Markdown()
|
| 75 |
+
tbl = gr.Dataframe()
|
| 76 |
+
bt.click(chat_interface, [file_in, q_in], [md, tbl])
|
|
|
|
|
|
|
| 77 |
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
demo.launch()
|