Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,26 +10,27 @@ 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) 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:"
|
|
|
10 |
OPENROUTER_API_KEY = "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"
|
11 |
OPENROUTER_MODEL = "sophosympatheia/rogue-rose-103b-v0.2:free"
|
12 |
|
13 |
+
# Path to the SQLite Database
|
14 |
DB_PATH = "ecommerce.db"
|
15 |
|
16 |
+
# Ensure database exists before proceeding
|
17 |
if not os.path.exists(DB_PATH):
|
18 |
+
raise FileNotFoundError("Database file 'ecommerce.db' not found. Upload it first!")
|
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 with SQLite-friendly queries
|
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) AS total_sales 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 |
+
{"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 SQLite-compatible SQL:\n\n"
|
34 |
for example in few_shot_examples:
|
35 |
prompt += f"Input: {example['input']}\nOutput: {example['output']}\n\n"
|
36 |
prompt += f"Input: {query}\nOutput:"
|