Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForVision2Seq | |
| from PIL import Image | |
| import torch | |
| # Load model and tokenizer | |
| model_id = "Qwen/Qwen-VL" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) | |
| model = AutoModelForVision2Seq.from_pretrained(model_id, trust_remote_code=True, torch_dtype=torch.float16).to("cuda").eval() | |
| # Inference function | |
| def ask_qwen(image, prompt): | |
| query = tokenizer.from_list_format([ | |
| {"image": image}, | |
| {"text": prompt} | |
| ]) | |
| inputs = tokenizer(query, return_tensors="pt").to("cuda", torch.float16) | |
| with torch.no_grad(): | |
| output = model.generate(**inputs, max_new_tokens=128) | |
| answer = tokenizer.decode(output[0], skip_special_tokens=True) | |
| return answer.strip() | |
| # Gradio interface | |
| demo = gr.Interface( | |
| fn=ask_qwen, | |
| inputs=[gr.Image(type="pil"), gr.Textbox(label="Prompt")], | |
| outputs=gr.Textbox(label="Answer"), | |
| title="Qwen-VL 2.5 - Vision Language Chatbot", | |
| description="Chat with an image using Qwen-VL 2.5 (3B)" | |
| ) | |
| demo.launch() | |