Spaces:
Sleeping
Sleeping
Commit
·
c420a86
1
Parent(s):
64c4209
Test
Browse files
app.py
CHANGED
|
@@ -6,7 +6,8 @@ from huggingface_hub import InferenceClient
|
|
| 6 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 7 |
"""
|
| 8 |
hf_token = os.getenv("user_token")
|
| 9 |
-
client = InferenceClient("Qwen/Qwen2.5-Coder-3B-Instruct", token=hf_token)
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def respond(
|
|
@@ -17,17 +18,67 @@ def respond(
|
|
| 17 |
temperature,
|
| 18 |
top_p,
|
| 19 |
):
|
| 20 |
-
|
| 21 |
-
### Instructions:
|
| 22 |
-
Your task is to convert a question into a SQL query, given a Postgres database schema.
|
| 23 |
-
Adhere to these rules:
|
| 24 |
-
- **Deliberately go through the question and database schema word by word** to appropriately answer the question
|
| 25 |
-
- **Use Table Aliases** to prevent ambiguity. For example, `SELECT table1.col1, table2.col1 FROM table1 JOIN table2 ON table1.id = table2.id`.
|
| 26 |
-
- When creating a ratio, always cast the numerator as float
|
| 27 |
-
|
| 28 |
-
### Input:
|
| 29 |
-
Generate a SQL query that answers the question `{question}`.
|
| 30 |
-
This query will run on a database whose schema is represented in this string:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
CREATE TABLE products (
|
| 32 |
product_id INTEGER PRIMARY KEY, -- Unique ID for each product
|
| 33 |
name VARCHAR(50), -- Name of the product
|
|
@@ -66,13 +117,12 @@ CREATE TABLE product_suppliers (
|
|
| 66 |
-- sales.customer_id can be joined with customers.customer_id
|
| 67 |
-- sales.salesperson_id can be joined with salespeople.salesperson_id
|
| 68 |
-- product_suppliers.product_id can be joined with products.product_id
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
Based on your instructions, here is the SQL query I have generated to answer the question `{question}`:
|
| 72 |
```sql
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
-
messages = [{"role": "system", "content": sytems}]
|
| 76 |
|
| 77 |
for val in history:
|
| 78 |
if val[0]:
|
|
|
|
| 6 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 7 |
"""
|
| 8 |
hf_token = os.getenv("user_token")
|
| 9 |
+
# client = InferenceClient("Qwen/Qwen2.5-Coder-3B-Instruct", token=hf_token)
|
| 10 |
+
client = InferenceClient("defog/llama-3-sqlcoder-8b", token=hf_token)
|
| 11 |
|
| 12 |
|
| 13 |
def respond(
|
|
|
|
| 18 |
temperature,
|
| 19 |
top_p,
|
| 20 |
):
|
| 21 |
+
# sytems = """
|
| 22 |
+
# ### Instructions:
|
| 23 |
+
# Your task is to convert a question into a SQL query, given a Postgres database schema.
|
| 24 |
+
# Adhere to these rules:
|
| 25 |
+
# - **Deliberately go through the question and database schema word by word** to appropriately answer the question
|
| 26 |
+
# - **Use Table Aliases** to prevent ambiguity. For example, `SELECT table1.col1, table2.col1 FROM table1 JOIN table2 ON table1.id = table2.id`.
|
| 27 |
+
# - When creating a ratio, always cast the numerator as float
|
| 28 |
+
|
| 29 |
+
# ### Input:
|
| 30 |
+
# Generate a SQL query that answers the question `{question}`.
|
| 31 |
+
# This query will run on a database whose schema is represented in this string:
|
| 32 |
+
# CREATE TABLE products (
|
| 33 |
+
# product_id INTEGER PRIMARY KEY, -- Unique ID for each product
|
| 34 |
+
# name VARCHAR(50), -- Name of the product
|
| 35 |
+
# price DECIMAL(10,2), -- Price of each unit of the product
|
| 36 |
+
# quantity INTEGER -- Current quantity in stock
|
| 37 |
+
# );
|
| 38 |
+
|
| 39 |
+
# CREATE TABLE customers (
|
| 40 |
+
# customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer
|
| 41 |
+
# name VARCHAR(50), -- Name of the customer
|
| 42 |
+
# address VARCHAR(100) -- Mailing address of the customer
|
| 43 |
+
# );
|
| 44 |
+
|
| 45 |
+
# CREATE TABLE salespeople (
|
| 46 |
+
# salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson
|
| 47 |
+
# name VARCHAR(50), -- Name of the salesperson
|
| 48 |
+
# region VARCHAR(50) -- Geographic sales region
|
| 49 |
+
# );
|
| 50 |
+
|
| 51 |
+
# CREATE TABLE sales (
|
| 52 |
+
# sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale
|
| 53 |
+
# product_id INTEGER, -- ID of product sold
|
| 54 |
+
# customer_id INTEGER, -- ID of customer who made purchase
|
| 55 |
+
# salesperson_id INTEGER, -- ID of salesperson who made the sale
|
| 56 |
+
# sale_date DATE, -- Date the sale occurred
|
| 57 |
+
# quantity INTEGER -- Quantity of product sold
|
| 58 |
+
# );
|
| 59 |
+
|
| 60 |
+
# CREATE TABLE product_suppliers (
|
| 61 |
+
# supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier
|
| 62 |
+
# product_id INTEGER, -- Product ID supplied
|
| 63 |
+
# supply_price DECIMAL(10,2) -- Unit price charged by supplier
|
| 64 |
+
# );
|
| 65 |
+
|
| 66 |
+
# -- sales.product_id can be joined with products.product_id
|
| 67 |
+
# -- sales.customer_id can be joined with customers.customer_id
|
| 68 |
+
# -- sales.salesperson_id can be joined with salespeople.salesperson_id
|
| 69 |
+
# -- product_suppliers.product_id can be joined with products.product_id
|
| 70 |
+
|
| 71 |
+
# ### Response:
|
| 72 |
+
# Based on your instructions, here is the SQL query I have generated to answer the question `{question}`:
|
| 73 |
+
# ```sql
|
| 74 |
+
# """
|
| 75 |
+
|
| 76 |
+
system2= """
|
| 77 |
+
<|begin_of_text|><|start_header_id|>user<|end_header_id|>
|
| 78 |
+
|
| 79 |
+
Generate a SQL query to answer this question: `{question}`
|
| 80 |
+
|
| 81 |
+
DDL statements:
|
| 82 |
CREATE TABLE products (
|
| 83 |
product_id INTEGER PRIMARY KEY, -- Unique ID for each product
|
| 84 |
name VARCHAR(50), -- Name of the product
|
|
|
|
| 117 |
-- sales.customer_id can be joined with customers.customer_id
|
| 118 |
-- sales.salesperson_id can be joined with salespeople.salesperson_id
|
| 119 |
-- product_suppliers.product_id can be joined with products.product_id
|
| 120 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
| 121 |
|
| 122 |
+
The following SQL query best answers the question `{question}`:
|
|
|
|
| 123 |
```sql
|
| 124 |
"""
|
| 125 |
+
messages = [{"role": "system", "content": sytems2}]
|
|
|
|
| 126 |
|
| 127 |
for val in history:
|
| 128 |
if val[0]:
|