adamboom111 commited on
Commit
cb04666
·
verified ·
1 Parent(s): cc6747b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
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 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()
 
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()