File size: 1,361 Bytes
dde1432
292255d
dde1432
292255d
2a4714f
 
dde1432
292255d
 
2a4714f
dde1432
292255d
2a4714f
dde1432
292255d
 
 
dde1432
292255d
2a4714f
292255d
 
 
 
 
 
 
 
2a4714f
 
292255d
dde1432
2a4714f
292255d
dde1432
292255d
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
31
32
33
34
35
36
37
38
39
40
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoImageProcessor
import torch
from PIL import Image
import os
from huggingface_hub import login

# ✅ 登入 Token(注意,不要寫死 token,請用 Secrets)
HF_TOKEN = os.environ.get("HF_TOKEN")
login(token=HF_TOKEN)

# ✅ 模型與處理器
MODEL_ID = "Qwen/Qwen-VL-Chat"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN)
image_processor = AutoImageProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN).eval()

# ✅ 推理函數
def ask(image, prompt):
    image_tensor = image_processor(image, return_tensors="pt")["pixel_values"].to(model.device)
    text_input = tokenizer(prompt, return_tensors="pt").to(model.device)
    inputs = {
        "input_ids": text_input["input_ids"],
        "pixel_values": image_tensor
    }
    output = model.generate(**inputs, max_new_tokens=512)
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    return response

# ✅ Gradio UI
demo = gr.Interface(
    fn=ask,
    inputs=[gr.Image(type="pil"), gr.Textbox(label="請輸入問題")],
    outputs="text",
    title="🧠 Qwen-VL 圖文問答 Demo"
)

if __name__ == "__main__":
    demo.launch()