File size: 935 Bytes
ce47c87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from fastapi import FastAPI, File, UploadFile
from PIL import Image
from transformers import AutoProcessor, Blip2ForConditionalGeneration
import torch
import io

app = FastAPI()

# Load the model and processor
model = Blip2ForConditionalGeneration.from_pretrained("ybelkada/blip2-opt-2.7b-fp16-sharded")
model.load_adapter('blip-cpu-model')
processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

@app.post("/generate-caption/")
async def generate_caption(file: UploadFile = File(...)):
    image = Image.open(io.BytesIO(await file.read()))
    inputs = processor(images=image, return_tensors="pt").to(device, torch.float16)
    
    with torch.no_grad():
        caption_ids = model.generate(**inputs, max_length=128)
        caption = processor.decode(caption_ids[0], skip_special_tokens=True)
    
    return {"caption": caption}