Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
|
4 |
-
# Load
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
def
|
|
|
8 |
question = payload.get("question", "")
|
9 |
schema = payload.get("schema", "")
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
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=
|
32 |
-
inputs=gr.JSON(label="question
|
33 |
outputs="text",
|
34 |
-
title="Text-to-SQL Generator
|
35 |
-
description="
|
36 |
)
|
37 |
|
38 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
|
4 |
+
# Load the GaussAlgo model
|
5 |
+
model_path = "gaussalgo/T5-LM-Large-text2sql-spider"
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
8 |
|
9 |
+
def generate_sql(payload):
|
10 |
+
# Extract components from payload
|
11 |
question = payload.get("question", "")
|
12 |
schema = payload.get("schema", "")
|
13 |
+
|
14 |
+
# Build model input
|
15 |
+
full_prompt = f"Question: {question} Schema: {schema}"
|
16 |
+
|
17 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
18 |
+
outputs = model.generate(**inputs, max_length=512)
|
19 |
+
generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
+
|
21 |
+
return generated_sql
|
22 |
+
|
23 |
+
# Define expected input as a JSON object (dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
demo = gr.Interface(
|
25 |
+
fn=generate_sql,
|
26 |
+
inputs=gr.JSON(label="Input JSON (with 'question' and 'schema')"),
|
27 |
outputs="text",
|
28 |
+
title="Text-to-SQL Generator",
|
29 |
+
description="Input a JSON with your natural language question and database schema. Output is SQL."
|
30 |
)
|
31 |
|
32 |
demo.launch()
|