"""Apparel Image Caption demo: upload a clothing photo and get alt-text for it.""" import os import spaces # Hugging Face ZeroGPU: lends the Space a GPU while caption() runs import gradio as gr import torch from transformers import AutoProcessor, Florence2ForConditionalGeneration # The fine-tuned model on the Hugging Face Hub. Set the MODEL_ID environment variable # to a local folder (outputs/checkpoints/best) to try the app without the Hub. MODEL_ID = os.environ.get("MODEL_ID", "MeghanaNanuvala/apparel-image-caption") DEVICE = "cuda" if torch.cuda.is_available() else "cpu" processor = AutoProcessor.from_pretrained(MODEL_ID) model = Florence2ForConditionalGeneration.from_pretrained(MODEL_ID).eval().to(DEVICE) @spaces.GPU(duration=30) # one caption takes about a second on the GPU @torch.inference_mode() def caption(image): if image is None: return "" # same prompt and decoding settings as in training and evaluation inputs = processor(text=["