Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,26 +7,31 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 8 |
|
| 9 |
def generate_sql(payload):
|
| 10 |
-
# Extract
|
| 11 |
question = payload.get("question", "")
|
| 12 |
schema = payload.get("schema", "")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=generate_sql,
|
| 26 |
-
inputs=gr.JSON(label="Input JSON (
|
| 27 |
outputs="text",
|
| 28 |
title="Text-to-SQL Generator",
|
| 29 |
-
description="
|
| 30 |
)
|
| 31 |
|
| 32 |
demo.launch()
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 8 |
|
| 9 |
def generate_sql(payload):
|
| 10 |
+
# Extract parts from the JSON payload
|
| 11 |
question = payload.get("question", "")
|
| 12 |
schema = payload.get("schema", "")
|
| 13 |
+
sample_rows = payload.get("sample_rows", [])
|
| 14 |
+
|
| 15 |
+
# Convert sample rows into a single string
|
| 16 |
+
sample_str = " ".join([str(row) for row in sample_rows]) if sample_rows else ""
|
| 17 |
+
|
| 18 |
+
# Build model input prompt
|
| 19 |
+
prompt = f"Question: {question} Schema: {schema} Sample Rows: {sample_str}"
|
| 20 |
+
|
| 21 |
+
# Tokenize and generate
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 23 |
outputs = model.generate(**inputs, max_length=512)
|
| 24 |
generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
return generated_sql
|
| 27 |
|
| 28 |
+
# Gradio interface
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=generate_sql,
|
| 31 |
+
inputs=gr.JSON(label="Input JSON (question, schema, sample_rows)"),
|
| 32 |
outputs="text",
|
| 33 |
title="Text-to-SQL Generator",
|
| 34 |
+
description="Enter a JSON object with 'question', 'schema', and optional 'sample_rows'. The model will generate SQL."
|
| 35 |
)
|
| 36 |
|
| 37 |
demo.launch()
|