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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -36
app.py CHANGED
@@ -159,49 +159,63 @@ if __name__ == "__main__":
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
 
159
  interface.launch(share=True)
160
  """
161
  import gradio as gr
162
+ import netron
163
+ import threading
164
+ import time
165
  import tempfile
166
+ import os
167
+
168
+ # Function to start a Netron server and get the file-based URL
169
+ def start_netron_server(file_path):
170
+ if not os.path.exists(file_path):
171
+ return None
172
+
173
+ # Temporary directory to store Netron visualizations
174
+ temp_dir = tempfile.mkdtemp()
175
+ output_path = os.path.join(temp_dir, "model_view.html")
176
+
177
+ # Run Netron in a separate thread with export option
178
+ def run_netron():
179
+ netron.start(file_path, browse=False, output=output_path)
180
+
181
+ thread = threading.Thread(target=run_netron, daemon=True)
182
+ thread.start()
183
+
184
+ # Wait for Netron to finish initializing
185
+ time.sleep(2)
186
+
187
+ # Ensure the exported file exists
188
+ if os.path.exists(output_path):
189
+ return f"file://{output_path}"
190
+ else:
191
+ return None
192
+
193
+
194
+ # Gradio function to display the model viewer
 
195
  def visualize_model(file):
196
  if file:
197
+ file_path = file.name
198
+ netron_url = start_netron_server(file_path)
199
+ if netron_url:
200
+ # Embed the Netron viewer using an iframe with the generated URL
201
+ iframe_html = f"""
202
+ <iframe
203
+ src="{netron_url}"
204
+ width="100%"
205
+ height="800"
206
+ frameborder="0">
207
+ </iframe>
208
+ """
209
+ return iframe_html
210
+ return "<p>Error: Unable to generate Netron visualization.</p>"
211
  return "<p>Please upload a valid model file.</p>"
212
 
213
 
214
+ # Gradio app
215
  with gr.Blocks() as demo:
216
+ gr.Markdown("## Netron Model Viewer with Gradio")
217
+ file_input = gr.File(label="Upload a Model File (e.g., ONNX, TFLite, etc.)")
218
+ output_html = gr.HTML(label="Model Visualization")
219
  file_input.upload(visualize_model, file_input, output_html)
220
 
221
  # Launch the Gradio app