adamboom111 commited on
Commit
de0a20e
Β·
verified Β·
1 Parent(s): 60e7810

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -1,44 +1,38 @@
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()
 
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()