Chaitanya182004 commited on
Commit
ca9941d
·
verified ·
1 Parent(s): 4cb4101

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -29
app.py CHANGED
@@ -1,9 +1,16 @@
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
 
5
  MODEL_NAME = "gaussalgo/T5-LM-Large-text2sql-spider"
6
 
 
7
  tokenizer = None
8
  model = None
9
 
@@ -15,6 +22,7 @@ def load_model():
15
  global tokenizer, model
16
 
17
  if model is None:
 
18
  print("Loading model...")
19
 
20
  tokenizer = AutoTokenizer.from_pretrained(
@@ -27,71 +35,106 @@ def load_model():
27
 
28
  model.to(device)
29
 
 
 
30
  print(f"Model ready on {device}")
31
 
32
 
 
33
  def generate_sql(question, context):
34
 
35
- load_model()
36
 
37
- input_text = f"{question} | {context}"
38
 
39
- inputs = tokenizer(
40
- input_text,
41
- return_tensors="pt",
42
- max_length=512,
43
- truncation=True
44
- ).to(device)
45
 
 
46
 
47
- outputs = model.generate(
48
- **inputs,
49
- max_new_tokens=128,
50
- num_beams=4,
51
- early_stopping=True
52
- )
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- sql = tokenizer.decode(
56
- outputs[0],
57
- skip_special_tokens=True
58
- )
59
 
60
- return sql
 
 
 
61
 
62
 
63
 
64
  with gr.Blocks() as demo:
65
 
66
- gr.Markdown("# NL2SQL API")
 
 
 
 
67
 
68
  with gr.Row():
69
 
70
  question = gr.Textbox(
71
- label="Question"
 
72
  )
73
 
 
74
  context = gr.Textbox(
75
- label="Context"
 
76
  )
77
 
78
 
79
  output = gr.Textbox(
80
- label="SQL"
 
81
  )
82
 
83
 
84
- btn = gr.Button("Submit")
 
 
85
 
86
 
87
  btn.click(
88
  fn=generate_sql,
89
- inputs=[question, context],
 
 
 
90
  outputs=output
91
  )
92
 
93
 
94
- demo.launch(
95
- server_name="0.0.0.0",
96
- server_port=7860
97
- )
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Disable Gradio SSR (better for Render)
4
+ os.environ["GRADIO_SSR_MODE"] = "False"
5
+
6
  import gradio as gr
7
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
8
  import torch
9
 
10
+
11
  MODEL_NAME = "gaussalgo/T5-LM-Large-text2sql-spider"
12
 
13
+
14
  tokenizer = None
15
  model = None
16
 
 
22
  global tokenizer, model
23
 
24
  if model is None:
25
+
26
  print("Loading model...")
27
 
28
  tokenizer = AutoTokenizer.from_pretrained(
 
35
 
36
  model.to(device)
37
 
38
+ model.eval()
39
+
40
  print(f"Model ready on {device}")
41
 
42
 
43
+
44
  def generate_sql(question, context):
45
 
46
+ try:
47
 
48
+ load_model()
49
 
 
 
 
 
 
 
50
 
51
+ input_text = f"{question} | {context}"
52
 
 
 
 
 
 
 
53
 
54
+ inputs = tokenizer(
55
+ input_text,
56
+ return_tensors="pt",
57
+ max_length=512,
58
+ truncation=True
59
+ ).to(device)
60
+
61
+
62
+ with torch.no_grad():
63
+
64
+ outputs = model.generate(
65
+ **inputs,
66
+ max_new_tokens=128,
67
+ num_beams=4,
68
+ early_stopping=True
69
+ )
70
+
71
+
72
+ sql = tokenizer.decode(
73
+ outputs[0],
74
+ skip_special_tokens=True
75
+ )
76
+
77
+
78
+ return sql
79
 
 
 
 
 
80
 
81
+ except Exception as e:
82
+
83
+ return f"Error: {str(e)}"
84
+
85
 
86
 
87
 
88
  with gr.Blocks() as demo:
89
 
90
+
91
+ gr.Markdown(
92
+ "# NL2SQL API\nGenerate SQL from Natural Language"
93
+ )
94
+
95
 
96
  with gr.Row():
97
 
98
  question = gr.Textbox(
99
+ label="Question",
100
+ placeholder="Example: Find all users"
101
  )
102
 
103
+
104
  context = gr.Textbox(
105
+ label="Database Schema",
106
+ placeholder="Example: users(id,name,email)"
107
  )
108
 
109
 
110
  output = gr.Textbox(
111
+ label="Generated SQL",
112
+ lines=5
113
  )
114
 
115
 
116
+ btn = gr.Button(
117
+ "Generate SQL"
118
+ )
119
 
120
 
121
  btn.click(
122
  fn=generate_sql,
123
+ inputs=[
124
+ question,
125
+ context
126
+ ],
127
  outputs=output
128
  )
129
 
130
 
131
+
132
+ if __name__ == "__main__":
133
+
134
+ demo.launch(
135
+ server_name="0.0.0.0",
136
+ server_port=int(
137
+ os.environ.get("PORT",7860)
138
+ ),
139
+ share=False
140
+ )