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,15 +15,17 @@ def get_together_api_key():
|
|
| 15 |
|
| 16 |
TOGETHER_API_KEY = get_together_api_key()
|
| 17 |
|
| 18 |
-
# 🧠 Generate SQL from prompt using Together
|
| 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 |
{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 = {
|
|
@@ -34,7 +36,7 @@ Write a valid SQL query using the 'df' table. Return only the SQL code."""
|
|
| 34 |
"model": "meta-llama/Llama-3-8B-Instruct",
|
| 35 |
"prompt": full_prompt,
|
| 36 |
"max_tokens": 300,
|
| 37 |
-
"temperature": 0.
|
| 38 |
}
|
| 39 |
|
| 40 |
response = requests.post(url, headers=headers, json=payload)
|
|
@@ -51,7 +53,7 @@ def clean_sql_for_duckdb(sql, df_columns):
|
|
| 51 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 52 |
return sql
|
| 53 |
|
| 54 |
-
# 💬 Main
|
| 55 |
def chatbot_interface(file, question):
|
| 56 |
try:
|
| 57 |
df = pd.read_excel(file)
|
|
|
|
| 5 |
import re
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# ✅ Securely load Together API Key
|
| 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's /inference endpoint
|
| 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 |
|
| 30 |
url = "https://api.together.xyz/inference"
|
| 31 |
headers = {
|
|
|
|
| 36 |
"model": "meta-llama/Llama-3-8B-Instruct",
|
| 37 |
"prompt": full_prompt,
|
| 38 |
"max_tokens": 300,
|
| 39 |
+
"temperature": 0.7,
|
| 40 |
}
|
| 41 |
|
| 42 |
response = requests.post(url, headers=headers, json=payload)
|
|
|
|
| 53 |
sql = re.sub(pattern, f'"{col}"', sql)
|
| 54 |
return sql
|
| 55 |
|
| 56 |
+
# 💬 Main chatbot function
|
| 57 |
def chatbot_interface(file, question):
|
| 58 |
try:
|
| 59 |
df = pd.read_excel(file)
|