BhumikaMak commited on
Commit
a83c2f4
·
verified ·
1 Parent(s): 2201a07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -36
app.py CHANGED
@@ -159,59 +159,50 @@ if __name__ == "__main__":
159
  interface.launch(share=True)
160
  """
161
  import gradio as gr
162
- import netron
163
  import os
164
- import threading
165
- import time
166
 
167
- # Function to start Netron server and get its port
168
- def start_netron_server(model_file):
 
169
  if not os.path.exists(model_file):
170
- return "Error: Model file does not exist."
171
-
172
- # Start Netron in a thread
173
- def run_netron():
174
- netron.start(model_file, browse=False) # Prevent opening in a local browser
175
-
176
- thread = threading.Thread(target=run_netron, daemon=True)
177
- thread.start()
178
 
179
- # Wait for the server to start
180
- time.sleep(2)
181
 
182
- # Netron typically uses port 8080 by default, but you may need to confirm this dynamically
183
- port = 8080
184
- return port
185
-
186
- # Gradio function to visualize the model
187
- def visualize_model(file):
188
- if file:
189
- file_path = file.name
190
- port = start_netron_server(file_path)
191
-
192
- # Construct the iframe URL using Hugging Face's public domain
193
- space_url = os.getenv("SPACE_ID", "your-space-id") # Replace 'your-space-id' with actual ID if testing locally
194
- iframe_url = f"https://bhumikamak-neuralvista.hf.space:{port}/"
195
-
196
- # Embed the iframe into the Gradio app
197
- iframe_html = f"""
198
  <iframe
199
- src="{iframe_url}"
200
  width="100%"
201
  height="800"
202
  frameborder="0">
203
  </iframe>
204
  """
205
- return iframe_html
206
-
 
 
 
 
 
207
  return "<p>Please upload a valid model file.</p>"
208
 
 
209
  # Gradio Interface
210
  with gr.Blocks() as demo:
211
- gr.Markdown("## Netron Model Visualization on Hugging Face Spaces")
212
  file_input = gr.File(label="Upload Model File (ONNX, TFLite, etc.)")
213
  output_html = gr.HTML(label="Netron Viewer")
214
  file_input.upload(visualize_model, file_input, output_html)
215
 
216
  # Launch the Gradio app
217
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
159
  interface.launch(share=True)
160
  """
161
  import gradio as gr
 
162
  import os
163
+ import tempfile
164
+ import subprocess
165
 
166
+
167
+ # Function to generate a Netron static visualization
168
+ def generate_netron_static(model_file):
169
  if not os.path.exists(model_file):
170
+ return "<p>Error: Model file does not exist.</p>"
171
+
172
+ # Create a temporary directory to save the static Netron files
173
+ output_dir = tempfile.mkdtemp()
 
 
 
 
174
 
175
+ # Run the Netron CLI to export the visualization to static files
176
+ subprocess.run(["netron", "--export", output_dir, model_file], check=True)
177
 
178
+ # Create a static HTML viewer from the output directory
179
+ static_html_path = os.path.join(output_dir, "index.html")
180
+ if os.path.exists(static_html_path):
181
+ # Embed the static HTML in an iframe
182
+ return f"""
 
 
 
 
 
 
 
 
 
 
 
183
  <iframe
184
+ srcdoc="{open(static_html_path).read()}"
185
  width="100%"
186
  height="800"
187
  frameborder="0">
188
  </iframe>
189
  """
190
+ return "<p>Error: Unable to generate static visualization.</p>"
191
+
192
+
193
+ # Gradio function to handle file upload
194
+ def visualize_model(file):
195
+ if file:
196
+ return generate_netron_static(file.name)
197
  return "<p>Please upload a valid model file.</p>"
198
 
199
+
200
  # Gradio Interface
201
  with gr.Blocks() as demo:
202
+ gr.Markdown("## Netron Model Visualization with Gradio")
203
  file_input = gr.File(label="Upload Model File (ONNX, TFLite, etc.)")
204
  output_html = gr.HTML(label="Netron Viewer")
205
  file_input.upload(visualize_model, file_input, output_html)
206
 
207
  # Launch the Gradio app
208
+ demo.launch()