Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -159,41 +159,42 @@ if __name__ == "__main__":
|
|
159 |
interface.launch(share=True)
|
160 |
"""
|
161 |
import gradio as gr
|
162 |
-
import
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
interface.launch(share=True)
|
160 |
"""
|
161 |
import gradio as gr
|
162 |
+
import torch
|
163 |
+
from ultralytics import YOLO
|
164 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
def visualize_yolov5(model_path):
|
167 |
+
"""
|
168 |
+
Visualizes the given YOLOv5 model using Netron.
|
169 |
+
|
170 |
+
Args:
|
171 |
+
model_path: The path to the YOLOv5 model directory.
|
172 |
+
"""
|
173 |
+
try:
|
174 |
+
# Load the YOLOv5 model
|
175 |
+
model = YOLO(model_path)
|
176 |
+
|
177 |
+
# Extract the PyTorch model
|
178 |
+
pytorch_model = model.model
|
179 |
+
|
180 |
+
# Save the PyTorch model to a temporary file
|
181 |
+
temp_model_path = "temp_model"
|
182 |
+
torch.save(pytorch_model.state_dict(), os.path.join(temp_model_path, "pytorch_model.bin"))
|
183 |
+
|
184 |
+
# Run Netron
|
185 |
+
os.system(f"netron {temp_model_path}")
|
186 |
+
|
187 |
+
except Exception as e:
|
188 |
+
print(f"Error visualizing model: {e}")
|
189 |
+
|
190 |
+
# Create the Gradio interface
|
191 |
+
iface = gr.Interface(
|
192 |
+
fn=visualize_yolov5,
|
193 |
+
inputs="text",
|
194 |
+
outputs="text",
|
195 |
+
title="Netron YOLOv5 Model Visualization",
|
196 |
+
description="Enter the path to your YOLOv5 model directory."
|
197 |
+
)
|
198 |
+
|
199 |
+
# Launch the Gradio app
|
200 |
+
iface.launch(share=True)
|