File size: 958 Bytes
dde1432 294e9b9 2a4714f 294e9b9 dde1432 292255d 2a4714f dde1432 294e9b9 dde1432 294e9b9 dde1432 2a4714f 294e9b9 2a4714f dde1432 2a4714f 292255d dde1432 294e9b9 dde1432 2a4714f |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoProcessor
from huggingface_hub import login
import os
import torch
HF_TOKEN = os.environ.get("HF_TOKEN")
login(token=HF_TOKEN)
MODEL_ID = "Qwen/Qwen-VL-Chat-Int4"
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True, device_map="auto", token=HF_TOKEN)
model.eval()
def ask(image, prompt):
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=512)
response = processor.batch_decode(outputs, skip_special_tokens=True)[0]
return response
demo = gr.Interface(
fn=ask,
inputs=[gr.Image(type="pil"), gr.Textbox(label="請輸入問題")],
outputs="text",
title="🧠 Qwen1.5-VL 圖文問答 Demo"
)
if __name__ == "__main__":
demo.launch() |