robot0820 commited on
Commit
758f4b1
·
verified ·
1 Parent(s): 19d5284

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -1,18 +1,31 @@
1
  import torch
2
- from transformers import AutoModelForCausalLM
3
-
4
- from deepseek_vl.models import VLChatProcessor, MultiModalityCausalLM
5
  from deepseek_vl.utils.io import load_pil_images
6
 
7
-
8
- # specify the path to the model
9
  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
- vl_gpt: MultiModalityCausalLM = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
14
- vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
16
  conversation = [
17
  {
18
  "role": "User",
@@ -25,28 +38,33 @@ conversation = [
25
  }
26
  ]
27
 
28
- # load images and prepare for inputs
29
- pil_images = load_pil_images(conversation)
30
- prepare_inputs = vl_chat_processor(
31
- conversations=conversation,
32
- images=pil_images,
33
- force_batchify=True
34
- ).to(vl_gpt.device)
35
-
36
- # run image encoder to get the image embeddings
37
- inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
38
-
39
- # run the model to get the response
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
- print(f"{prepare_inputs['sft_format'][0]}", answer)
 
 
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
5
 
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
+ )
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
+ # 範例對話
29
  conversation = [
30
  {
31
  "role": "User",
 
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)