Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
generator = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-wikiSQL")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
demo = gr.Interface(
|
| 13 |
fn=convert_to_sql,
|
| 14 |
-
inputs=gr.
|
| 15 |
outputs="text",
|
| 16 |
-
title="Text-to-SQL Generator",
|
| 17 |
-
description="
|
| 18 |
)
|
| 19 |
|
| 20 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load text-to-SQL model
|
| 5 |
generator = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-wikiSQL")
|
| 6 |
|
| 7 |
+
# Function to process full JSON input
|
| 8 |
+
def convert_to_sql(payload):
|
| 9 |
+
question = payload.get("question", "")
|
| 10 |
+
schema = payload.get("schema", "")
|
| 11 |
+
sample_rows = payload.get("sample_rows", [])
|
| 12 |
|
| 13 |
+
# Turn sample data into a preview string
|
| 14 |
+
preview_rows = "\n".join([str(row) for row in sample_rows[:5]])
|
| 15 |
+
|
| 16 |
+
# Build enhanced prompt
|
| 17 |
+
prompt = f"""
|
| 18 |
+
You are an AI that writes DuckDB-compatible SQL based on user questions.
|
| 19 |
+
|
| 20 |
+
Table Schema:
|
| 21 |
+
{schema}
|
| 22 |
+
|
| 23 |
+
Sample Data:
|
| 24 |
+
{preview_rows}
|
| 25 |
+
|
| 26 |
+
Question:
|
| 27 |
+
{question}
|
| 28 |
+
|
| 29 |
+
Write only the SQL query — no explanations.
|
| 30 |
+
""".strip()
|
| 31 |
+
|
| 32 |
+
result = generator(prompt, max_length=256, clean_up_tokenization_spaces=True)[0]["generated_text"]
|
| 33 |
+
return result.strip()
|
| 34 |
+
|
| 35 |
+
# Gradio interface for testing (takes JSON input)
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=convert_to_sql,
|
| 38 |
+
inputs=gr.JSON(label="question + schema + sample_rows"),
|
| 39 |
outputs="text",
|
| 40 |
+
title="Smarter Text-to-SQL Generator",
|
| 41 |
+
description="Send in natural question, schema, and data — get back accurate SQL."
|
| 42 |
)
|
| 43 |
|
| 44 |
demo.launch()
|