adamboom111 commited on
Commit
60e7810
·
verified ·
1 Parent(s): 07c821d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -1,20 +1,44 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load a text-to-SQL model. Can be swapped out for better ones later.
5
  generator = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-wikiSQL")
6
 
7
- def convert_to_sql(question):
8
- prompt = f"translate English to SQL: {question}"
9
- result = generator(prompt, max_length=256)[0]["generated_text"]
10
- return result
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  demo = gr.Interface(
13
  fn=convert_to_sql,
14
- inputs=gr.Textbox(lines=2, placeholder="Ask a question about your data..."),
15
  outputs="text",
16
- title="Text-to-SQL Generator",
17
- description="Enter a natural language question and get the SQL query for a table named 'sales_data'."
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()