Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,27 +10,26 @@ import os
|
|
| 10 |
OPENROUTER_API_KEY = "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"
|
| 11 |
OPENROUTER_MODEL = "sophosympatheia/rogue-rose-103b-v0.2:free"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
DB_PATH = "ecommerce.db"
|
| 15 |
|
| 16 |
-
# Ensure
|
| 17 |
if not os.path.exists(DB_PATH):
|
| 18 |
-
|
| 19 |
|
| 20 |
# Initialize OpenAI client
|
| 21 |
openai_client = openai.OpenAI(api_key=OPENROUTER_API_KEY, base_url="https://openrouter.ai/api/v1")
|
| 22 |
|
| 23 |
-
# Few-shot examples
|
| 24 |
few_shot_examples = [
|
| 25 |
{"input": "Show all customers from São Paulo.", "output": "SELECT * FROM customers WHERE customer_state = 'SP';"},
|
| 26 |
-
{"input": "Find the total sales per product.", "output": "SELECT product_id, SUM(price)
|
| 27 |
-
{"input": "List all orders placed in 2017.", "output": "SELECT * FROM orders WHERE order_purchase_timestamp LIKE '2017%';"}
|
| 28 |
-
{"input": "Find the busiest months for orders.", "output": "SELECT strftime('%m', order_purchase_timestamp) AS order_month, COUNT(*) AS orders_count FROM orders GROUP BY order_month ORDER BY orders_count DESC;"},
|
| 29 |
]
|
| 30 |
|
| 31 |
# Function: Convert text to SQL
|
| 32 |
def text_to_sql(query):
|
| 33 |
-
prompt = "Convert the following queries into
|
| 34 |
for example in few_shot_examples:
|
| 35 |
prompt += f"Input: {example['input']}\nOutput: {example['output']}\n\n"
|
| 36 |
prompt += f"Input: {query}\nOutput:"
|
|
@@ -40,13 +39,18 @@ def text_to_sql(query):
|
|
| 40 |
model=OPENROUTER_MODEL,
|
| 41 |
messages=[{"role": "system", "content": "You are an SQL expert."}, {"role": "user", "content": prompt}]
|
| 42 |
)
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
return f"Error: {e}"
|
| 46 |
|
|
|
|
| 47 |
def execute_sql(sql_query):
|
| 48 |
try:
|
| 49 |
sql_query = sql_query.strip().rstrip(";") # Remove trailing semicolons
|
|
|
|
| 50 |
conn = sqlite3.connect(DB_PATH)
|
| 51 |
df = pd.read_sql_query(sql_query, conn)
|
| 52 |
conn.close()
|
|
@@ -54,7 +58,6 @@ def execute_sql(sql_query):
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"SQL Execution Error: {e}"
|
| 56 |
|
| 57 |
-
|
| 58 |
# Function: Generate Dynamic Visualization
|
| 59 |
def visualize_data(df):
|
| 60 |
if df.empty or df.shape[1] < 2:
|
|
@@ -96,8 +99,8 @@ def gradio_ui(query):
|
|
| 96 |
return sql_query, results.to_string(index=False) if isinstance(results, pd.DataFrame) else results, visualization
|
| 97 |
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
-
gr.Markdown("## SQL Explorer: Text
|
| 100 |
-
query_input = gr.Textbox(label="Enter your query", placeholder="
|
| 101 |
submit_btn = gr.Button("Convert & Execute")
|
| 102 |
sql_output = gr.Textbox(label="Generated SQL Query")
|
| 103 |
table_output = gr.Textbox(label="Query Results")
|
|
|
|
| 10 |
OPENROUTER_API_KEY = "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"
|
| 11 |
OPENROUTER_MODEL = "sophosympatheia/rogue-rose-103b-v0.2:free"
|
| 12 |
|
| 13 |
+
# Hugging Face Space path
|
| 14 |
DB_PATH = "ecommerce.db"
|
| 15 |
|
| 16 |
+
# Ensure dataset exists
|
| 17 |
if not os.path.exists(DB_PATH):
|
| 18 |
+
os.system("wget https://your-dataset-link.com/ecommerce.db -O ecommerce.db") # Replace with actual dataset link
|
| 19 |
|
| 20 |
# Initialize OpenAI client
|
| 21 |
openai_client = openai.OpenAI(api_key=OPENROUTER_API_KEY, base_url="https://openrouter.ai/api/v1")
|
| 22 |
|
| 23 |
+
# Few-shot examples for text-to-SQL
|
| 24 |
few_shot_examples = [
|
| 25 |
{"input": "Show all customers from São Paulo.", "output": "SELECT * FROM customers WHERE customer_state = 'SP';"},
|
| 26 |
+
{"input": "Find the total sales per product.", "output": "SELECT product_id, SUM(price) FROM order_items GROUP BY product_id;"},
|
| 27 |
+
{"input": "List all orders placed in 2017.", "output": "SELECT * FROM orders WHERE order_purchase_timestamp LIKE '2017%';"}
|
|
|
|
| 28 |
]
|
| 29 |
|
| 30 |
# Function: Convert text to SQL
|
| 31 |
def text_to_sql(query):
|
| 32 |
+
prompt = "Convert the following queries into SQL:\n\n"
|
| 33 |
for example in few_shot_examples:
|
| 34 |
prompt += f"Input: {example['input']}\nOutput: {example['output']}\n\n"
|
| 35 |
prompt += f"Input: {query}\nOutput:"
|
|
|
|
| 39 |
model=OPENROUTER_MODEL,
|
| 40 |
messages=[{"role": "system", "content": "You are an SQL expert."}, {"role": "user", "content": prompt}]
|
| 41 |
)
|
| 42 |
+
sql_query = response.choices[0].message.content.strip()
|
| 43 |
+
sql_query = sql_query.split("\n")[0] # Take only the first line if multiple lines exist
|
| 44 |
+
sql_query = sql_query.replace("mathchar", "").rstrip(";") # Remove unwanted text
|
| 45 |
+
return sql_query
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error: {e}"
|
| 48 |
|
| 49 |
+
# Function: Execute SQL on SQLite database
|
| 50 |
def execute_sql(sql_query):
|
| 51 |
try:
|
| 52 |
sql_query = sql_query.strip().rstrip(";") # Remove trailing semicolons
|
| 53 |
+
sql_query = sql_query.replace("mathchar", "") # Remove any bad tokens
|
| 54 |
conn = sqlite3.connect(DB_PATH)
|
| 55 |
df = pd.read_sql_query(sql_query, conn)
|
| 56 |
conn.close()
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"SQL Execution Error: {e}"
|
| 60 |
|
|
|
|
| 61 |
# Function: Generate Dynamic Visualization
|
| 62 |
def visualize_data(df):
|
| 63 |
if df.empty or df.shape[1] < 2:
|
|
|
|
| 99 |
return sql_query, results.to_string(index=False) if isinstance(results, pd.DataFrame) else results, visualization
|
| 100 |
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
+
gr.Markdown("## SQL Explorer: Text to SQL with a Simple Visualization")
|
| 103 |
+
query_input = gr.Textbox(label="Enter your query", placeholder="Enter your query in English.")
|
| 104 |
submit_btn = gr.Button("Convert & Execute")
|
| 105 |
sql_output = gr.Textbox(label="Generated SQL Query")
|
| 106 |
table_output = gr.Textbox(label="Query Results")
|