BhumikaMak commited on
Commit
448ae04
·
verified ·
1 Parent(s): e015d4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -160,23 +160,40 @@ if __name__ == "__main__":
160
  """
161
  import gradio as gr
162
  import netron
 
 
163
 
164
- # Function to visualize the model using Netron
165
- def visualize_model(model_path):
166
- # Start Netron to visualize the model
167
- netron.start(model_path, browse=False) # Set browse=False to avoid opening a new browser tab
168
-
169
- # Specify the local path to your model file
170
- model_path = os.path.join(os.getcwd(), "weight_files/yolov5.onnx") # Change this to your actual model file path
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  # Create Gradio interface
173
- iface = gr.Interface(
174
- fn=lambda: visualize_model(model_path), # Call the function without user input
175
- inputs=None, # No input required from user
176
- outputs="text", # Output type is text (or you can use "html" if needed)
177
- title="Model Visualization with Netron",
178
- description="This app visualizes a model using Netron."
179
- )
180
-
181
- # Launch the app
182
- iface.launch()
 
 
 
 
 
160
  """
161
  import gradio as gr
162
  import netron
163
+ import os
164
+ import threading
165
 
166
+ # Function to launch Netron in a separate thread
167
+ def launch_netron(model_path):
168
+ # Start Netron server for the model file
169
+ netron.start(model_path)
170
+
171
+ # Function to handle file upload and launch Netron
172
+ def visualize_model(model_file):
173
+ # Save the uploaded model file temporarily
174
+ model_path = os.path.join("temp_models", model_file.name)
175
+ os.makedirs("temp_models", exist_ok=True)
176
+
177
+ with open(model_path, "wb") as f:
178
+ f.write(model_file.read())
179
+
180
+ # Launch Netron in a separate thread
181
+ threading.Thread(target=launch_netron, args=(model_path,), daemon=True).start()
182
+
183
+ return f"Netron is running for {model_file.name}. Open your browser at http://localhost:8080."
184
 
185
  # Create Gradio interface
186
+ with gr.Blocks() as interface:
187
+ gr.Markdown("<h1 style='color: lightgrey;'>Model Visualization with Netron</h1>")
188
+ gr.Markdown("<p style='color: lightgrey;'>Upload your model file (e.g., .onnx, .pb) to visualize it.</p>")
189
+
190
+ model_input = gr.File(label="Upload Model File", type="file")
191
+ output_text = gr.Textbox(label="Output", interactive=False)
192
+
193
+ run_button = gr.Button("Visualize Model")
194
+
195
+ run_button.click(fn=visualize_model, inputs=model_input, outputs=output_text)
196
+
197
+ # Launch Gradio app
198
+ interface.launch(share=True)
199
+