taybeyond commited on
Commit
292255d
·
verified ·
1 Parent(s): 2a4714f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -1,38 +1,39 @@
1
  import gradio as gr
2
- from transformers import AutoProcessor, AutoModelForCausalLM
3
  import torch
 
4
  import os
5
  from huggingface_hub import login
6
 
7
- # ✅ 设置你的 Hugging Face Token
8
- HF_TOKEN = os.environ.get("HF_TOKEN")# ← 这里替换为你的 token
9
  login(token=HF_TOKEN)
10
 
11
- # ✅ 指定模型
12
  MODEL_ID = "Qwen/Qwen-VL-Chat"
13
 
14
- # 加载模型
15
- processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN)
16
- model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True, device_map="auto", token=HF_TOKEN)
17
- model.eval()
18
 
19
- # 推理函数
20
  def ask(image, prompt):
21
- inputs = processor.from_list_format([
22
- {"image": image},
23
- {"text": prompt}
24
- ])
25
- inputs = processor(inputs, return_tensors="pt").to(model.device)
26
- outputs = model.generate(**inputs, max_new_tokens=512)
27
- response = processor.batch_decode(outputs, skip_special_tokens=True)[0]
 
28
  return response
29
 
30
- # Gradio 页面
31
  demo = gr.Interface(
32
  fn=ask,
33
- inputs=[gr.Image(type="pil"), gr.Textbox(label="请输入问题")],
34
  outputs="text",
35
- title="Qwen1.5-VL-Chat 在线体验"
36
  )
37
 
38
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, AutoImageProcessor
3
  import torch
4
+ from PIL import Image
5
  import os
6
  from huggingface_hub import login
7
 
8
+ # ✅ 登入 Token(注意,不要寫死 token,請用 Secrets)
9
+ HF_TOKEN = os.environ.get("HF_TOKEN")
10
  login(token=HF_TOKEN)
11
 
12
+ # ✅ 模型與處理器
13
  MODEL_ID = "Qwen/Qwen-VL-Chat"
14
 
15
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN)
16
+ image_processor = AutoImageProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN)
17
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN).eval()
 
18
 
19
+ # ✅ 推理函數
20
  def ask(image, prompt):
21
+ image_tensor = image_processor(image, return_tensors="pt")["pixel_values"].to(model.device)
22
+ text_input = tokenizer(prompt, return_tensors="pt").to(model.device)
23
+ inputs = {
24
+ "input_ids": text_input["input_ids"],
25
+ "pixel_values": image_tensor
26
+ }
27
+ output = model.generate(**inputs, max_new_tokens=512)
28
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
29
  return response
30
 
31
+ # Gradio UI
32
  demo = gr.Interface(
33
  fn=ask,
34
+ inputs=[gr.Image(type="pil"), gr.Textbox(label="請輸入問題")],
35
  outputs="text",
36
+ title="🧠 Qwen-VL 圖文問答 Demo"
37
  )
38
 
39
  if __name__ == "__main__":