BhumikaMak commited on
Commit
91e8379
·
verified ·
1 Parent(s): 074e593

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
app.py CHANGED
@@ -158,38 +158,52 @@ with gr.Blocks(css=custom_css) as interface:
158
  if __name__ == "__main__":
159
  interface.launch(share=True)
160
  """
161
-
162
  import gradio as gr
163
- import onnxruntime
164
  import os
165
-
166
- def visualize_onnx_model(onnx_model_path):
167
- """
168
- Visualizes the given ONNX model using Netron.
169
-
170
- Args:
171
- onnx_model_path: The path to the ONNX model file.
172
- """
173
- try:
174
- # Save the ONNX model to a temporary file (optional, but can be helpful)
175
- temp_model_path = "temp_model.onnx"
176
- with open(temp_model_path, "wb") as f:
177
- f.write(onnx_model_path.read()) # Assuming onnx_model_path is a file-like object
178
-
179
- # Run Netron
180
- os.system(f"netron {temp_model_path}")
181
-
182
- except Exception as e:
183
- print(f"Error visualizing model: {e}")
184
-
185
- # Create the Gradio interface
186
- iface = gr.Interface(
187
- fn=visualize_onnx_model,
188
- inputs="file", # Accept the ONNX model as a file upload
189
- outputs="text",
190
- title="Netron ONNX Model Visualization",
191
- description="Upload an ONNX model file to visualize with Netron."
192
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
  # Launch the Gradio app
195
- iface.launch(share=True)
 
158
  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
168
+ def start_netron_server(model_file, port=8080):
169
+ if not os.path.exists(model_file):
170
+ return "Error: Model file does not exist."
171
+
172
+ # Start Netron in a thread
173
+ thread = threading.Thread(target=netron.start, args=(model_file,), kwargs={"port": port, "host": "0.0.0.0"}, daemon=True)
174
+ thread.start()
175
+
176
+ # Wait for the server to initialize
177
+ time.sleep(2)
178
+
179
+ # Function to return the Netron viewer iframe
180
+ def visualize_model(file):
181
+ if file:
182
+ file_path = file.name
183
+ port = 8080 # Port for Netron server
184
+ start_netron_server(file_path, port)
185
+
186
+ iframe_url = f"https://bhumikamak-neuralvista.hf.space:{port}/"
187
+
188
+ # Embed the Netron viewer using iframe
189
+ iframe_html = f"""
190
+ <iframe
191
+ src="{iframe_url}"
192
+ width="100%"
193
+ height="800"
194
+ frameborder="0">
195
+ </iframe>
196
+ """
197
+ return iframe_html
198
+
199
+ return "<p>Please upload a valid model file.</p>"
200
+
201
+ # Gradio Interface
202
+ with gr.Blocks() as demo:
203
+ gr.Markdown("## Netron Model Visualization on Hugging Face Spaces")
204
+ file_input = gr.File(label="Upload Model File (ONNX, TFLite, etc.)")
205
+ output_html = gr.HTML(label="Netron Viewer")
206
+ file_input.upload(visualize_model, file_input, output_html)
207
 
208
  # Launch the Gradio app
209
+ demo.launch(server_name="0.0.0.0", server_port=7860)