Jellyfish042 Claude Opus 4.5 commited on
Commit
91f5d7c
·
1 Parent(s): 1a54ca4

Fix Gradio API schema bug by replacing gr.File with gr.DownloadButton

Browse files

- Replace gr.File with gr.DownloadButton to avoid API schema generation bug
- Restructure download logic to work with DownloadButton

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Files changed (2) hide show
  1. .claude/settings.local.json +2 -1
  2. app.py +29 -15
.claude/settings.local.json CHANGED
@@ -5,7 +5,8 @@
5
  "Bash(git add:*)",
6
  "Bash(git remote add:*)",
7
  "Bash(git push:*)",
8
- "Bash(git branch:*)"
 
9
  ]
10
  }
11
  }
 
5
  "Bash(git add:*)",
6
  "Bash(git remote add:*)",
7
  "Bash(git push:*)",
8
+ "Bash(git branch:*)",
9
+ "Bash(git commit -m \"$\\(cat <<''EOF''\nFix Gradio compatibility for HuggingFace Spaces\n\n- Upgrade gradio to >=5.0.0 to fix API schema bug\n- Add server_name and server_port to demo.launch\\(\\)\n\nCo-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>\nEOF\n\\)\")"
10
  ]
11
  }
12
  }
app.py CHANGED
@@ -243,19 +243,13 @@ def run_evaluation(text: str, progress=gr.Progress()):
243
  # Wrap HTML for iframe display
244
  wrapped_html = wrap_html_in_iframe(html)
245
 
246
- # Save HTML for download
247
- temp_file = tempfile.NamedTemporaryFile(
248
- mode='w',
249
- suffix='.html',
250
- delete=False,
251
- encoding='utf-8'
252
- )
253
- temp_file.write(html)
254
- temp_file.close()
255
 
256
  progress(1.0, desc="Done!")
257
 
258
- return wrapped_html, temp_file.name
259
 
260
  except torch.cuda.OutOfMemoryError:
261
  if torch.cuda.is_available():
@@ -275,7 +269,11 @@ def run_evaluation(text: str, progress=gr.Progress()):
275
 
276
  def clear_inputs():
277
  """Clear all inputs and outputs."""
278
- return "", None, None
 
 
 
 
279
 
280
 
281
  # Build Gradio UI
@@ -322,7 +320,7 @@ with gr.Blocks(
322
  with gr.Row():
323
  with gr.Column():
324
  output_html = gr.HTML(label="Visualization")
325
- download_file = gr.File(label="Download HTML", visible=True)
326
 
327
  # Event handlers
328
  news_btn.click(fn=lambda: EXAMPLE_NEWS, outputs=[text_input])
@@ -331,13 +329,29 @@ with gr.Blocks(
331
 
332
  clear_btn.click(
333
  fn=clear_inputs,
334
- outputs=[text_input, output_html, download_file]
335
  )
336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
  run_btn.click(
338
- fn=run_evaluation,
339
  inputs=[text_input],
340
- outputs=[output_html, download_file]
341
  )
342
 
343
  gr.Markdown("""
 
243
  # Wrap HTML for iframe display
244
  wrapped_html = wrap_html_in_iframe(html)
245
 
246
+ # Store HTML for download
247
+ global _last_html_content
248
+ _last_html_content = html
 
 
 
 
 
 
249
 
250
  progress(1.0, desc="Done!")
251
 
252
+ return wrapped_html
253
 
254
  except torch.cuda.OutOfMemoryError:
255
  if torch.cuda.is_available():
 
269
 
270
  def clear_inputs():
271
  """Clear all inputs and outputs."""
272
+ return "", None
273
+
274
+
275
+ # Global variable to store the last generated HTML for download
276
+ _last_html_content = None
277
 
278
 
279
  # Build Gradio UI
 
320
  with gr.Row():
321
  with gr.Column():
322
  output_html = gr.HTML(label="Visualization")
323
+ download_btn = gr.DownloadButton("📥 Download HTML", visible=True)
324
 
325
  # Event handlers
326
  news_btn.click(fn=lambda: EXAMPLE_NEWS, outputs=[text_input])
 
329
 
330
  clear_btn.click(
331
  fn=clear_inputs,
332
+ outputs=[text_input, output_html]
333
  )
334
 
335
+ def run_and_prepare_download(text, progress=gr.Progress()):
336
+ """Run evaluation and prepare download file."""
337
+ wrapped_html = run_evaluation(text, progress)
338
+
339
+ # Save HTML for download
340
+ temp_file = tempfile.NamedTemporaryFile(
341
+ mode='w',
342
+ suffix='.html',
343
+ delete=False,
344
+ encoding='utf-8'
345
+ )
346
+ temp_file.write(_last_html_content)
347
+ temp_file.close()
348
+
349
+ return wrapped_html, temp_file.name
350
+
351
  run_btn.click(
352
+ fn=run_and_prepare_download,
353
  inputs=[text_input],
354
+ outputs=[output_html, download_btn]
355
  )
356
 
357
  gr.Markdown("""