File size: 1,335 Bytes
f6d510a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
# image_captioning.py

import streamlit as st
from PIL import Image
import torch
from transformers import BlipProcessor, BlipForConditionalGeneration
import io

# Cache the model loading to avoid reloading on every interaction
@st.cache_resource
def load_captioning_model():
    """Load the BLIP model and processor for image captioning."""
    try:
        processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
        model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
        return processor, model
    except Exception as e:
        st.error(f"Error loading captioning model: {e}")
        return None, None

def generate_caption(image_bytes: bytes):
    """Generate a caption for a given image."""
    processor, model = load_captioning_model()
    if not processor or not model:
        return "Captioning model not available."

    try:
        image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
        inputs = processor(image, return_tensors="pt")

        with torch.no_grad():
            output = model.generate(**inputs, max_length=50, num_beams=4, early_stopping=True)

        caption = processor.decode(output[0], skip_special_tokens=True)
        return caption
    except Exception as e:
        return f"Error generating caption: {e}"