Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -14,8 +14,6 @@ app = FastAPI(title="SmolVLM Inference API", version="1.2.0")
|
|
| 14 |
_runner: Optional[SmolVLMRunner] = None
|
| 15 |
|
| 16 |
|
| 17 |
-
# ----------------------- Pydantic models -----------------------
|
| 18 |
-
|
| 19 |
class URLRequest(BaseModel):
|
| 20 |
prompt: str = Field(..., description="Text prompt to accompany the images.")
|
| 21 |
image_urls: List[HttpUrl] = Field(..., description="List of image URLs.")
|
|
@@ -34,9 +32,6 @@ class DetectDescribeURLRequest(BaseModel):
|
|
| 34 |
temperature: Optional[float] = None
|
| 35 |
top_p: Optional[float] = None
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# ----------------------- Startup / health -----------------------
|
| 39 |
-
|
| 40 |
@app.on_event("startup")
|
| 41 |
async def _load_model_on_startup():
|
| 42 |
global _runner
|
|
@@ -46,9 +41,6 @@ async def _load_model_on_startup():
|
|
| 46 |
def health():
|
| 47 |
return {"status": "ok", "model": _runner.model_id if _runner else None}
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# ----------------------- Core VLM endpoints -----------------------
|
| 51 |
-
|
| 52 |
@app.post("/generate")
|
| 53 |
async def generate_from_files(
|
| 54 |
prompt: str = Form(...),
|
|
@@ -122,9 +114,6 @@ async def generate_from_urls(req: URLRequest):
|
|
| 122 |
}
|
| 123 |
return {"text": text, "metrics": metrics}
|
| 124 |
|
| 125 |
-
|
| 126 |
-
# ----------------------- Detect & Describe endpoints -----------------------
|
| 127 |
-
|
| 128 |
@app.post("/detect_describe")
|
| 129 |
async def detect_describe(
|
| 130 |
image: UploadFile = File(..., description="One image file (image/*)"),
|
|
@@ -181,8 +170,6 @@ async def detect_describe_url(req: DetectDescribeURLRequest):
|
|
| 181 |
return out
|
| 182 |
|
| 183 |
|
| 184 |
-
# ----------------------- Entrypoint -----------------------
|
| 185 |
-
|
| 186 |
if __name__ == "__main__":
|
| 187 |
# Run with: python app.py (or: uvicorn app:app --host 0.0.0.0 --port 8000)
|
| 188 |
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=False)
|
|
|
|
| 14 |
_runner: Optional[SmolVLMRunner] = None
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
| 17 |
class URLRequest(BaseModel):
|
| 18 |
prompt: str = Field(..., description="Text prompt to accompany the images.")
|
| 19 |
image_urls: List[HttpUrl] = Field(..., description="List of image URLs.")
|
|
|
|
| 32 |
temperature: Optional[float] = None
|
| 33 |
top_p: Optional[float] = None
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
@app.on_event("startup")
|
| 36 |
async def _load_model_on_startup():
|
| 37 |
global _runner
|
|
|
|
| 41 |
def health():
|
| 42 |
return {"status": "ok", "model": _runner.model_id if _runner else None}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
@app.post("/generate")
|
| 45 |
async def generate_from_files(
|
| 46 |
prompt: str = Form(...),
|
|
|
|
| 114 |
}
|
| 115 |
return {"text": text, "metrics": metrics}
|
| 116 |
|
|
|
|
|
|
|
|
|
|
| 117 |
@app.post("/detect_describe")
|
| 118 |
async def detect_describe(
|
| 119 |
image: UploadFile = File(..., description="One image file (image/*)"),
|
|
|
|
| 170 |
return out
|
| 171 |
|
| 172 |
|
|
|
|
|
|
|
| 173 |
if __name__ == "__main__":
|
| 174 |
# Run with: python app.py (or: uvicorn app:app --host 0.0.0.0 --port 8000)
|
| 175 |
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=False)
|
grounding_dino2.py
CHANGED
|
@@ -11,7 +11,6 @@ import torch
|
|
| 11 |
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
|
| 13 |
|
| 14 |
-
# ---- Writable caches (HF Spaces / containers) ----
|
| 15 |
CACHE_DIR = os.getenv("HF_CACHE_DIR", "/tmp/hf-cache")
|
| 16 |
Path(CACHE_DIR).mkdir(parents=True, exist_ok=True)
|
| 17 |
os.environ.setdefault("HOME", "/tmp")
|
|
@@ -181,8 +180,6 @@ def visualize_detections(
|
|
| 181 |
draw.text((x0 + pad, max(0, y0 - 18)), text, fill=text_color, font=font)
|
| 182 |
return vis
|
| 183 |
|
| 184 |
-
|
| 185 |
-
# convenience singleton
|
| 186 |
_runner_singleton: GroundingDINORunner | None = None
|
| 187 |
|
| 188 |
def get_runner() -> GroundingDINORunner:
|
|
|
|
| 11 |
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
|
| 13 |
|
|
|
|
| 14 |
CACHE_DIR = os.getenv("HF_CACHE_DIR", "/tmp/hf-cache")
|
| 15 |
Path(CACHE_DIR).mkdir(parents=True, exist_ok=True)
|
| 16 |
os.environ.setdefault("HOME", "/tmp")
|
|
|
|
| 180 |
draw.text((x0 + pad, max(0, y0 - 18)), text, fill=text_color, font=font)
|
| 181 |
return vis
|
| 182 |
|
|
|
|
|
|
|
| 183 |
_runner_singleton: GroundingDINORunner | None = None
|
| 184 |
|
| 185 |
def get_runner() -> GroundingDINORunner:
|
ui.py
CHANGED
|
@@ -29,7 +29,6 @@ def show_metrics(metrics: dict):
|
|
| 29 |
|
| 30 |
tab_upload, tab_detect = st.tabs(["SmolVLM Detection", "Grounded Detection"])
|
| 31 |
|
| 32 |
-
# -------------------- Tab 1: uploads -> /generate --------------------
|
| 33 |
with tab_upload:
|
| 34 |
st.subheader("Upload an image")
|
| 35 |
files = st.file_uploader("Images", type=["png", "jpg", "jpeg", "webp"], accept_multiple_files=True)
|
|
@@ -84,7 +83,6 @@ with tab_upload:
|
|
| 84 |
except Exception:
|
| 85 |
st.write(e.response.text)
|
| 86 |
|
| 87 |
-
# -------------------- Tab 2: Detect & Describe -> /detect_describe --------------------
|
| 88 |
with tab_detect:
|
| 89 |
st.subheader("SmolVLM Grounded Detection")
|
| 90 |
|
|
|
|
| 29 |
|
| 30 |
tab_upload, tab_detect = st.tabs(["SmolVLM Detection", "Grounded Detection"])
|
| 31 |
|
|
|
|
| 32 |
with tab_upload:
|
| 33 |
st.subheader("Upload an image")
|
| 34 |
files = st.file_uploader("Images", type=["png", "jpg", "jpeg", "webp"], accept_multiple_files=True)
|
|
|
|
| 83 |
except Exception:
|
| 84 |
st.write(e.response.text)
|
| 85 |
|
|
|
|
| 86 |
with tab_detect:
|
| 87 |
st.subheader("SmolVLM Grounded Detection")
|
| 88 |
|
util.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
-
# util.py
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
@@ -15,7 +15,6 @@ os.environ.setdefault("HUGGINGFACE_HUB_CACHE", CACHE_DIR)
|
|
| 15 |
os.environ.setdefault("XDG_CACHE_HOME", CACHE_DIR)
|
| 16 |
os.environ.setdefault("TORCH_HOME", CACHE_DIR)
|
| 17 |
|
| 18 |
-
# util.py (Spaces-safe + metrics)
|
| 19 |
|
| 20 |
from time import perf_counter
|
| 21 |
import threading
|
|
@@ -285,8 +284,6 @@ class SmolVLMRunner:
|
|
| 285 |
|
| 286 |
return (text, metrics) if return_stats else text
|
| 287 |
|
| 288 |
-
|
| 289 |
-
# Convenience singleton
|
| 290 |
_runner_singleton = None
|
| 291 |
def get_runner():
|
| 292 |
global _runner_singleton
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
+
# util.py
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
|
|
| 15 |
os.environ.setdefault("XDG_CACHE_HOME", CACHE_DIR)
|
| 16 |
os.environ.setdefault("TORCH_HOME", CACHE_DIR)
|
| 17 |
|
|
|
|
| 18 |
|
| 19 |
from time import perf_counter
|
| 20 |
import threading
|
|
|
|
| 284 |
|
| 285 |
return (text, metrics) if return_stats else text
|
| 286 |
|
|
|
|
|
|
|
| 287 |
_runner_singleton = None
|
| 288 |
def get_runner():
|
| 289 |
global _runner_singleton
|