Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoModelForCausalLM
|
| 4 |
from deepseek_vl.models import VLChatProcessor
|
| 5 |
-
from deepseek_vl.utils.io import load_pil_images
|
| 6 |
|
| 7 |
# 模型路徑
|
| 8 |
model_path = "deepseek-ai/deepseek-vl-7b-chat"
|
|
@@ -11,45 +10,55 @@ model_path = "deepseek-ai/deepseek-vl-7b-chat"
|
|
| 11 |
vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)
|
| 12 |
tokenizer = vl_chat_processor.tokenizer
|
| 13 |
|
| 14 |
-
# ====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
vl_gpt: AutoModelForCausalLM = AutoModelForCausalLM.from_pretrained(
|
| 16 |
model_path,
|
| 17 |
trust_remote_code=True,
|
| 18 |
-
device_map="auto",
|
| 19 |
-
|
| 20 |
)
|
| 21 |
vl_gpt.eval()
|
| 22 |
|
| 23 |
# ==== 單張圖片處理 + 減少 max_new_tokens ====
|
| 24 |
def generate_answer(image, text):
|
| 25 |
try:
|
|
|
|
| 26 |
conversation = [
|
| 27 |
{"role": "User", "content": "<image_placeholder>" + text, "images": [image]},
|
| 28 |
{"role": "Assistant", "content": ""}
|
| 29 |
]
|
| 30 |
|
| 31 |
-
|
| 32 |
prepare_inputs = vl_chat_processor(
|
| 33 |
conversations=conversation,
|
| 34 |
-
images=
|
| 35 |
force_batchify=True
|
| 36 |
).to(vl_gpt.device)
|
| 37 |
|
|
|
|
| 38 |
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
|
| 39 |
|
|
|
|
| 40 |
outputs = vl_gpt.language_model.generate(
|
| 41 |
inputs_embeds=inputs_embeds,
|
| 42 |
attention_mask=prepare_inputs.attention_mask,
|
| 43 |
pad_token_id=tokenizer.eos_token_id,
|
| 44 |
bos_token_id=tokenizer.bos_token_id,
|
| 45 |
eos_token_id=tokenizer.eos_token_id,
|
| 46 |
-
max_new_tokens=128, #
|
| 47 |
do_sample=False,
|
| 48 |
use_cache=True
|
| 49 |
)
|
| 50 |
|
| 51 |
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
|
| 52 |
return f"{prepare_inputs['sft_format'][0]} {answer}"
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error: {str(e)}"
|
| 55 |
|
|
@@ -59,7 +68,7 @@ demo = gr.Interface(
|
|
| 59 |
inputs=[gr.Image(type="pil", label="Upload Image"), gr.Textbox(label="Question")],
|
| 60 |
outputs="text",
|
| 61 |
title="DeepSeek-VL-7B Chat Demo",
|
| 62 |
-
description="上傳圖片並輸入問題,模型會生成與圖片相關的回答(
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
from deepseek_vl.models import VLChatProcessor
|
|
|
|
| 5 |
|
| 6 |
# 模型路徑
|
| 7 |
model_path = "deepseek-ai/deepseek-vl-7b-chat"
|
|
|
|
| 10 |
vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)
|
| 11 |
tokenizer = vl_chat_processor.tokenizer
|
| 12 |
|
| 13 |
+
# ==== 量化模型設定 (4-bit) ====
|
| 14 |
+
bnb_config = BitsAndBytesConfig(
|
| 15 |
+
load_in_4bit=True,
|
| 16 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 17 |
+
bnb_4bit_use_double_quant=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
vl_gpt: AutoModelForCausalLM = AutoModelForCausalLM.from_pretrained(
|
| 21 |
model_path,
|
| 22 |
trust_remote_code=True,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
quantization_config=bnb_config
|
| 25 |
)
|
| 26 |
vl_gpt.eval()
|
| 27 |
|
| 28 |
# ==== 單張圖片處理 + 減少 max_new_tokens ====
|
| 29 |
def generate_answer(image, text):
|
| 30 |
try:
|
| 31 |
+
# 將圖片與文字組合成對話格式
|
| 32 |
conversation = [
|
| 33 |
{"role": "User", "content": "<image_placeholder>" + text, "images": [image]},
|
| 34 |
{"role": "Assistant", "content": ""}
|
| 35 |
]
|
| 36 |
|
| 37 |
+
# 🚨 這裡直接傳入 [image],因為已經是 PIL.Image,不需要 load_pil_images
|
| 38 |
prepare_inputs = vl_chat_processor(
|
| 39 |
conversations=conversation,
|
| 40 |
+
images=[image],
|
| 41 |
force_batchify=True
|
| 42 |
).to(vl_gpt.device)
|
| 43 |
|
| 44 |
+
# 轉換成 embeddings
|
| 45 |
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
|
| 46 |
|
| 47 |
+
# 產生回答
|
| 48 |
outputs = vl_gpt.language_model.generate(
|
| 49 |
inputs_embeds=inputs_embeds,
|
| 50 |
attention_mask=prepare_inputs.attention_mask,
|
| 51 |
pad_token_id=tokenizer.eos_token_id,
|
| 52 |
bos_token_id=tokenizer.bos_token_id,
|
| 53 |
eos_token_id=tokenizer.eos_token_id,
|
| 54 |
+
max_new_tokens=128, # 降低生成長度以減少記憶體
|
| 55 |
do_sample=False,
|
| 56 |
use_cache=True
|
| 57 |
)
|
| 58 |
|
| 59 |
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
|
| 60 |
return f"{prepare_inputs['sft_format'][0]} {answer}"
|
| 61 |
+
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error: {str(e)}"
|
| 64 |
|
|
|
|
| 68 |
inputs=[gr.Image(type="pil", label="Upload Image"), gr.Textbox(label="Question")],
|
| 69 |
outputs="text",
|
| 70 |
title="DeepSeek-VL-7B Chat Demo",
|
| 71 |
+
description="上傳圖片並輸入問題,模型會生成與圖片相關的回答(4-bit 量化,低記憶體模式)"
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|