Update app.py
Browse files
app.py
CHANGED
|
@@ -186,61 +186,80 @@ def process_input(question, table_context):
|
|
| 186 |
return cleaned_sql
|
| 187 |
|
| 188 |
# Sample table context examples for the example selector
|
| 189 |
-
|
| 190 |
-
# Example 1
|
| 191 |
-
"""
|
| 192 |
-
CREATE TABLE customers (
|
| 193 |
-
id INT PRIMARY KEY,
|
| 194 |
-
name VARCHAR(100),
|
| 195 |
-
email VARCHAR(100),
|
| 196 |
-
order_date DATE
|
| 197 |
-
);
|
| 198 |
-
""",
|
| 199 |
-
|
| 200 |
-
# Example 2
|
| 201 |
-
"""
|
| 202 |
-
CREATE TABLE products (
|
| 203 |
-
id INT PRIMARY KEY,
|
| 204 |
-
name VARCHAR(100),
|
| 205 |
-
category VARCHAR(50),
|
| 206 |
-
price DECIMAL(10,2),
|
| 207 |
-
stock_quantity INT
|
| 208 |
-
);
|
| 209 |
-
""",
|
| 210 |
-
|
| 211 |
-
# Example 3
|
| 212 |
-
"""
|
| 213 |
-
CREATE TABLE employees (
|
| 214 |
-
id INT PRIMARY KEY,
|
| 215 |
-
name VARCHAR(100),
|
| 216 |
-
department VARCHAR(50),
|
| 217 |
-
salary DECIMAL(10,2),
|
| 218 |
-
hire_date DATE
|
| 219 |
-
);
|
| 220 |
-
CREATE TABLE departments (
|
| 221 |
-
id INT PRIMARY KEY,
|
| 222 |
-
name VARCHAR(50),
|
| 223 |
-
manager_id INT,
|
| 224 |
-
budget DECIMAL(15,2)
|
| 225 |
-
);
|
| 226 |
-
"""
|
| 227 |
-
]
|
| 228 |
|
| 229 |
# Sample question examples
|
| 230 |
-
|
| 231 |
-
"Get the names and emails of customers who placed an order in the last 30 days.",
|
| 232 |
-
"Find all products with less than 10 items in stock.",
|
| 233 |
-
"List all employees in the Sales department with a salary greater than 50000.",
|
| 234 |
-
"What is the total budget for departments with more than 5 employees?",
|
| 235 |
-
"Count how many products are in each category where the price is greater than 100."
|
| 236 |
-
]
|
| 237 |
|
| 238 |
# Create the Gradio interface
|
| 239 |
with gr.Blocks(title="Text to SQL Converter") as demo:
|
| 240 |
gr.Markdown("# Text to SQL Query Converter")
|
| 241 |
gr.Markdown("Enter your question and optional table context to generate an SQL query.")
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
# Launch the app
|
| 246 |
demo.launch()
|
|
|
|
| 186 |
return cleaned_sql
|
| 187 |
|
| 188 |
# Sample table context examples for the example selector
|
| 189 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
# Sample question examples
|
| 192 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
# Create the Gradio interface
|
| 195 |
with gr.Blocks(title="Text to SQL Converter") as demo:
|
| 196 |
gr.Markdown("# Text to SQL Query Converter")
|
| 197 |
gr.Markdown("Enter your question and optional table context to generate an SQL query.")
|
| 198 |
|
| 199 |
+
with gr.Row():
|
| 200 |
+
with gr.Column():
|
| 201 |
+
question_input = gr.Textbox(
|
| 202 |
+
label="Your Question",
|
| 203 |
+
placeholder="e.g., Find all products with price less than $50",
|
| 204 |
+
lines=2
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
table_context = gr.Textbox(
|
| 208 |
+
label="Table Context (Optional)",
|
| 209 |
+
placeholder="Enter your database schema or table definitions here...",
|
| 210 |
+
lines=10
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
submit_btn = gr.Button("Generate SQL Query")
|
| 214 |
+
|
| 215 |
+
with gr.Column():
|
| 216 |
+
sql_output = gr.Code(
|
| 217 |
+
label="Generated SQL Query",
|
| 218 |
+
language="sql",
|
| 219 |
+
lines=12
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
# Examples section
|
| 223 |
+
gr.Markdown("### Try some examples")
|
| 224 |
+
|
| 225 |
+
example_selector = gr.Examples(
|
| 226 |
+
examples=[
|
| 227 |
+
["List all products in the 'Electronics' category with price less than $500", example_contexts[1]],
|
| 228 |
+
["Find the total number of employees in each department", example_contexts[2]],
|
| 229 |
+
["Get customers who placed orders in the last 7 days", example_contexts[0]],
|
| 230 |
+
["Count the number of products in each category", example_contexts[1]],
|
| 231 |
+
["Find the average salary by department", example_contexts[2]]
|
| 232 |
+
],
|
| 233 |
+
inputs=[question_input, table_context]
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
# Set up the submit button to trigger the process_input function
|
| 237 |
+
submit_btn.click(
|
| 238 |
+
fn=process_input,
|
| 239 |
+
inputs=[question_input, table_context],
|
| 240 |
+
outputs=sql_output
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
# Also trigger on pressing Enter in the question input
|
| 244 |
+
question_input.submit(
|
| 245 |
+
fn=process_input,
|
| 246 |
+
inputs=[question_input, table_context],
|
| 247 |
+
outputs=sql_output
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
# Add information about the model
|
| 251 |
+
gr.Markdown("""
|
| 252 |
+
### About
|
| 253 |
+
This app uses a fine-tuned language model to convert natural language questions into SQL queries.
|
| 254 |
|
| 255 |
+
- **Model**: [onkolahmet/Qwen2-0.5B-Instruct-SQL-generator](https://huggingface.co/onkolahmet/Qwen2-0.5B-Instruct-SQL-generator)
|
| 256 |
+
- **How to use**:
|
| 257 |
+
1. Enter your question in natural language
|
| 258 |
+
2. If you have specific table schemas, add them in the Table Context field
|
| 259 |
+
3. Click "Generate SQL Query" or press Enter
|
| 260 |
+
|
| 261 |
+
Note: The model works best when table context is provided, but can generate generic SQL queries without it.
|
| 262 |
+
""")
|
| 263 |
|
| 264 |
# Launch the app
|
| 265 |
demo.launch()
|