Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -159,49 +159,63 @@ if __name__ == "__main__":
|
|
159 |
interface.launch(share=True)
|
160 |
"""
|
161 |
import gradio as gr
|
162 |
-
import
|
|
|
|
|
163 |
import tempfile
|
164 |
-
import
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
# Run
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
# Gradio function to handle file upload
|
194 |
def visualize_model(file):
|
195 |
if file:
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
return "<p>Please upload a valid model file.</p>"
|
198 |
|
199 |
|
200 |
-
# Gradio
|
201 |
with gr.Blocks() as demo:
|
202 |
-
gr.Markdown("## Netron Model
|
203 |
-
file_input = gr.File(label="Upload Model File (ONNX, TFLite, etc.)")
|
204 |
-
output_html = gr.HTML(label="
|
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
|