Spaces:
Sleeping
Sleeping
Commit ·
210def2
1
Parent(s): 641b32e
fix: added
Browse filesFiles changed (4)
9 unmodified lines101112139 unmodified linesImage captioning API using `microsoft/Florence-2-base` with a Python FastAPI backend. Open `/docs` for Swagger UI.Speed tuning env vars: `DEFAULT_MAX_TOKENS` (default `64`), `MAX_IMAGE_SIDE` (default `896`), `MAX_MAX_TOKENS` (default `256`), `MODEL_ID` (default `microsoft/Florence-2-base`).9 unmodified lines101112139 unmodified linesImage captioning API using `microsoft/Florence-2-base` with a Python FastAPI backend. Open `/docs` for Swagger UI.Speed tuning env vars: `DEFAULT_MAX_TOKENS` (default `64`), `MAX_IMAGE_SIDE` (default `896`), `MAX_MAX_TOKENS` (default `256`), `MODEL_ID` (default `microsoft/Florence-2-base`), `MODEL_REVISION` (optional commit SHA to pin remote model code).
8 unmodified lines91011128 unmodified lines- MAX_IMAGE_SIDE=896- MAX_MAX_TOKENS=256- MODEL_ID=microsoft/Florence-2-baserestart: unless-stopped8 unmodified lines910111213148 unmodified lines- MAX_IMAGE_SIDE=896- MAX_MAX_TOKENS=256- MODEL_ID=microsoft/Florence-2-base# Optional: pin to a specific commit SHA from huggingface.co/microsoft/Florence-2-base# - MODEL_REVISION=<commit_sha>restart: unless-stopped
3 unmodified lines4563 unmodified linestorch==2.8.0pillow==11.3.0python-multipart==0.0.203 unmodified lines456783 unmodified linestorch==2.8.0pillow==11.3.0python-multipart==0.0.20einops==0.8.1timm==1.0.19
8 unmodified lines910111213141516178 unmodified lines26272829303132332 unmodified lines3637383940414243444546474849505152535422 unmodified lines777879808182838485868788899091928 unmodified linesfrom transformers import AutoModelForCausalLM, AutoProcessorMODEL_ID = os.getenv("MODEL_ID", "microsoft/Florence-2-base")DEFAULT_MAX_TOKENS = int(os.getenv("DEFAULT_MAX_TOKENS", "64"))MAX_MAX_TOKENS = int(os.getenv("MAX_MAX_TOKENS", "256"))MAX_IMAGE_SIDE = int(os.getenv("MAX_IMAGE_SIDE", "896"))TASKS = {"caption": "<CAPTION>",8 unmodified lines_model = None_processor = None_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")_dtype = torch.float16 if _device.type == "cuda" else torch.float32def _prepare_image(image_bytes: bytes) -> Image.Image:2 unmodified linesif width <= MAX_IMAGE_SIDE and height <= MAX_IMAGE_SIDE:return imageratio = min(MAX_IMAGE_SIDE / width, MAX_IMAGE_SIDE / height)new_size = (max(1, int(width * ratio)), max(1, int(height * ratio)))return image.resize(new_size, Image.Resampling.LANCZOS)def load_model() -> tuple[Any, Any]:global _model, _processorif _model is None or _processor is None:_processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)_model = AutoModelForCausalLM.from_pretrained(MODEL_ID,trust_remote_code=True,torch_dtype=_dtype,).to(_device)_model.eval()return _model, _processor22 unmodified linespixel_values=pixel_values,do_sample=False,max_new_tokens=safe_max_tokens,num_beams=1,)generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()parsed = Nonepost_process = getattr(processor, "post_process_generation", None)if callable(post_process):try:parsed = post_process(generated_text, task=prompt_task, image_size=image.size)except Exception:parsed = None8 unmodified lines910111213141516171819208 unmodified lines29303132333435362 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747522 unmodified lines98991001011021031041051061071081091101111121131141151161178 unmodified linesfrom transformers import AutoModelForCausalLM, AutoProcessorMODEL_ID = os.getenv("MODEL_ID", "microsoft/Florence-2-base")MODEL_REVISION = os.getenv("MODEL_REVISION")DEFAULT_MAX_TOKENS = int(os.getenv("DEFAULT_MAX_TOKENS", "64"))MAX_MAX_TOKENS = int(os.getenv("MAX_MAX_TOKENS", "256"))MAX_IMAGE_SIDE = int(os.getenv("MAX_IMAGE_SIDE", "896"))RESIZE_MULTIPLE = int(os.getenv("RESIZE_MULTIPLE", "32"))NUM_BEAMS = int(os.getenv("NUM_BEAMS", "3"))TASKS = {"caption": "<CAPTION>",8 unmodified lines_model = None_processor = None_device = torch.device("cpu")_dtype = torch.float32def _prepare_image(image_bytes: bytes) -> Image.Image:2 unmodified linesif width <= MAX_IMAGE_SIDE and height <= MAX_IMAGE_SIDE:return imageif width >= height:# Landscape: cap width, preserve aspect ratio.ratio = MAX_IMAGE_SIDE / widthelse:# Portrait: cap height, preserve aspect ratio.ratio = MAX_IMAGE_SIDE / heightnew_w = max(1, int(width * ratio))new_h = max(1, int(height * ratio))# Align dimensions to improve tensor-core friendly shapes.if RESIZE_MULTIPLE > 1:new_w = max(RESIZE_MULTIPLE, (new_w // RESIZE_MULTIPLE) * RESIZE_MULTIPLE)new_h = max(RESIZE_MULTIPLE, (new_h // RESIZE_MULTIPLE) * RESIZE_MULTIPLE)new_size = (new_w, new_h)return image.resize(new_size, Image.Resampling.LANCZOS)def load_model() -> tuple[Any, Any]:global _model, _processorif _model is None or _processor is None:pretrained_kwargs: dict[str, Any] = {"trust_remote_code": True}if MODEL_REVISION:pretrained_kwargs["revision"] = MODEL_REVISION_processor = AutoProcessor.from_pretrained(MODEL_ID, **pretrained_kwargs)_model = AutoModelForCausalLM.from_pretrained(MODEL_ID,torch_dtype=_dtype,**pretrained_kwargs,).to(_device)_model.eval()return _model, _processor22 unmodified linespixel_values=pixel_values,do_sample=False,max_new_tokens=safe_max_tokens,num_beams=max(1, NUM_BEAMS),)generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0].strip()parsed = Nonepost_process = getattr(processor, "post_process_generation", None)if callable(post_process):try:parsed = post_process(generated_text,task=prompt_task,image_size=(image.width, image.height),)except Exception:parsed = None
README.md+1-1
docker-compose.yml+2
requirements.txt+2
src/model.py+34-9