BhumikaMak commited on
Commit
940b1b1
·
verified ·
1 Parent(s): 17e61c9

revert port

Browse files
Files changed (1) hide show
  1. app.py +24 -51
app.py CHANGED
@@ -159,55 +159,28 @@ 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)
 
159
  interface.launch(share=True)
160
  """
161
  import gradio as gr
 
162
  import os
163
+ import subprocess
164
+
165
+ # Path to the model file (change this to your actual model path)
166
+ MODEL_FILE = os.path.join(os.getcwd(), "weight_files/yolov5.onnx")
167
+
168
+ # Function to visualize the model using Netron
169
+ def visualize_model():
170
+ # Serve the model using Netron
171
+ netron_command = f"netron.start('{MODEL_FILE}', address='0.0.0.0', port=8081)"
172
+ subprocess.Popen(["python3", "-c", netron_command])
173
+ # Return the URL where the model is served
174
+ return f"http://localhost:8081"
175
+
176
+ # Gradio Interface
177
+ with gr.Blocks() as demo:
178
+ gr.Markdown("## Model Visualization with Netron")
179
+ output_link = gr.Textbox(label="Netron Model Link")
180
+ visualize_btn = gr.Button("Visualize Model")
181
+
182
+ # On button click, call the function and display the link
183
+ visualize_btn.click(fn=visualize_model, inputs=[], outputs=[output_link])
184
+
185
+ # Launch the Gradio app
186
+ demo.launch(server_name="0.0.0.0", server_port=7860, ssr=False)