Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- Dockerfile +12 -0
- README.md +20 -5
- app.py +81 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY app.py .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,10 +1,25 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SAM3 LS Backend
|
| 3 |
+
emoji: π―
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# SAM3 Label Studio ML Backend
|
| 12 |
+
|
| 13 |
+
Phase 1: hardcoded predictions for round-trip testing with Label Studio.
|
| 14 |
+
|
| 15 |
+
Endpoints:
|
| 16 |
+
- `GET /health` β liveness
|
| 17 |
+
- `POST /setup` β LS project setup (no-op stub)
|
| 18 |
+
- `POST /predict` β returns one fixed bounding box per task
|
| 19 |
+
- `POST /webhook` β annotation events (logged, no-op)
|
| 20 |
+
|
| 21 |
+
Configure in Label Studio: Settings β Machine Learning β Add Model β URL = `https://<username>-sam3-ls-backend.hf.space`.
|
| 22 |
+
|
| 23 |
+
Expects an LS label config with:
|
| 24 |
+
- `<Image name="image" value="$image"/>`
|
| 25 |
+
- `<RectangleLabels name="label" toName="image">` containing a `<Label value="butterfly"/>` (or override `DEFAULT_LABEL` env var).
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
|
| 5 |
+
from flask import Flask, jsonify, request
|
| 6 |
+
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
log = logging.getLogger("sam3-ls-backend")
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
MODEL_VERSION = os.environ.get("MODEL_VERSION", "sam3-stub-v1")
|
| 13 |
+
DEFAULT_LABEL = os.environ.get("DEFAULT_LABEL", "butterfly")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def stub_prediction(task: dict) -> dict:
|
| 17 |
+
return {
|
| 18 |
+
"model_version": MODEL_VERSION,
|
| 19 |
+
"score": 0.9,
|
| 20 |
+
"result": [
|
| 21 |
+
{
|
| 22 |
+
"id": str(uuid.uuid4())[:8],
|
| 23 |
+
"from_name": "label",
|
| 24 |
+
"to_name": "image",
|
| 25 |
+
"type": "rectanglelabels",
|
| 26 |
+
"original_width": 1920,
|
| 27 |
+
"original_height": 1080,
|
| 28 |
+
"image_rotation": 0,
|
| 29 |
+
"value": {
|
| 30 |
+
"x": 25.0,
|
| 31 |
+
"y": 25.0,
|
| 32 |
+
"width": 50.0,
|
| 33 |
+
"height": 50.0,
|
| 34 |
+
"rotation": 0,
|
| 35 |
+
"rectanglelabels": [DEFAULT_LABEL],
|
| 36 |
+
},
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@app.route("/health", methods=["GET"])
|
| 43 |
+
def health():
|
| 44 |
+
return jsonify({"status": "UP", "model_version": MODEL_VERSION})
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@app.route("/setup", methods=["POST"])
|
| 48 |
+
def setup():
|
| 49 |
+
payload = request.get_json(silent=True) or {}
|
| 50 |
+
log.info("setup called: project=%s", payload.get("project"))
|
| 51 |
+
return jsonify({"model_version": MODEL_VERSION})
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@app.route("/predict", methods=["POST"])
|
| 55 |
+
def predict():
|
| 56 |
+
payload = request.get_json(silent=True) or {}
|
| 57 |
+
tasks = payload.get("tasks", [])
|
| 58 |
+
log.info("predict called: %d task(s)", len(tasks))
|
| 59 |
+
return jsonify({"results": [stub_prediction(t) for t in tasks]})
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.route("/webhook", methods=["POST"])
|
| 63 |
+
def webhook():
|
| 64 |
+
payload = request.get_json(silent=True) or {}
|
| 65 |
+
log.info("webhook event: %s", payload.get("action"))
|
| 66 |
+
return jsonify({"status": "ok"})
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@app.route("/", methods=["GET"])
|
| 70 |
+
def root():
|
| 71 |
+
return jsonify(
|
| 72 |
+
{
|
| 73 |
+
"service": "sam3-ls-backend",
|
| 74 |
+
"model_version": MODEL_VERSION,
|
| 75 |
+
"endpoints": ["/health", "/setup", "/predict", "/webhook"],
|
| 76 |
+
}
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask==3.0.3
|
| 2 |
+
gunicorn==23.0.0
|