abir-hr196 commited on
Commit
f35a40c
·
1 Parent(s): edf7bcc
Files changed (3) hide show
  1. app.py +9 -3
  2. tinysql_dataset_viewer.py +123 -200
  3. tinysql_model_demo.py +94 -118
app.py CHANGED
@@ -6,6 +6,7 @@ custom_css = """
6
  :root {
7
  --martian-orange: #FF6B4A;
8
  --martian-black: #0A0A0A;
 
9
  }
10
 
11
  .gradio-container {
@@ -26,11 +27,16 @@ custom_css = """
26
  """
27
 
28
  with gr.Blocks(css=custom_css, title="TinySQL Demo") as demo:
 
 
 
 
29
  with gr.Tabs():
30
- with gr.Tab("Model Demo"):
31
- model_demo()
32
  with gr.Tab("Dataset Viewer"):
33
- dataset_viewer()
 
 
 
34
 
35
  if __name__ == "__main__":
36
  demo.launch()
 
6
  :root {
7
  --martian-orange: #FF6B4A;
8
  --martian-black: #0A0A0A;
9
+ --martian-gray-dark: #1A1A1A;
10
  }
11
 
12
  .gradio-container {
 
27
  """
28
 
29
  with gr.Blocks(css=custom_css, title="TinySQL Demo") as demo:
30
+ # Shared state for passing data between tabs
31
+ shared_instruction = gr.State("")
32
+ shared_schema = gr.State("")
33
+
34
  with gr.Tabs():
 
 
35
  with gr.Tab("Dataset Viewer"):
36
+ viewer_components = dataset_viewer(shared_instruction, shared_schema)
37
+
38
+ with gr.Tab("Model Demo"):
39
+ model_components = model_demo(shared_instruction, shared_schema)
40
 
41
  if __name__ == "__main__":
42
  demo.launch()
tinysql_dataset_viewer.py CHANGED
@@ -18,7 +18,10 @@ def load_preview(dataset_name):
18
  """Load first 500 rows of selected dataset"""
19
  try:
20
  ds = load_dataset(DATASETS[dataset_name], split="train")
21
- df = pd.DataFrame(ds)[COLUMNS].head(500)
 
 
 
22
  return df
23
  except Exception as e:
24
  return pd.DataFrame({"Error": [str(e)]})
@@ -34,208 +37,128 @@ def filter_dataframe(df, search_query):
34
  )
35
  return df[mask]
36
 
37
- # CSS styling
38
- custom_css = """
39
- :root {
40
- --martian-orange: #FF6B4A;
41
- --martian-black: #0A0A0A;
42
- --martian-gray-dark: #1A1A1A;
43
- --martian-gray-medium: #2A2A2A;
44
- --martian-gray-light: #3A3A3A;
45
- }
46
-
47
- .gradio-container {
48
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
49
- background-color: var(--martian-black) !important;
50
- color: #E0E0E0 !important;
51
- }
52
-
53
- .header-section {
54
- text-align: center;
55
- padding: 2.5rem 1.5rem;
56
- background: linear-gradient(135deg, var(--martian-gray-dark) 0%, var(--martian-gray-medium) 100%);
57
- border-radius: 16px;
58
- margin-bottom: 2rem;
59
- color: white;
60
- box-shadow: 0 4px 6px rgba(0,0,0,0.3);
61
- }
62
-
63
- .header-section h1 {
64
- font-size: 2.2rem;
65
- font-weight: 700;
66
- margin-bottom: 0.75rem;
67
- }
68
-
69
- .header-section .subtitle {
70
- font-size: 1.1rem;
71
- opacity: 0.9;
72
- line-height: 1.6;
73
- }
74
-
75
- .orange-accent {
76
- color: var(--martian-orange);
77
- font-weight: 600;
78
- }
79
-
80
- .info-box {
81
- background: var(--martian-gray-dark);
82
- border-radius: 12px;
83
- padding: 1.5rem;
84
- margin: 1.5rem 0;
85
- border-left: 4px solid var(--martian-orange);
86
- color: #E0E0E0;
87
- }
88
-
89
- .dataset-guide {
90
- background: var(--martian-gray-dark);
91
- border-radius: 8px;
92
- padding: 1rem;
93
- margin-top: 1rem;
94
- font-size: 0.9rem;
95
- color: #D0D0D0;
96
- }
97
-
98
- button.primary {
99
- background: var(--martian-orange) !important;
100
- border: none !important;
101
- color: white !important;
102
- font-weight: 600 !important;
103
- }
104
-
105
- button.primary:hover {
106
- background: #FF5733 !important;
107
- transform: translateY(-1px);
108
- box-shadow: 0 4px 8px rgba(255, 107, 74, 0.3);
109
- }
110
-
111
- input, select, textarea {
112
- background: var(--martian-gray-medium) !important;
113
- border-color: var(--martian-gray-light) !important;
114
- color: #E0E0E0 !important;
115
- }
116
-
117
- .dataframe {
118
- background: var(--martian-gray-dark) !important;
119
- }
120
-
121
- label {
122
- color: #D0D0D0 !important;
123
- }
124
-
125
- .label-wrap span {
126
- color: var(--martian-orange) !important;
127
- }
128
- """
129
-
130
- def dataset_viewer():
131
- with gr.Blocks(css=custom_css, title="TinySQL Dataset Viewer") as viewer:
132
- # Header
133
- gr.HTML("""
134
- <div class="header-section">
135
- <h1>TinySQL Dataset Viewer</h1>
136
- <p class="subtitle">
137
- Browse dataset previews, search, and filter queries with <span class="orange-accent">ease</span>
138
- </p>
139
- </div>
140
- """)
141
-
142
- # Info box
143
- gr.HTML("""
144
- <div class="info-box">
145
- <strong>Preview Mode:</strong> Showing first 500 rows of each dataset. Use search to filter results in real-time.
146
- </div>
147
- """)
148
-
149
- with gr.Row():
150
- with gr.Column(scale=1):
151
- gr.Markdown("### Dataset Selection")
152
- dataset_dropdown = gr.Dropdown(
153
- choices=list(DATASETS.keys()),
154
- value="CS1",
155
- label="Choose Dataset",
156
- info="Select a dataset to preview"
157
- )
158
-
159
- gr.HTML("""
160
- <div class="dataset-guide">
161
- <strong>Complexity Levels:</strong><br><br>
162
- <strong>CS1:</strong> Basic SELECT-FROM<br>
163
- <strong>CS2:</strong> Adds ORDER BY<br>
164
- <strong>CS3:</strong> Aggregations<br>
165
- <strong>CS4:</strong> Adds WHERE filters<br><br>
166
- <strong>Synonyms:</strong> Natural language variations
167
- </div>
168
- """)
169
-
170
- load_btn = gr.Button("Load Dataset", variant="primary", size="lg")
171
-
172
- gr.HTML("<br>")
173
-
174
- demo_btn = gr.Button("🚀 Try Model Demo", variant="primary")
175
 
176
- with gr.Column(scale=3):
177
- gr.Markdown("### Dataset Preview (First 500 Rows)")
178
-
179
- search_box = gr.Textbox(
180
- label="Search",
181
- placeholder="Search across all columns...",
182
- lines=1
183
- )
184
-
185
- df_display = gr.Dataframe(
186
- headers=COLUMNS,
187
- datatype=["str", "str", "str"],
188
- interactive=False,
189
- wrap=True,
190
- label="Results",
191
- max_height=600 # Use height instead to control visible area
192
- )
193
-
194
- stats_display = gr.Markdown("Click 'Load Dataset' to begin")
195
-
196
- # Store the loaded dataframe
197
- df_state = gr.State(value=pd.DataFrame())
198
-
199
- # Load dataset
200
- def load_and_display(dataset_name):
201
- df = load_preview(dataset_name)
202
- if "Error" in df.columns:
203
- return df, df, "❌ Error loading dataset"
204
- stats = f"**Loaded:** {len(df)} rows | **Columns:** {', '.join(COLUMNS)}"
205
- return df, df, stats
206
-
207
- load_btn.click(
208
- fn=load_and_display,
209
- inputs=dataset_dropdown,
210
- outputs=[df_state, df_display, stats_display]
211
- )
212
 
213
- # Search functionality
214
- def search_and_display(df, query):
215
- if df.empty:
216
- return df, "Load a dataset first"
 
 
 
 
217
 
218
- filtered_df = filter_dataframe(df, query)
219
- stats = f"**Showing:** {len(filtered_df)} of {len(df)} rows"
220
- if query:
221
- stats += f" | **Search:** '{query}'"
222
- return filtered_df, stats
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
- search_box.change(
225
- fn=search_and_display,
226
- inputs=[df_state, search_box],
227
- outputs=[df_display, stats_display]
228
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
- # Open model demo
231
- demo_btn.click(
232
- lambda: None,
233
- None,
234
- None,
235
- _js="()=>{ window.open('https://huggingface.co/spaces/abir-hr196/tinysql-demo','_blank'); }"
236
- )
237
 
238
- return viewer
239
-
240
- if __name__ == "__main__":
241
- dataset_viewer().launch()
 
 
 
 
 
 
 
18
  """Load first 500 rows of selected dataset"""
19
  try:
20
  ds = load_dataset(DATASETS[dataset_name], split="train")
21
+ df = pd.DataFrame(ds).head(500)
22
+ # Filter to only the columns we want
23
+ if all(col in df.columns for col in COLUMNS):
24
+ df = df[COLUMNS]
25
  return df
26
  except Exception as e:
27
  return pd.DataFrame({"Error": [str(e)]})
 
37
  )
38
  return df[mask]
39
 
40
+ def dataset_viewer(shared_instruction, shared_schema):
41
+ """Dataset viewer component with ability to send examples to model demo"""
42
+
43
+ gr.HTML("""
44
+ <div class="header-section" style="text-align: center; padding: 2.5rem 1.5rem; background: linear-gradient(135deg, #1A1A1A 0%, #2A2A2A 100%); border-radius: 16px; margin-bottom: 2rem; color: white;">
45
+ <h1 style="font-size: 2.2rem; font-weight: 700; margin-bottom: 0.75rem;">TinySQL Dataset Viewer</h1>
46
+ <p style="font-size: 1.1rem; opacity: 0.9; line-height: 1.6;">
47
+ Browse dataset previews, search, and filter queries with <span style="color: #FF6B4A; font-weight: 600;">ease</span>
48
+ </p>
49
+ </div>
50
+ """)
51
+
52
+ gr.HTML("""
53
+ <div class="info-box" style="background: #3A3A3A; border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; border-left: 4px solid #FF6B4A; color: #E0E0E0;">
54
+ <strong>Preview Mode:</strong> Showing first 500 rows of each dataset. Use search to filter results in real-time.
55
+ </div>
56
+ """)
57
+
58
+ with gr.Row():
59
+ with gr.Column(scale=1):
60
+ gr.Markdown("### Dataset Selection")
61
+ dataset_dropdown = gr.Dropdown(
62
+ choices=list(DATASETS.keys()),
63
+ value="CS1",
64
+ label="Choose Dataset",
65
+ info="Select a dataset to preview"
66
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
+ gr.HTML("""
69
+ <div style="background: #3A3A3A; border-radius: 8px; padding: 1rem; margin-top: 1rem; font-size: 0.9rem; color: #D0D0D0;">
70
+ <strong>Complexity Levels:</strong><br><br>
71
+ <strong>CS1:</strong> Basic SELECT-FROM<br>
72
+ <strong>CS2:</strong> Adds ORDER BY<br>
73
+ <strong>CS3:</strong> Aggregations<br>
74
+ <strong>CS4:</strong> Adds WHERE filters<br><br>
75
+ <strong>Synonyms:</strong> Natural language variations
76
+ </div>
77
+ """)
78
+
79
+ load_btn = gr.Button("Load Dataset", variant="primary", size="lg")
80
+
81
+ row_selector = gr.Number(
82
+ label="Select Row to Test",
83
+ value=0,
84
+ minimum=0,
85
+ precision=0,
86
+ info="Enter row number to send to Model Demo"
87
+ )
88
+
89
+ send_to_model_btn = gr.Button("🚀 Run This Example in Model Demo", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ with gr.Column(scale=3):
92
+ gr.Markdown("### Dataset Preview (First 500 Rows)")
93
+
94
+ search_box = gr.Textbox(
95
+ label="Search",
96
+ placeholder="Search across all columns...",
97
+ lines=1
98
+ )
99
 
100
+ df_display = gr.Dataframe(
101
+ headers=COLUMNS,
102
+ datatype=["str", "str", "str"],
103
+ interactive=False,
104
+ wrap=True,
105
+ label="Results"
106
+ )
107
+
108
+ stats_display = gr.Markdown("Click 'Load Dataset' to begin")
109
+
110
+ # Store the loaded dataframe
111
+ df_state = gr.State(value=pd.DataFrame())
112
+
113
+ # Load dataset
114
+ def load_and_display(dataset_name):
115
+ df = load_preview(dataset_name)
116
+ if "Error" in df.columns:
117
+ return df, df, "❌ Error loading dataset"
118
+ stats = f"**Loaded:** {len(df)} rows | **Columns:** {', '.join(COLUMNS)}"
119
+ return df, df, stats
120
+
121
+ load_btn.click(
122
+ fn=load_and_display,
123
+ inputs=dataset_dropdown,
124
+ outputs=[df_state, df_display, stats_display]
125
+ )
126
+
127
+ # Search functionality
128
+ def search_and_display(df, query):
129
+ if df.empty:
130
+ return df, "Load a dataset first"
131
 
132
+ filtered_df = filter_dataframe(df, query)
133
+ stats = f"**Showing:** {len(filtered_df)} of {len(df)} rows"
134
+ if query:
135
+ stats += f" | **Search:** '{query}'"
136
+ return filtered_df, stats
137
+
138
+ search_box.change(
139
+ fn=search_and_display,
140
+ inputs=[df_state, search_box],
141
+ outputs=[df_display, stats_display]
142
+ )
143
+
144
+ # Send example to model demo
145
+ def send_to_model(df, row_num):
146
+ if df.empty or row_num >= len(df):
147
+ return "", "", "⚠️ Invalid row number or no data loaded"
148
+
149
+ row = df.iloc[int(row_num)]
150
+ instruction = row['english_prompt'] if 'english_prompt' in row else ""
151
+ schema = row['create_statement'] if 'create_statement' in row else ""
152
 
153
+ return instruction, schema, f"✅ Row {row_num} loaded! Switch to Model Demo tab."
 
 
 
 
 
 
154
 
155
+ send_to_model_btn.click(
156
+ fn=send_to_model,
157
+ inputs=[df_state, row_selector],
158
+ outputs=[shared_instruction, shared_schema, stats_display]
159
+ )
160
+
161
+ return {
162
+ 'df_state': df_state,
163
+ 'df_display': df_display
164
+ }
tinysql_model_demo.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
- # ---------------- Model Setup ----------------
6
  MODELS = {
7
  "BM1_CS1_Syn (33M)": "withmartian/sql_interp_bm1_cs1_experiment_1.10",
8
  "BM1_CS2_Syn (33M)": "withmartian/sql_interp_bm1_cs2_experiment_2.10",
@@ -62,7 +62,6 @@ def generate_sql(model_name, instruction, schema, max_length=256, temperature=0.
62
  except Exception as e:
63
  return f"Error: {str(e)}"
64
 
65
- # ---------------- Example Queries ----------------
66
  examples = [
67
  [
68
  "BM1_CS1_Syn (33M)",
@@ -81,119 +80,96 @@ examples = [
81
  ],
82
  ]
83
 
84
- # ---------------- Model Demo Function ----------------
85
- def model_demo():
86
- custom_css = """
87
- :root {
88
- --martian-orange: #FF6B4A;
89
- --martian-bg: #0E0E0E; /* deep black background */
90
- --martian-gray-dark: #3A3A3A;
91
- --martian-gray-medium: #4A4A4A;
92
- --martian-gray-light: #5A5A5A;
93
- }
94
-
95
- .gradio-container {
96
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
97
- background-color: var(--martian-bg) !important;
98
- }
99
-
100
- .header-section {
101
- text-align: center;
102
- padding: 3rem 2rem;
103
- background: linear-gradient(135deg, var(--martian-gray-dark) 0%, var(--martian-gray-medium) 100%);
104
- border-radius: 16px;
105
- margin-bottom: 2rem;
106
- color: white;
107
- box-shadow: 0 4px 6px rgba(0,0,0,0.3);
108
- }
109
-
110
- .header-section h1 { font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem; color: white; }
111
- .header-section .subtitle { font-size: 1.2rem; opacity: 0.9; line-height: 1.6; color: white; }
112
- .orange-accent { color: var(--martian-orange); font-weight: 600; }
113
-
114
- .info-box { background: var(--martian-gray-dark); border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; border-left: 4px solid var(--martian-orange); color: #E0E0E0; }
115
- .model-guide { background: var(--martian-gray-dark); border-radius: 8px; padding: 1rem; margin-top: 1rem; font-size: 0.9rem; color: #D0D0D0; }
116
-
117
- button.primary { background: var(--martian-orange) !important; border: none !important; color: white !important; }
118
- button.primary:hover { background: #FF5733 !important; }
119
-
120
- label { color: #D0D0D0 !important; }
121
- .label-wrap span { color: var(--martian-orange) !important; }
122
-
123
- input, textarea, select { background: var(--martian-gray-medium) !important; border-color: var(--martian-gray-light) !important; color: #E0E0E0 !important; }
124
- textarea::placeholder, input::placeholder { color: #888 !important; }
125
-
126
- .code { background: var(--martian-gray-dark) !important; color: #E0E0E0 !important; }
127
- """
128
-
129
- with gr.Blocks(css=custom_css, title="TinySQL Model Demo") as demo:
130
-
131
- # Header
132
- gr.HTML("""
133
- <div class="header-section">
134
- <h1>TinySQL Interactive Demo</h1>
135
- <p class="subtitle">
136
- Transform natural language into SQL queries using <span class="orange-accent">mechanistically interpretable</span> models
137
- </p>
138
- </div>
139
- """)
140
-
141
- # Info box
142
- gr.HTML("""
143
- <div class="info-box">
144
- <strong>How it works:</strong> Select a model, describe your query in plain English, and watch the model generate SQL.
145
- </div>
146
- """)
147
-
148
- with gr.Row():
149
- with gr.Column(scale=1):
150
- gr.Markdown("### Configuration")
151
- model_dropdown = gr.Dropdown(
152
- choices=list(MODELS.keys()),
153
- value="BM2_CS2_Syn (0.5B)",
154
- label="Model Selection",
155
- info="Larger models = better accuracy, slower inference"
156
- )
157
- gr.HTML("""
158
- <div class="model-guide">
159
- <strong>BM1 (33M)</strong> - Lightning fast, simple queries<br>
160
- <strong>BM2 (0.5B)</strong> - Balanced performance<br>
161
- <strong>BM3 (1B)</strong> - Most accurate, complex queries<br><br>
162
- <strong>Dataset Complexity:</strong><br>
163
- CS1: Basic SELECT-FROM<br>
164
- CS2: Adds ORDER BY<br>
165
- CS3: Aggregations<br>
166
- CS4: Adds WHERE filters<br>
167
- CS5: Multi-table JOINs
168
- </div>
169
- """)
170
-
171
- with gr.Column(scale=2):
172
- gr.Markdown("### Your Query")
173
- instruction = gr.Textbox(
174
- label="What do you want to know?",
175
- placeholder="e.g., Find all employees earning more than $50,000 sorted by name",
176
- lines=2
177
- )
178
- schema = gr.Textbox(
179
- label="Database Schema",
180
- placeholder="CREATE TABLE employees (name VARCHAR, salary INT, department VARCHAR)",
181
- lines=3,
182
- value="CREATE TABLE employees (name VARCHAR(100), salary INT, department VARCHAR(100))"
183
- )
184
- with gr.Row():
185
- max_length = gr.Slider(64, 512, value=256, step=32, label="Max Length")
186
- temperature = gr.Slider(0.0, 1.0, value=0.1, step=0.1, label="Temperature")
187
-
188
- generate_btn = gr.Button("Generate SQL", variant="primary", size="lg")
189
- output = gr.Code(label="Generated SQL Query", language="sql", lines=8)
190
-
191
- gr.Examples(examples=examples, inputs=[model_dropdown, instruction, schema])
192
-
193
- generate_btn.click(
194
- fn=generate_sql,
195
- inputs=[model_dropdown, instruction, schema, max_length, temperature],
196
- outputs=output
197
- )
198
-
199
- return demo
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
+ # Model configurations
6
  MODELS = {
7
  "BM1_CS1_Syn (33M)": "withmartian/sql_interp_bm1_cs1_experiment_1.10",
8
  "BM1_CS2_Syn (33M)": "withmartian/sql_interp_bm1_cs2_experiment_2.10",
 
62
  except Exception as e:
63
  return f"Error: {str(e)}"
64
 
 
65
  examples = [
66
  [
67
  "BM1_CS1_Syn (33M)",
 
80
  ],
81
  ]
82
 
83
+ def model_demo(shared_instruction, shared_schema):
84
+ """Model demo component that can receive examples from dataset viewer"""
85
+
86
+ gr.HTML("""
87
+ <div style="text-align: center; padding: 3rem 2rem; background: linear-gradient(135deg, #3A3A3A 0%, #4A4A4A 100%); border-radius: 16px; margin-bottom: 2rem; color: white;">
88
+ <h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem;">TinySQL Interactive Demo</h1>
89
+ <p style="font-size: 1.2rem; opacity: 0.9; line-height: 1.6;">
90
+ Transform natural language into SQL queries using <span style="color: #FF6B4A; font-weight: 600;">mechanistically interpretable</span> models
91
+ </p>
92
+ </div>
93
+ """)
94
+
95
+ gr.HTML("""
96
+ <div style="background: #3A3A3A; border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; border-left: 4px solid #FF6B4A; color: #E0E0E0;">
97
+ <strong>How it works:</strong> Select a model, describe your query in plain English, and watch the model generate SQL.
98
+ </div>
99
+ """)
100
+
101
+ with gr.Row():
102
+ with gr.Column(scale=1):
103
+ gr.Markdown("### Configuration")
104
+ model_dropdown = gr.Dropdown(
105
+ choices=list(MODELS.keys()),
106
+ value="BM2_CS2_Syn (0.5B)",
107
+ label="Model Selection",
108
+ info="Larger models = better accuracy, slower inference"
109
+ )
110
+ gr.HTML("""
111
+ <div style="background: #3A3A3A; border-radius: 8px; padding: 1rem; margin-top: 1rem; font-size: 0.9rem; color: #D0D0D0;">
112
+ <strong>BM1 (33M)</strong> - Lightning fast, simple queries<br>
113
+ <strong>BM2 (0.5B)</strong> - Balanced performance<br>
114
+ <strong>BM3 (1B)</strong> - Most accurate, complex queries<br><br>
115
+ <strong>Dataset Complexity:</strong><br>
116
+ CS1: Basic SELECT-FROM<br>
117
+ CS2: Adds ORDER BY<br>
118
+ CS3: Aggregations<br>
119
+ CS4: Adds WHERE filters<br>
120
+ CS5: Multi-table JOINs
121
+ </div>
122
+ """)
123
+
124
+ with gr.Column(scale=2):
125
+ gr.Markdown("### Your Query")
126
+ instruction = gr.Textbox(
127
+ label="What do you want to know?",
128
+ placeholder="e.g., Find all employees earning more than $50,000 sorted by name",
129
+ lines=2,
130
+ value=""
131
+ )
132
+ schema = gr.Textbox(
133
+ label="Database Schema",
134
+ placeholder="CREATE TABLE employees (name VARCHAR, salary INT, department VARCHAR)",
135
+ lines=3,
136
+ value="CREATE TABLE employees (name VARCHAR(100), salary INT, department VARCHAR(100))"
137
+ )
138
+
139
+ with gr.Row():
140
+ max_length = gr.Slider(64, 512, value=256, step=32, label="Max Length")
141
+ temperature = gr.Slider(0.0, 1.0, value=0.1, step=0.1, label="Temperature")
142
+
143
+ generate_btn = gr.Button("Generate SQL", variant="primary", size="lg")
144
+ output = gr.Code(label="Generated SQL Query", language="sql", lines=8)
145
+
146
+ gr.Markdown("### Example Queries")
147
+ gr.Examples(examples=examples, inputs=[model_dropdown, instruction, schema])
148
+
149
+ # Update instruction and schema from shared state when values change
150
+ def update_from_shared(shared_inst, shared_sch):
151
+ return shared_inst if shared_inst else "", shared_sch if shared_sch else ""
152
+
153
+ shared_instruction.change(
154
+ fn=lambda x: x,
155
+ inputs=shared_instruction,
156
+ outputs=instruction
157
+ )
158
+
159
+ shared_schema.change(
160
+ fn=lambda x: x,
161
+ inputs=shared_schema,
162
+ outputs=schema
163
+ )
164
+
165
+ generate_btn.click(
166
+ fn=generate_sql,
167
+ inputs=[model_dropdown, instruction, schema, max_length, temperature],
168
+ outputs=output
169
+ )
170
+
171
+ return {
172
+ 'instruction': instruction,
173
+ 'schema': schema,
174
+ 'output': output
175
+ }