tengomucho HF Staff commited on
Commit
06e24a5
·
verified ·
1 Parent(s): bc7e77f

feat: allow image loading and avoid CORS error

Browse files

When loading with some browsers (e.g. Firefox), there is an error loading the image because it is an external image. This change is a workaround to make it work.

Files changed (1) hide show
  1. app.py +36 -8
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import csv
2
  import os
 
3
  from datetime import datetime
4
  from typing import Optional, Union, List
5
  import gradio as gr
@@ -209,12 +210,6 @@ def neuron_export(model_id: str, model_type: str, pipeline_name: str, task_or_pi
209
  except Exception as e:
210
  yield log(f"❗ An unexpected error occurred in the Gradio interface: {e}")
211
 
212
- TITLE_IMAGE = """
213
- <div style="display: block; margin-left: auto; margin-right: auto; width: 50%;">
214
- <img src="https://huggingface.co/spaces/optimum/neuron-export/resolve/main/huggingfaceXneuron.png"/>
215
- </div>
216
- """
217
-
218
  TITLE = """
219
  <div style="text-align: center; max-width: 1400px; margin: 0 auto;">
220
  <h1 style="font-weight: 900; margin-bottom: 10px; margin-top: 10px; font-size: 2.2rem;">
@@ -309,13 +304,46 @@ LOADING_MESSAGE = """
309
  <div id="in_progress"><span class="loader">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;Model export in progress...</div>
310
  """
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft()) as demo:
313
  gr.Markdown("**You must be logged in to use this space**")
314
  gr.LoginButton(elem_id="login-button", elem_classes="center-button", min_width=250)
315
- gr.HTML(TITLE_IMAGE)
 
316
  gr.HTML(TITLE)
317
  gr.Markdown(DESCRIPTION)
318
-
319
  with gr.Tabs():
320
  with gr.Tab("Export Model"):
321
  with gr.Group():
 
1
  import csv
2
  import os
3
+ import base64
4
  from datetime import datetime
5
  from typing import Optional, Union, List
6
  import gradio as gr
 
210
  except Exception as e:
211
  yield log(f"❗ An unexpected error occurred in the Gradio interface: {e}")
212
 
 
 
 
 
 
 
213
  TITLE = """
214
  <div style="text-align: center; max-width: 1400px; margin: 0 auto;">
215
  <h1 style="font-weight: 900; margin-bottom: 10px; margin-top: 10px; font-size: 2.2rem;">
 
304
  <div id="in_progress"><span class="loader">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;Model export in progress...</div>
305
  """
306
 
307
+ # Download the title image at startup and encode as base64 to avoid CORS errors
308
+ def get_title_image_html():
309
+ """Download image and return HTML with base64 encoded image"""
310
+ image_data = None
311
+
312
+ # Download image
313
+ try:
314
+ # Use huggingface_hub to download the file (handles auth automatically)
315
+ from huggingface_hub import hf_hub_download
316
+ downloaded_path = hf_hub_download(
317
+ repo_id="optimum/neuron-exporter",
318
+ filename="huggingfaceXneuron.png",
319
+ repo_type="space"
320
+ )
321
+ # Read directly from downloaded path
322
+ with open(downloaded_path, 'rb') as f:
323
+ image_data = f.read()
324
+ except Exception as e:
325
+ print(f"Warning: Could not download title image: {e}")
326
+ return "" # Return empty if download fails
327
+
328
+ # Encode as base64
329
+ if image_data:
330
+ encoded = base64.b64encode(image_data).decode('utf-8')
331
+ return f"""
332
+ <div style="display: block; margin-left: auto; margin-right: auto; width: 50%;">
333
+ <img src="data:image/png;base64,{encoded}"/>
334
+ </div>
335
+ """
336
+ return ""
337
+
338
+
339
  with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft()) as demo:
340
  gr.Markdown("**You must be logged in to use this space**")
341
  gr.LoginButton(elem_id="login-button", elem_classes="center-button", min_width=250)
342
+ title_image = get_title_image_html()
343
+ gr.HTML(title_image)
344
  gr.HTML(TITLE)
345
  gr.Markdown(DESCRIPTION)
346
+
347
  with gr.Tabs():
348
  with gr.Tab("Export Model"):
349
  with gr.Group():