File size: 592 Bytes
ca7c5dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from transformers import pipeline

def load_image_model():
    """

    Loads HuggingFaceTB/SmolVLM-500M-Instruct or another image-to-text model.

    """
    return pipeline("image-to-text", model="HuggingFaceTB/SmolVLM-500M-Instruct")

def analyze_image(image_file, image_model):
    """

    Pass an image file to the image model pipeline and return the text/caption.

    """
    result = image_model(image_file)
    if isinstance(result, list) and len(result) > 0:
        return result[0].get("generated_text", "No caption generated.")
    return "Unable to process image."