Spaces:
Sleeping
Sleeping
dynamic port allocation
Browse files
app.py
CHANGED
@@ -158,41 +158,56 @@ 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 netron
|
164 |
import os
|
165 |
import threading
|
|
|
|
|
166 |
|
167 |
-
|
168 |
-
def visualize_model(model_file):
|
169 |
-
# If the model file is a path (file uploaded), read it
|
170 |
-
if isinstance(model_file, str):
|
171 |
-
model_path = model_file
|
172 |
-
else:
|
173 |
-
# If it's a NamedString (new behavior), save it to a temporary file
|
174 |
-
temp_model_path = f"./temp_model.{model_file.name.split('.')[-1]}"
|
175 |
-
with open(temp_model_path, "wb") as f:
|
176 |
-
f.write(model_file.encode()) # Use encode() to write the string content
|
177 |
-
model_path = temp_model_path
|
178 |
|
179 |
-
|
180 |
-
|
|
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
return iframe_html
|
185 |
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
interface = gr.Interface(
|
188 |
fn=visualize_model,
|
189 |
-
inputs=gr.File(label="Upload Model File"), #
|
190 |
-
outputs=gr.HTML(), # Output
|
191 |
live=True
|
192 |
)
|
193 |
|
194 |
-
# Launch Gradio
|
195 |
-
interface.launch(server_name="0.0.0.0", server_port=7860
|
196 |
-
|
197 |
-
|
198 |
-
|
|
|
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 logging
|
166 |
+
import time
|
167 |
|
168 |
+
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
+
# Store the last Netron thread and port
|
171 |
+
last_thread = None
|
172 |
+
last_port = None
|
173 |
|
174 |
+
def visualize_model(model_file):
|
175 |
+
global last_thread, last_port
|
|
|
176 |
|
177 |
+
try:
|
178 |
+
# Generate a temporary file for the model
|
179 |
+
temp_model_path = f"./temp_model.{model_file.name.split('.')[-1]}"
|
180 |
+
with open(temp_model_path, "wb") as f:
|
181 |
+
f.write(model_file.read())
|
182 |
+
|
183 |
+
# Stop previous Netron server if running
|
184 |
+
if last_port:
|
185 |
+
logging.info(f"Stopping Netron on port {last_port}")
|
186 |
+
netron.stop()
|
187 |
+
time.sleep(1) # Small delay for proper shutdown
|
188 |
+
|
189 |
+
# Start a new Netron server on a dynamic port
|
190 |
+
new_port = 8081 + int(time.time()) % 1000 # Avoid conflicts
|
191 |
+
logging.info(f"Starting Netron on port {new_port}")
|
192 |
+
last_port = new_port
|
193 |
+
last_thread = threading.Thread(target=netron.start, args=(temp_model_path, new_port))
|
194 |
+
last_thread.start()
|
195 |
+
|
196 |
+
# Return iframe with dynamic port
|
197 |
+
iframe_html = f'<iframe src="http://localhost:{new_port}" width="800" height="600"></iframe>'
|
198 |
+
return iframe_html
|
199 |
+
|
200 |
+
except Exception as e:
|
201 |
+
logging.error(f"Error in visualization: {e}")
|
202 |
+
return "An error occurred while visualizing the model."
|
203 |
+
|
204 |
+
# Create Gradio interface
|
205 |
interface = gr.Interface(
|
206 |
fn=visualize_model,
|
207 |
+
inputs=gr.File(label="Upload Model File"), # Input as a model file
|
208 |
+
outputs=gr.HTML(), # Output the embedded Netron visualization
|
209 |
live=True
|
210 |
)
|
211 |
|
212 |
+
# Launch Gradio server on a fixed port
|
213 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|