Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
164 |
import os
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
# Launch the Gradio app
|
195 |
-
|
|
|
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)
|