File size: 771 Bytes
b6a0b2f
 
 
 
 
 
bd0bd80
 
b6a0b2f
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import gradio as gr
from transformers import AutoProcessor, AutoModelForVision2Seq
from PIL import Image
import torch

model_id = "Qwen/Qwen-VL"
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True)

def answer_question(image, question):
    inputs = processor(images=image, text=question, return_tensors="pt").to("cuda")
    generated_ids = model.generate(**inputs, max_new_tokens=50)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_text

iface = gr.Interface(fn=answer_question, inputs=["image", "text"], outputs="text", title="Qwen-VL Demo")
iface.launch()