Skip to content

Image Input with Python Binary IO objects generate random answer #153

@rainyear

Description

@rainyear

Python codes here:

def download_image(src) -> BytesIO:
    resp = req.get(src, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}, stream=True, timeout=10)
    resp.raise_for_status()
    img_data = BytesIO(resp.content)
    img_data.seek(0)
    return img_data

def caption_image(src, cache_dir):
    model = lms.llm(config={"temperature": 0.1})
    try:
        img = download_image(src)
        
        image_handle = lms.prepare_image(img)
        chat = lms.Chat()
        chat.add_user_message("Describe this image please ", images=[image_handle])
        prediction = model.respond(chat)
        return f"![{prediction.content}]({src})"
    except:
        return f"![]({src})"

repond random answer.
but save image to temp file and prepare_image(/path/to/temp.jpeg) works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions