File size: 968 Bytes
b3dfb2c
4473c1b
c8ff0d2
 
 
 
b3dfb2c
4e4112c
4473c1b
 
 
 
c8ff0d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image

from transformers import AutoProcessor, AutoModelForVision2Seq

DEVICE = "cuda:0"

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Image.', use_column_width=True)

    processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-8b-base")
    model = AutoModelForVision2Seq.from_pretrained(
        "HuggingFaceM4/idefics2-8b-base",
    ).to(DEVICE)

    # Create inputs
    prompts = [
        "<image>",
    ]
    images = [image]
    inputs = processor(text=prompts, images=images, padding=True, return_tensors="pt")
    inputs = {k: v.to(DEVICE) for k, v in inputs.items()}


    # Generate
    generated_ids = model.generate(**inputs, max_new_tokens=500)
    generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)

    print(generated_texts)