rayh commited on
Commit
8fd2d1a
·
0 Parent(s):

Deploy latest YOLO model and app (version 20250422.7)

Browse files
Files changed (5) hide show
  1. README.md +28 -0
  2. VERSION +1 -0
  3. app.py +51 -0
  4. deploy.sh +40 -0
  5. requirements.txt +5 -0
README.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # YOLO Segmentation Model Server (Gradio)
2
+
3
+ This subproject provides a Gradio web app and API endpoint for your YOLO segmentation model.
4
+
5
+ ## Quickstart
6
+
7
+ 1. Install dependencies:
8
+
9
+ ```bash
10
+ pip install -r requirements.txt
11
+ ```
12
+
13
+ 2. Run the server locally:
14
+
15
+ ```bash
16
+ python app.py
17
+ ```
18
+
19
+ 3. Deploy to [Hugging Face Spaces](https://huggingface.co/spaces):
20
+ - Push this directory to a new Space as a Gradio app.
21
+
22
+ ## Integration
23
+ - Replace the dummy `segment` function in `app.py` with your YOLO model inference code.
24
+ - The API will accept an image and return a segmentation mask (as an image).
25
+
26
+ ## Notes
27
+ - Add any additional dependencies to `requirements.txt` as needed.
28
+ - For Spaces, ensure your model weights are included or downloadable.
VERSION ADDED
@@ -0,0 +1 @@
 
 
1
+ 20250422.7
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from pathlib import Path
4
+ import numpy as np
5
+ import torch
6
+ from ultralytics import YOLO
7
+ import os
8
+
9
+ MODEL_WEIGHTS_PATH = Path("weights/best.pt") # Path to model weights (populated by deploy.sh)
10
+ VERSION_PATH = Path("VERSION")
11
+
12
+ # Read version string from VERSION file
13
+ try:
14
+ VERSION = VERSION_PATH.read_text().strip()
15
+ except Exception:
16
+ VERSION = "unknown"
17
+
18
+ # Lazy-load model (singleton)
19
+ model = None
20
+ def get_model():
21
+ global model
22
+ if model is None:
23
+ if not MODEL_WEIGHTS_PATH.exists():
24
+ raise FileNotFoundError(f"Model weights not found at {MODEL_WEIGHTS_PATH}. Please deploy weights before running.")
25
+ model = YOLO(str(MODEL_WEIGHTS_PATH))
26
+ return model
27
+
28
+ def segment(image: Image.Image):
29
+ model = get_model()
30
+ img_np = np.array(image)
31
+ # Run prediction
32
+ results = model(img_np)
33
+ if not results or not hasattr(results[0], "masks") or results[0].masks is None:
34
+ mask_img = Image.new("L", image.size, 0) # Blank mask if no detections
35
+ else:
36
+ mask = results[0].masks.data[0].cpu().numpy() # (H, W) binary mask
37
+ mask_img = Image.fromarray((mask * 255).astype(np.uint8))
38
+ mask_img = mask_img.resize(image.size) # Ensure mask matches input size
39
+ # Return both the mask and version in the API response
40
+ return {"mask": mask_img, "version": VERSION}
41
+
42
+ iface = gr.Interface(
43
+ fn=segment,
44
+ inputs=gr.Image(type="pil"),
45
+ outputs=[gr.Image(type="pil", label="Segmentation Mask"), gr.Textbox(label="Model Version")],
46
+ title=f"YOLO Segmentation Model (version: {VERSION})",
47
+ description=f"Upload an image to get a segmentation mask. Model version: {VERSION}"
48
+ )
49
+
50
+ if __name__ == "__main__":
51
+ iface.launch()
deploy.sh ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # Usage: ./deploy.sh <path_to_latest_yolo_train_dir> <hf_space_git_url>
3
+ # Example: ./deploy.sh ../yolo-output/models/20250422.7/ https://huggingface.co/spaces/<user>/<space_name>.git
4
+
5
+ set -e
6
+
7
+ if [ $# -lt 1 ]; then
8
+ echo "Usage: $0 <path_to_latest_yolo_train_dir>"
9
+ exit 1
10
+ fi
11
+
12
+ MODEL_DIR=$1
13
+ [email protected]:spaces/rayh/clusterflux
14
+ MODEL_SERVER_DIR=$(dirname "$0")
15
+ WEIGHTS_SRC="$MODEL_DIR/train/weights/best.pt"
16
+ WEIGHTS_DST="$MODEL_SERVER_DIR/weights/best.pt"
17
+
18
+ # Step 1: Copy model weights
19
+ mkdir -p "$MODEL_SERVER_DIR/weights"
20
+ cp "$WEIGHTS_SRC" "$WEIGHTS_DST"
21
+ echo "Copied model weights from $WEIGHTS_SRC to $WEIGHTS_DST"
22
+
23
+ # Step 2: Extract version (last part of model dir)
24
+ VERSION=$(basename "$MODEL_DIR")
25
+ echo "$VERSION" > "$MODEL_SERVER_DIR/VERSION"
26
+ echo "Set VERSION to $VERSION"
27
+
28
+ # Step 3: Deploy to Hugging Face Spaces
29
+ echo "Pushing to Hugging Face Space..."
30
+ cd "$MODEL_SERVER_DIR"
31
+ if [ ! -d .git ]; then
32
+ git init
33
+ git remote add origin "$HF_SPACE_URL"
34
+ fi
35
+ git add .
36
+ git commit -m "Deploy latest YOLO model and app (version $VERSION)" || echo "Nothing to commit"
37
+ git branch -M main
38
+ git push -u origin main --force
39
+
40
+ echo "Deployment complete."
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ pillow
3
+ # Add your model dependencies below, e.g.:
4
+ ultralytics
5
+ torch