RealMati commited on
Commit
735b5da
·
verified ·
1 Parent(s): e27cd82

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +20 -105
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import re
 
2
  import gradio as gr
3
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
  import torch
@@ -14,85 +15,10 @@ print("Model loaded.")
14
  AGG_OPS = ["", "MAX", "MIN", "COUNT", "SUM", "AVG"]
15
  OPS = ["=", ">", "<", ">=", "<=", "!="]
16
 
17
- CSS = """
18
- .main-header {
19
- text-align: center;
20
- margin-bottom: 0.5rem;
21
- }
22
- .main-header h1 {
23
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
24
- -webkit-background-clip: text;
25
- -webkit-text-fill-color: transparent;
26
- font-size: 2.4rem;
27
- font-weight: 800;
28
- margin-bottom: 0.25rem;
29
- }
30
- .main-header p {
31
- color: #6b7280;
32
- font-size: 1.05rem;
33
- max-width: 600px;
34
- margin: 0 auto;
35
- }
36
- .pipeline-box {
37
- background: linear-gradient(135deg, #f0f4ff 0%, #faf0ff 100%);
38
- border: 1px solid #e0d4f5;
39
- border-radius: 12px;
40
- padding: 1rem 1.5rem;
41
- text-align: center;
42
- font-family: 'SF Mono', 'Fira Code', monospace;
43
- font-size: 0.9rem;
44
- color: #4a4a6a;
45
- margin-bottom: 1rem;
46
- }
47
- .sql-output textarea {
48
- font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace !important;
49
- font-size: 1.1rem !important;
50
- background: #1e1e2e !important;
51
- color: #cdd6f4 !important;
52
- border: 1px solid #45475a !important;
53
- border-radius: 10px !important;
54
- padding: 1rem !important;
55
- }
56
- .raw-output textarea {
57
- font-family: 'SF Mono', 'Fira Code', monospace !important;
58
- font-size: 0.9rem !important;
59
- background: #f8f9fc !important;
60
- color: #6b7280 !important;
61
- border: 1px solid #e5e7eb !important;
62
- border-radius: 8px !important;
63
- }
64
- .input-section {
65
- border: 1px solid #e5e7eb;
66
- border-radius: 12px;
67
- padding: 1.25rem;
68
- background: #fafbff;
69
- }
70
- .generate-btn {
71
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
72
- border: none !important;
73
- color: white !important;
74
- font-weight: 600 !important;
75
- font-size: 1.05rem !important;
76
- padding: 0.75rem 2rem !important;
77
- border-radius: 10px !important;
78
- min-height: 46px !important;
79
- }
80
- .generate-btn:hover {
81
- opacity: 0.92 !important;
82
- transform: translateY(-1px);
83
- box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
84
- }
85
- .info-badge {
86
- display: inline-block;
87
- background: #eef2ff;
88
- color: #4f46e5;
89
- padding: 0.2rem 0.6rem;
90
- border-radius: 6px;
91
- font-size: 0.8rem;
92
- font-weight: 600;
93
- }
94
- footer { display: none !important; }
95
- """
96
 
97
 
98
  def decode_structured_output(text):
@@ -216,18 +142,6 @@ theme = gr.themes.Soft(
216
  neutral_hue="slate",
217
  font=gr.themes.GoogleFont("Inter"),
218
  font_mono=gr.themes.GoogleFont("Fira Code"),
219
- ).set(
220
- body_background_fill="#fafbff",
221
- block_background_fill="white",
222
- block_border_width="1px",
223
- block_border_color="#e5e7eb",
224
- block_radius="12px",
225
- block_shadow="0 1px 3px rgba(0,0,0,0.06)",
226
- input_border_color="#d1d5db",
227
- input_border_width="1px",
228
- input_radius="8px",
229
- button_primary_background_fill="linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
230
- button_primary_text_color="white",
231
  )
232
 
233
  with gr.Blocks(title="Text-to-SQL Demo") as demo:
@@ -235,33 +149,37 @@ with gr.Blocks(title="Text-to-SQL Demo") as demo:
235
  gr.HTML("""
236
  <div class="main-header">
237
  <h1>Text-to-SQL</h1>
238
- <p>Fine-tuned T5 model that converts natural language questions into structured SQL queries using the WikiSQL dataset</p>
 
239
  </div>
240
  """)
241
 
242
- # Pipeline visualization
243
  gr.HTML("""
244
  <div class="pipeline-box">
245
- Natural Language &rarr; T5 Encoder &rarr; Structured Tokens (SEL | AGG | CONDS) &rarr; SQL Query
 
 
 
 
 
 
246
  </div>
247
  """)
248
 
249
  with gr.Row(equal_height=True):
250
- # Left: Inputs
251
  with gr.Column(scale=1):
252
- gr.Markdown("### Input")
253
  question = gr.Textbox(
254
  label="Natural Language Question",
255
  placeholder="e.g. What is terrence ross' nationality?",
256
  lines=2,
257
- elem_classes=["input-section"],
258
  )
259
  schema = gr.Textbox(
260
  label="Database Schema",
261
  placeholder="table_name: col1, col2, col3, ...",
262
  lines=2,
263
  info="Format: table_name: column1, column2, column3",
264
- elem_classes=["input-section"],
265
  )
266
  with gr.Row():
267
  beams = gr.Slider(
@@ -275,9 +193,8 @@ with gr.Blocks(title="Text-to-SQL Demo") as demo:
275
  )
276
  btn = gr.Button("Generate SQL", variant="primary", elem_classes=["generate-btn"])
277
 
278
- # Right: Outputs
279
  with gr.Column(scale=1):
280
- gr.Markdown("### Output")
281
  sql_out = gr.Textbox(
282
  label="Generated SQL",
283
  lines=3,
@@ -300,8 +217,7 @@ with gr.Blocks(title="Text-to-SQL Demo") as demo:
300
  outputs=[sql_out, raw_out, parsed_out],
301
  )
302
 
303
- # Examples
304
- gr.Markdown("### Try These Examples")
305
  gr.Examples(
306
  examples=[
307
  ["What is terrence ross' nationality", "players: Player, No., Nationality, Position, Years in Toronto, School/Club Team", 5, 256],
@@ -320,15 +236,14 @@ with gr.Blocks(title="Text-to-SQL Demo") as demo:
320
  cache_examples=False,
321
  )
322
 
323
- # Footer info
324
  gr.HTML("""
325
- <div style="text-align:center; margin-top:1.5rem; padding:1rem; color:#9ca3af; font-size:0.85rem;">
326
  <span class="info-badge">T5-base</span>&nbsp;
327
  <span class="info-badge">WikiSQL</span>&nbsp;
328
  <span class="info-badge">Seq2Seq</span>&nbsp;
329
  <span class="info-badge">Structured Output</span>
330
  <p style="margin-top:0.75rem;">
331
- Model: <a href="https://huggingface.co/RealMati/t2sql_v6_structured" target="_blank" style="color:#667eea;">RealMati/t2sql_v6_structured</a>
332
  </p>
333
  </div>
334
  """)
 
1
  import re
2
+ import os
3
  import gradio as gr
4
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
5
  import torch
 
15
  AGG_OPS = ["", "MAX", "MIN", "COUNT", "SUM", "AVG"]
16
  OPS = ["=", ">", "<", ">=", "<=", "!="]
17
 
18
+ # Load CSS from external file
19
+ css_path = os.path.join(os.path.dirname(__file__), "style.css")
20
+ with open(css_path, "r") as f:
21
+ CSS = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
 
24
  def decode_structured_output(text):
 
142
  neutral_hue="slate",
143
  font=gr.themes.GoogleFont("Inter"),
144
  font_mono=gr.themes.GoogleFont("Fira Code"),
 
 
 
 
 
 
 
 
 
 
 
 
145
  )
146
 
147
  with gr.Blocks(title="Text-to-SQL Demo") as demo:
 
149
  gr.HTML("""
150
  <div class="main-header">
151
  <h1>Text-to-SQL</h1>
152
+ <p>Fine-tuned T5 model that converts natural language questions
153
+ into structured SQL queries using the WikiSQL dataset</p>
154
  </div>
155
  """)
156
 
157
+ # Pipeline visualization - dark background so text is always visible
158
  gr.HTML("""
159
  <div class="pipeline-box">
160
+ <span class="stage">Natural Language</span>
161
+ <span class="arrow"> &rarr; </span>
162
+ <span class="stage">T5 Encoder</span>
163
+ <span class="arrow"> &rarr; </span>
164
+ <span class="highlight">Structured Tokens (SEL | AGG | CONDS)</span>
165
+ <span class="arrow"> &rarr; </span>
166
+ <span class="stage">SQL Query</span>
167
  </div>
168
  """)
169
 
170
  with gr.Row(equal_height=True):
 
171
  with gr.Column(scale=1):
172
+ gr.Markdown("### Input", elem_classes=["section-header"])
173
  question = gr.Textbox(
174
  label="Natural Language Question",
175
  placeholder="e.g. What is terrence ross' nationality?",
176
  lines=2,
 
177
  )
178
  schema = gr.Textbox(
179
  label="Database Schema",
180
  placeholder="table_name: col1, col2, col3, ...",
181
  lines=2,
182
  info="Format: table_name: column1, column2, column3",
 
183
  )
184
  with gr.Row():
185
  beams = gr.Slider(
 
193
  )
194
  btn = gr.Button("Generate SQL", variant="primary", elem_classes=["generate-btn"])
195
 
 
196
  with gr.Column(scale=1):
197
+ gr.Markdown("### Output", elem_classes=["section-header"])
198
  sql_out = gr.Textbox(
199
  label="Generated SQL",
200
  lines=3,
 
217
  outputs=[sql_out, raw_out, parsed_out],
218
  )
219
 
220
+ gr.Markdown("### Try These Examples", elem_classes=["section-header"])
 
221
  gr.Examples(
222
  examples=[
223
  ["What is terrence ross' nationality", "players: Player, No., Nationality, Position, Years in Toronto, School/Club Team", 5, 256],
 
236
  cache_examples=False,
237
  )
238
 
 
239
  gr.HTML("""
240
+ <div class="footer-section">
241
  <span class="info-badge">T5-base</span>&nbsp;
242
  <span class="info-badge">WikiSQL</span>&nbsp;
243
  <span class="info-badge">Seq2Seq</span>&nbsp;
244
  <span class="info-badge">Structured Output</span>
245
  <p style="margin-top:0.75rem;">
246
+ Model: <a href="https://huggingface.co/RealMati/t2sql_v6_structured" target="_blank">RealMati/t2sql_v6_structured</a>
247
  </p>
248
  </div>
249
  """)