Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import requests
|
|
| 5 |
import re
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# ✅
|
| 9 |
def get_together_api_key():
|
| 10 |
key = os.environ.get("TOGETHER_API_KEY")
|
| 11 |
if key:
|
|
@@ -15,33 +15,32 @@ def get_together_api_key():
|
|
| 15 |
|
| 16 |
TOGETHER_API_KEY = get_together_api_key()
|
| 17 |
|
| 18 |
-
# 🧠 Generate SQL from prompt
|
| 19 |
def generate_sql_from_prompt(prompt, df):
|
| 20 |
schema = ", ".join([f"{col} ({str(dtype)})" for col, dtype in df.dtypes.items()])
|
| 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 |
-
Write a valid SQL query using the 'df' table. Return only the SQL code.
|
| 28 |
-
|
| 29 |
-
url = "https://api.together.xyz/
|
| 30 |
headers = {
|
| 31 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 32 |
"Content-Type": "application/json"
|
| 33 |
}
|
| 34 |
payload = {
|
| 35 |
-
"model": "
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
}
|
| 40 |
|
| 41 |
response = requests.post(url, headers=headers, json=payload)
|
| 42 |
response.raise_for_status()
|
| 43 |
result = response.json()
|
| 44 |
-
return result['
|
| 45 |
|
| 46 |
# 🧽 Clean SQL for DuckDB compatibility
|
| 47 |
def clean_sql_for_duckdb(sql, df_columns):
|
|
@@ -52,7 +51,7 @@ def clean_sql_for_duckdb(sql, df_columns):
|
|
| 52 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 53 |
return sql
|
| 54 |
|
| 55 |
-
# 💬 Main
|
| 56 |
def chatbot_interface(file, question):
|
| 57 |
try:
|
| 58 |
df = pd.read_excel(file)
|
|
@@ -77,4 +76,4 @@ with gr.Blocks() as demo:
|
|
| 77 |
|
| 78 |
# 🚀 Launch the app
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
demo.launch()
|
|
|
|
| 5 |
import re
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# ✅ Load Together API key securely
|
| 9 |
def get_together_api_key():
|
| 10 |
key = os.environ.get("TOGETHER_API_KEY")
|
| 11 |
if key:
|
|
|
|
| 15 |
|
| 16 |
TOGETHER_API_KEY = get_together_api_key()
|
| 17 |
|
| 18 |
+
# 🧠 Generate SQL from prompt using Together API
|
| 19 |
def generate_sql_from_prompt(prompt, df):
|
| 20 |
schema = ", ".join([f"{col} ({str(dtype)})" for col, dtype in df.dtypes.items()])
|
| 21 |
+
full_prompt = f"""You are a SQL expert. Here is a table called 'df' with the following schema:
|
|
|
|
| 22 |
{schema}
|
| 23 |
|
| 24 |
User question: "{prompt}"
|
| 25 |
|
| 26 |
+
Write a valid SQL query using the 'df' table. Return only the SQL code."""
|
| 27 |
+
|
| 28 |
+
url = "https://api.together.xyz/inference"
|
| 29 |
headers = {
|
| 30 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 31 |
"Content-Type": "application/json"
|
| 32 |
}
|
| 33 |
payload = {
|
| 34 |
+
"model": "meta-llama/Llama-3-8B-Instruct",
|
| 35 |
+
"prompt": full_prompt,
|
| 36 |
+
"max_tokens": 300,
|
| 37 |
+
"temperature": 0.5,
|
| 38 |
}
|
| 39 |
|
| 40 |
response = requests.post(url, headers=headers, json=payload)
|
| 41 |
response.raise_for_status()
|
| 42 |
result = response.json()
|
| 43 |
+
return result['output'].strip("```sql").strip("```").strip()
|
| 44 |
|
| 45 |
# 🧽 Clean SQL for DuckDB compatibility
|
| 46 |
def clean_sql_for_duckdb(sql, df_columns):
|
|
|
|
| 51 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 52 |
return sql
|
| 53 |
|
| 54 |
+
# 💬 Main Gradio function
|
| 55 |
def chatbot_interface(file, question):
|
| 56 |
try:
|
| 57 |
df = pd.read_excel(file)
|
|
|
|
| 76 |
|
| 77 |
# 🚀 Launch the app
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|