Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,38 @@
|
|
| 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 |
-
# 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 |
-
#
|
| 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
|
| 19 |
-
|
| 20 |
-
Table Schema:
|
| 21 |
{schema}
|
| 22 |
|
| 23 |
-
|
| 24 |
-
{
|
| 25 |
|
| 26 |
-
|
| 27 |
-
{question}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
""".strip()
|
| 31 |
|
| 32 |
-
result = generator(prompt, max_length=256
|
| 33 |
return result.strip()
|
| 34 |
|
| 35 |
-
#
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=convert_to_sql,
|
| 38 |
inputs=gr.JSON(label="question + schema + sample_rows"),
|
| 39 |
outputs="text",
|
| 40 |
-
title="
|
| 41 |
-
description="Send
|
| 42 |
)
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load model — stick with this one for now, later we can upgrade
|
| 5 |
generator = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-wikiSQL")
|
| 6 |
|
|
|
|
| 7 |
def convert_to_sql(payload):
|
| 8 |
question = payload.get("question", "")
|
| 9 |
schema = payload.get("schema", "")
|
| 10 |
sample_rows = payload.get("sample_rows", [])
|
| 11 |
|
| 12 |
+
# Craft prompt
|
|
|
|
|
|
|
|
|
|
| 13 |
prompt = f"""
|
| 14 |
+
You are an AI that converts natural language into SQL for a DuckDB database.
|
| 15 |
+
Given a table with the following schema:
|
|
|
|
| 16 |
{schema}
|
| 17 |
|
| 18 |
+
Here are some sample rows:
|
| 19 |
+
{sample_rows}
|
| 20 |
|
| 21 |
+
Write a syntactically correct SQL query (DuckDB-compatible) to answer this question: "{question}"
|
|
|
|
| 22 |
|
| 23 |
+
Only return the SQL query — no explanation, no markdown.
|
| 24 |
+
""".strip()
|
| 25 |
|
| 26 |
+
result = generator(prompt, max_length=256)[0]["generated_text"]
|
| 27 |
return result.strip()
|
| 28 |
|
| 29 |
+
# Define inputs/outputs for interactive mode (not used by FastAPI)
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=convert_to_sql,
|
| 32 |
inputs=gr.JSON(label="question + schema + sample_rows"),
|
| 33 |
outputs="text",
|
| 34 |
+
title="Text-to-SQL Generator (DuckDB)",
|
| 35 |
+
description="Send a JSON payload with question, schema, and sample rows"
|
| 36 |
)
|
| 37 |
|
| 38 |
demo.launch()
|