Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files1 unmodified line2345678910111213141516171819202122239 unmodified lines333435363738110 unmodified lines1491501511521531541551 unmodified lineimport torchfrom PIL import Imagefrom transformers import AutoTokenizer, AutoModelForCausalLM# Model configurationMID = "apple/FastVLM-0.5B"IMAGE_TOKEN_INDEX = -200# Load model and tokenizer once at startupprint("Loading model...")tok = AutoTokenizer.from_pretrained(MID, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(MID,torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,device_map="auto",trust_remote_code=True,)print("Model loaded successfully!")def caption_image(image, custom_prompt=None):"""Generate a caption for the input image.9 unmodified linesreturn "Please upload an image first."try:# Convert image to RGB if neededif image.mode != "RGB":image = image.convert("RGB")110 unmodified lines---**Model:** [apple/FastVLM-0.5B](https://huggingface.co/apple/FastVLM-0.5B)**Note:** This model runs best on GPU. CPU inference may be slower.""")1 unmodified line2345678910111213141516171819202122232425262728293031329 unmodified lines4243444546474849110 unmodified lines1601611621631641651661 unmodified lineimport torchfrom PIL import Imagefrom transformers import AutoTokenizer, AutoModelForCausalLMimport spaces# Model configurationMID = "apple/FastVLM-0.5B"IMAGE_TOKEN_INDEX = -200# Load model and tokenizer (will be loaded on first GPU allocation)tok = Nonemodel = Nonedef load_model():global tok, modelif tok is None or model is None:print("Loading model...")tok = AutoTokenizer.from_pretrained(MID, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(MID,torch_dtype=torch.float16,device_map="cuda",trust_remote_code=True,)print("Model loaded successfully!")return tok, model@spaces.GPU(duration=60)def caption_image(image, custom_prompt=None):"""Generate a caption for the input image.9 unmodified linesreturn "Please upload an image first."try:# Load model if not already loadedtok, model = load_model()# Convert image to RGB if neededif image.mode != "RGB":image = image.convert("RGB")110 unmodified lines---**Model:** [apple/FastVLM-0.5B](https://huggingface.co/apple/FastVLM-0.5B)**Note:** This Space uses ZeroGPU for dynamic GPU allocation.""")
app.py+22-11