Upload 3 files
Browse files- README.md +8 -6
- app.py +43 -0
- requirements.txt +9 -0
README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
1 |
---
|
2 |
+
title: TEST 01
|
3 |
+
emoji: 🧠
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.50.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
+
⚠️ 本模型為 gated model,請至 [Hugging Face token 設定頁](https://huggingface.co/settings/tokens) 創建 token,並在本 Space 的 Settings → Secrets 中添加 `HF_TOKEN`。
|
13 |
+
|
14 |
+
---
|
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
5 |
+
|
6 |
+
# 模型与处理器
|
7 |
+
model_id = "Qwen/Qwen1.5-VL-Chat"
|
8 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
9 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
10 |
+
model_id,
|
11 |
+
device_map="auto", # 自动分配 GPU 或 CPU
|
12 |
+
trust_remote_code=True,
|
13 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
14 |
+
**{"disable_exllama": True} # 防止加载失败
|
15 |
+
).eval()
|
16 |
+
|
17 |
+
# 推理函数
|
18 |
+
def chat(image, question):
|
19 |
+
if image is None or question.strip() == "":
|
20 |
+
return "請上傳圖片並輸入問題。"
|
21 |
+
inputs = processor(text=question, images=image, return_tensors="pt").to(model.device)
|
22 |
+
outputs = model.generate(**inputs, max_new_tokens=512)
|
23 |
+
answer = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
24 |
+
return answer.strip()
|
25 |
+
|
26 |
+
# 界面設計
|
27 |
+
with gr.Blocks(title="Qwen1.5-VL 圖文問答 Demo") as demo:
|
28 |
+
gr.Markdown("## 🧠 Qwen1.5-VL 圖文問答 Demo")
|
29 |
+
|
30 |
+
with gr.Row():
|
31 |
+
with gr.Column():
|
32 |
+
image_input = gr.Image(type="pil", label="📷 請上傳圖片")
|
33 |
+
question_input = gr.Textbox(label="請輸入問題", placeholder="例如:這是什麼地方?")
|
34 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
35 |
+
clear_btn = gr.Button("Clear")
|
36 |
+
with gr.Column():
|
37 |
+
answer_output = gr.Textbox(label="💬 答案", lines=8)
|
38 |
+
|
39 |
+
submit_btn.click(fn=chat, inputs=[image_input, question_input], outputs=answer_output)
|
40 |
+
clear_btn.click(lambda: ("", "", ""), outputs=[image_input, question_input, answer_output])
|
41 |
+
|
42 |
+
# 啟動服務
|
43 |
+
demo.launch(share=True) # 如果你不想公開訪問可以改為 share=False
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers>=4.36.2
|
2 |
+
torch
|
3 |
+
gradio
|
4 |
+
einops
|
5 |
+
tiktoken
|
6 |
+
torchvision
|
7 |
+
optimum
|
8 |
+
auto-gptq
|
9 |
+
transformers_stream_generator
|