robot0820 commited on
Commit
2c94591
·
verified ·
1 Parent(s): 3cdbf3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import torch
 
2
  from transformers import AutoModelForCausalLM, BitsAndBytesConfig
3
  from deepseek_vl.models import VLChatProcessor
4
  from deepseek_vl.utils.io import load_pil_images
@@ -6,13 +7,13 @@ from deepseek_vl.utils.io import load_pil_images
6
  # 模型路徑
7
  model_path = "deepseek-ai/deepseek-vl-7b-chat"
8
 
9
- # 讀取 processor
10
  vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)
11
  tokenizer = vl_chat_processor.tokenizer
12
 
13
- # ==== 量化模型設定 ====
14
  bnb_config = BitsAndBytesConfig(
15
- load_in_4bit=True, # 4-bit 量化
16
  bnb_4bit_compute_dtype=torch.float16,
17
  bnb_4bit_use_double_quant=True
18
  )
@@ -25,46 +26,44 @@ vl_gpt: AutoModelForCausalLM = AutoModelForCausalLM.from_pretrained(
25
  )
26
  vl_gpt.eval()
27
 
28
- # 範例對話
29
- conversation = [
30
- {
31
- "role": "User",
32
- "content": "<image_placeholder>Describe each stage of this image.",
33
- "images": ["./images/training_pipelines.png"]
34
- },
35
- {
36
- "role": "Assistant",
37
- "content": ""
38
- }
39
- ]
40
 
41
- # ==== 逐張圖片處理,降低 VRAM 使用 ====
42
- answers = []
43
- for conv in conversation:
44
- pil_images = load_pil_images([conv])
45
  prepare_inputs = vl_chat_processor(
46
- conversations=[conv],
47
  images=pil_images,
48
  force_batchify=True
49
  ).to(vl_gpt.device)
50
 
51
  inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
52
 
53
- # 減少生成長度 max_new_tokens
54
  outputs = vl_gpt.language_model.generate(
55
  inputs_embeds=inputs_embeds,
56
  attention_mask=prepare_inputs.attention_mask,
57
  pad_token_id=tokenizer.eos_token_id,
58
  bos_token_id=tokenizer.bos_token_id,
59
  eos_token_id=tokenizer.eos_token_id,
60
- max_new_tokens=128, # 原本 512 → 128
61
  do_sample=False,
62
  use_cache=True
63
  )
64
 
65
  answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
66
- answers.append(f"{prepare_inputs['sft_format'][0]} {answer}")
67
 
68
- # 輸出結果
69
- for ans in answers:
70
- print(ans)
 
 
 
 
 
 
 
 
 
1
  import torch
2
+ import gradio as gr
3
  from transformers import AutoModelForCausalLM, BitsAndBytesConfig
4
  from deepseek_vl.models import VLChatProcessor
5
  from deepseek_vl.utils.io import load_pil_images
 
7
  # 模型路徑
8
  model_path = "deepseek-ai/deepseek-vl-7b-chat"
9
 
10
+ # 載入 processor 和 tokenizer
11
  vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)
12
  tokenizer = vl_chat_processor.tokenizer
13
 
14
+ # ==== 量化模型設定 (4-bit) ====
15
  bnb_config = BitsAndBytesConfig(
16
+ load_in_4bit=True,
17
  bnb_4bit_compute_dtype=torch.float16,
18
  bnb_4bit_use_double_quant=True
19
  )
 
26
  )
27
  vl_gpt.eval()
28
 
29
+ # ==== 單張圖片處理 + 減少 max_new_tokens ====
30
+ def generate_answer(image, text):
31
+ conversation = [
32
+ {"role": "User", "content": "<image_placeholder>" + text, "images": [image]},
33
+ {"role": "Assistant", "content": ""}
34
+ ]
 
 
 
 
 
 
35
 
36
+ pil_images = load_pil_images(conversation)
 
 
 
37
  prepare_inputs = vl_chat_processor(
38
+ conversations=conversation,
39
  images=pil_images,
40
  force_batchify=True
41
  ).to(vl_gpt.device)
42
 
43
  inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
44
 
 
45
  outputs = vl_gpt.language_model.generate(
46
  inputs_embeds=inputs_embeds,
47
  attention_mask=prepare_inputs.attention_mask,
48
  pad_token_id=tokenizer.eos_token_id,
49
  bos_token_id=tokenizer.bos_token_id,
50
  eos_token_id=tokenizer.eos_token_id,
51
+ max_new_tokens=128, # 降低生成長度
52
  do_sample=False,
53
  use_cache=True
54
  )
55
 
56
  answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
57
+ return f"{prepare_inputs['sft_format'][0]} {answer}"
58
 
59
+ # ==== Gradio Web UI ====
60
+ demo = gr.Interface(
61
+ fn=generate_answer,
62
+ inputs=[gr.Image(type="pil", label="Upload Image"), gr.Textbox(label="Question")],
63
+ outputs="text",
64
+ title="DeepSeek-VL-7B Chat Demo",
65
+ description="上傳圖片並輸入問題,模型會生成與圖片相關的回答(4-bit 量化,低記憶體模式)"
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ demo.launch()