Spaces:
Runtime error
Runtime error
nileshhanotia
commited on
Commit
•
9a4c76d
1
Parent(s):
6028d5c
Create sql_generator.py
Browse files- sql_generator.py +60 -0
sql_generator.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spacy
|
2 |
+
|
3 |
+
# Load the spaCy English model
|
4 |
+
nlp = spacy.load("en_core_web_sm")
|
5 |
+
|
6 |
+
# Define the mapping of keywords to SQL column names based on the new schema
|
7 |
+
column_mapping = {
|
8 |
+
"handle": "Handle",
|
9 |
+
"title": "Title",
|
10 |
+
"body": "Body (HTML)",
|
11 |
+
"vendor": "Vendor",
|
12 |
+
"type": "Type",
|
13 |
+
"tags": "Tags",
|
14 |
+
"published": "Published",
|
15 |
+
"option1_name": "Option1 Name",
|
16 |
+
"option1_value": "Option1 Value",
|
17 |
+
"option2_name": "Option2 Name",
|
18 |
+
"option2_value": "Option2 Value",
|
19 |
+
"option3_name": "Option3 Name",
|
20 |
+
"option3_value": "Option3 Value",
|
21 |
+
"variant_sku": "Variant SKU",
|
22 |
+
"variant_price": "Variant Price",
|
23 |
+
"variant_inventory_qty": "Variant Inventory Qty",
|
24 |
+
# Add more mappings as needed
|
25 |
+
}
|
26 |
+
|
27 |
+
# Function to generate SQL query based on natural language input
|
28 |
+
def generate_sql_query(natural_language_input):
|
29 |
+
doc = nlp(natural_language_input)
|
30 |
+
|
31 |
+
# Initialize SQL query parts
|
32 |
+
sql_query = "SELECT * FROM products WHERE "
|
33 |
+
conditions = []
|
34 |
+
|
35 |
+
# Extract keywords and values for building the query
|
36 |
+
for token in doc:
|
37 |
+
if token.lemma_ in column_mapping:
|
38 |
+
column_name = column_mapping[token.lemma_]
|
39 |
+
# Look for the word 'is' and the value after it
|
40 |
+
if token.nbor(1).text.lower() == "is":
|
41 |
+
value_token = token.nbor(2) # Get the token after 'is'
|
42 |
+
# Check if the value token is valid (e.g., if it's a string)
|
43 |
+
if value_token and value_token.text not in ["and", "or"]:
|
44 |
+
# Format the value correctly for SQL
|
45 |
+
conditions.append(f"{column_name} = '{value_token.text}'")
|
46 |
+
|
47 |
+
# Join conditions with AND
|
48 |
+
if conditions:
|
49 |
+
sql_query += " AND ".join(conditions)
|
50 |
+
else:
|
51 |
+
sql_query = sql_query[:-7] # Remove the last " WHERE " if no conditions were added
|
52 |
+
|
53 |
+
return sql_query
|
54 |
+
|
55 |
+
# Example natural language input
|
56 |
+
natural_language_input = "Find products where title is laptop and vendor is apple and published is active"
|
57 |
+
|
58 |
+
# Generate SQL query
|
59 |
+
sql_query = generate_sql_query(natural_language_input)
|
60 |
+
print(sql_query)
|