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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -17
app.py CHANGED
@@ -4,37 +4,94 @@ import torch
4
 
5
  MODEL_NAME = "gaussalgo/T5-LM-Large-text2sql-spider"
6
 
7
- print("Loading model...")
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
- model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
10
- device = 'cuda' if torch.cuda.is_available() else 'cpu'
11
- model = model.to(device)
12
- print(f"Model ready on {device}")
13
-
14
- def generate_sql(question: str, context: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  input_text = f"{question} | {context}"
16
- inputs = tokenizer(
 
17
  input_text,
18
- return_tensors='pt',
19
  max_length=512,
20
  truncation=True
21
  ).to(device)
 
 
22
  outputs = model.generate(
23
  **inputs,
24
  max_new_tokens=128,
25
  num_beams=4,
26
  early_stopping=True
27
  )
28
- sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
29
  return sql
30
 
 
 
31
  with gr.Blocks() as demo:
 
32
  gr.Markdown("# NL2SQL API")
 
33
  with gr.Row():
34
- question = gr.Textbox(label="Question")
35
- context = gr.Textbox(label="Context")
36
- output = gr.Textbox(label="SQL")
37
- btn = gr.Button("Submit")
38
- btn.click(fn=generate_sql, inputs=[question, context], outputs=output)
39
 
40
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  MODEL_NAME = "gaussalgo/T5-LM-Large-text2sql-spider"
6
 
7
+ tokenizer = None
8
+ model = None
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+
12
+
13
+ def load_model():
14
+
15
+ global tokenizer, model
16
+
17
+ if model is None:
18
+ print("Loading model...")
19
+
20
+ tokenizer = AutoTokenizer.from_pretrained(
21
+ MODEL_NAME
22
+ )
23
+
24
+ model = AutoModelForSeq2SeqLM.from_pretrained(
25
+ MODEL_NAME
26
+ )
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
+ )