|
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() |