Spaces:
Runtime error
Runtime error
File size: 1,533 Bytes
5fbf285 3b37ceb 5fbf285 e37f9bc 5fbf285 3b37ceb 5fbf285 8b39564 5fbf285 27862e1 5fbf285 3b37ceb 5fbf285 3b37ceb 40634f4 3b37ceb 5fbf285 4d9f8ad 5fbf285 3b37ceb e37f9bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import os
import torch
# CRITICAL: Redirect cache to temporary storage to avoid Hugging Face Space eviction
os.environ['TORCH_HOME'] = '/tmp/torch_cache'
os.environ['HUB_DIR'] = '/tmp/torch_hub'
os.environ['TMPDIR'] = '/tmp'
# Added to fix the Ultralytics config warning:
os.environ['YOLO_CONFIG_DIR'] = '/tmp/yolo_config'
torch.hub.set_dir('/tmp/torch_hub')
import gradio as gr
from ultralytics import YOLO
# Load the model
model_path = "OceanCV_FirstPass.pt"
model = YOLO(model_path)
def run(image_path, conf, iou):
# Predict using the slider values
# Note: 'classes=0' is kept from your baseline template
results = model.predict(image_path, conf=conf, iou=iou, classes=0)
# Reverse channels from BGR (OpenCV/YOLO default) to RGB (Gradio expectation)
return results[0].plot()[:, :, ::-1]
title = "OceanCV First Pass Detector"
description = "Upload an image to detect objects using the first pass model."
# Build the interface with interactive sliders
interface = gr.Interface(
fn=run,
inputs=[
gr.Image(type="filepath", label="Upload Image"),
gr.Slider(minimum=0.05, maximum=1.0, value=0.20, step=0.05, label="Confidence Threshold"),
gr.Slider(minimum=0.05, maximum=1.0, value=0.85, step=0.05, label="IoU Threshold")
],
outputs=gr.Image(type="numpy", label="Detections"),
title=title,
description=description
)
# Launch the app with server_name and port explicitly set for HF Spaces
interface.queue().launch(server_name="0.0.0.0", server_port=7860) |