Melika Kheirieh commited on
Commit
a5a91fa
·
1 Parent(s): e1a22fb

fix(demo): ensure Gradio UI launches on PORT alongside FastAPI

Browse files
Files changed (2) hide show
  1. demo/app.py +52 -53
  2. start.py +10 -3
demo/app.py CHANGED
@@ -89,12 +89,10 @@ def upload_db(file_obj):
89
  if size and size > 20 * 1024 * 1024:
90
  return None, "File too large (>20MB) for this demo."
91
 
92
- # Gradio gives a temp file path as file_obj.name
93
  files = {"file": (name, open(file_obj.name, "rb"), "application/octet-stream")}
94
  try:
95
  r = requests.post(API_UPLOAD, files=files, timeout=120)
96
  finally:
97
- # best-effort close
98
  try:
99
  files["file"][1].close()
100
  except Exception:
@@ -111,7 +109,6 @@ def upload_db(file_obj):
111
 
112
 
113
  def query_to_sql(user_query: str, db_id: str | None, _debug_flag: bool):
114
- """Unified query handler: tries backend or mock fallback."""
115
  if not user_query.strip():
116
  return "❌ Please enter a query.", "", "", {}, [], [], "", []
117
 
@@ -134,60 +131,62 @@ def query_to_sql(user_query: str, db_id: str | None, _debug_flag: bool):
134
  return badges_text, sql, explanation, result, trace_list, [], "", timings_table
135
 
136
 
137
- # ---- UI definition (unchanged) ----
138
- with gr.Blocks(title="NL2SQL Copilot") as demo:
139
- gr.Markdown("# NL2SQL Copilot\nUpload a SQLite DB (optional) or use default.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
- db_state = gr.State(value=None)
142
 
143
- with gr.Row():
144
- db_file = gr.File(
145
- label="Upload SQLite (.db/.sqlite)", file_types=[".db", ".sqlite"]
146
- )
147
- upload_btn = gr.Button("Upload DB")
148
- db_msg = gr.Markdown()
149
- upload_btn.click(upload_db, inputs=[db_file], outputs=[db_state, db_msg])
150
-
151
- with gr.Row():
152
- q = gr.Textbox(label="Question", scale=4)
153
- debug = gr.Checkbox(label="Debug (UI only)", value=True, scale=1)
154
- run = gr.Button("Run")
155
-
156
- badges = gr.Markdown()
157
- sql_out = gr.Code(label="Final SQL", language="sql")
158
- exp_out = gr.Textbox(label="Explanation", lines=3)
159
-
160
- with gr.Tab("Result"):
161
- res_out = gr.JSON()
162
-
163
- with gr.Tab("Trace"):
164
- trace = gr.JSON(label="Stage trace")
165
-
166
- with gr.Tab("Repair"):
167
- repair_candidates = gr.JSON(label="Candidates")
168
- repair_diff = gr.Textbox(label="Diff (if any)", lines=10)
169
-
170
- with gr.Tab("Timings"):
171
- timings = gr.Dataframe(headers=["metric", "ms"], datatype=["str", "number"])
172
-
173
- run.click(
174
- query_to_sql,
175
- inputs=[q, db_state, debug],
176
- outputs=[
177
- badges,
178
- sql_out,
179
- exp_out,
180
- res_out,
181
- trace,
182
- repair_candidates,
183
- repair_diff,
184
- timings,
185
- ],
186
- )
187
 
188
  if __name__ == "__main__":
189
- import os
190
-
191
  print("[demo] Launching Gradio demo on 0.0.0.0:7860 ...", flush=True)
192
  demo.launch(
193
  server_name=os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"),
 
89
  if size and size > 20 * 1024 * 1024:
90
  return None, "File too large (>20MB) for this demo."
91
 
 
92
  files = {"file": (name, open(file_obj.name, "rb"), "application/octet-stream")}
93
  try:
94
  r = requests.post(API_UPLOAD, files=files, timeout=120)
95
  finally:
 
96
  try:
97
  files["file"][1].close()
98
  except Exception:
 
109
 
110
 
111
  def query_to_sql(user_query: str, db_id: str | None, _debug_flag: bool):
 
112
  if not user_query.strip():
113
  return "❌ Please enter a query.", "", "", {}, [], [], "", []
114
 
 
131
  return badges_text, sql, explanation, result, trace_list, [], "", timings_table
132
 
133
 
134
+ def build_ui() -> gr.Blocks:
135
+ with gr.Blocks(title="NL2SQL Copilot") as demo:
136
+ gr.Markdown("# NL2SQL Copilot\nUpload a SQLite DB (optional) or use default.")
137
+ db_state = gr.State(value=None)
138
+
139
+ with gr.Row():
140
+ db_file = gr.File(
141
+ label="Upload SQLite (.db/.sqlite)", file_types=[".db", ".sqlite"]
142
+ )
143
+ upload_btn = gr.Button("Upload DB")
144
+ db_msg = gr.Markdown()
145
+ upload_btn.click(upload_db, inputs=[db_file], outputs=[db_state, db_msg])
146
+
147
+ with gr.Row():
148
+ q = gr.Textbox(label="Question", scale=4)
149
+ debug = gr.Checkbox(label="Debug (UI only)", value=True, scale=1)
150
+ run = gr.Button("Run")
151
+
152
+ badges = gr.Markdown()
153
+ sql_out = gr.Code(label="Final SQL", language="sql")
154
+ exp_out = gr.Textbox(label="Explanation", lines=3)
155
+
156
+ with gr.Tab("Result"):
157
+ res_out = gr.JSON()
158
+
159
+ with gr.Tab("Trace"):
160
+ trace = gr.JSON(label="Stage trace")
161
+
162
+ with gr.Tab("Repair"):
163
+ repair_candidates = gr.JSON(label="Candidates")
164
+ repair_diff = gr.Textbox(label="Diff (if any)", lines=10)
165
+
166
+ with gr.Tab("Timings"):
167
+ timings = gr.Dataframe(headers=["metric", "ms"], datatype=["str", "number"])
168
+
169
+ run.click(
170
+ query_to_sql,
171
+ inputs=[q, db_state, debug],
172
+ outputs=[
173
+ badges,
174
+ sql_out,
175
+ exp_out,
176
+ res_out,
177
+ trace,
178
+ repair_candidates,
179
+ repair_diff,
180
+ timings,
181
+ ],
182
+ )
183
+ return demo
184
 
 
185
 
186
+ # expose for SDK mode (no Docker)
187
+ demo = build_ui()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  if __name__ == "__main__":
 
 
190
  print("[demo] Launching Gradio demo on 0.0.0.0:7860 ...", flush=True)
191
  demo.launch(
192
  server_name=os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"),
start.py CHANGED
@@ -22,9 +22,16 @@ def run_fastapi():
22
 
23
 
24
  def run_gradio():
25
- """Launch Gradio UI (demo/app.py)."""
26
- import demo.app # noqa: F401
27
- # This module runs demo.launch() on import
 
 
 
 
 
 
 
28
 
29
 
30
  if __name__ == "__main__":
 
22
 
23
 
24
  def run_gradio():
25
+ """Build and launch Gradio UI on the Space's PORT."""
26
+ from demo.app import build_ui
27
+
28
+ demo = build_ui()
29
+ demo.launch(
30
+ server_name="0.0.0.0",
31
+ server_port=int(os.getenv("PORT", "7860")),
32
+ show_api=False,
33
+ debug=False,
34
+ )
35
 
36
 
37
  if __name__ == "__main__":