khang119966 commited on
Commit
3f4d030
1 Parent(s): 96ed8d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -18,6 +18,8 @@ from PIL import Image
18
  import torch
19
  import spaces
20
  import subprocess
 
 
21
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
22
 
23
  torch.set_default_device('cuda')
@@ -108,29 +110,43 @@ tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-3B-beta", trust_remote
108
 
109
  @spaces.GPU
110
  def chat(message, history):
111
- print(history)
112
- print(message)
 
113
  if len(history) == 0 or len(message["files"]) != 0:
114
  test_image = message["files"][0]["path"]
115
- else:
 
 
 
116
  test_image = history[0][0][0]
 
 
 
117
 
118
- pixel_values = load_image(test_image, max_num=6).to(torch.bfloat16).cuda()
119
- generation_config = dict(max_new_tokens= 512, do_sample=False, num_beams = 3, repetition_penalty=4.0)
120
-
121
 
 
122
 
123
  if len(history) == 0:
124
- question = '<image>\n'+message["text"]
 
 
 
125
  response, conv_history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
126
  else:
 
127
  conv_history = []
128
  for chat_pair in history:
129
  if chat_pair[1] is not None:
130
  if len(conv_history) == 0 and len(message["files"]) == 0:
131
  chat_pair[0] = '<image>\n' + chat_pair[0]
132
  conv_history.append(tuple(chat_pair))
133
- print(conv_history)
 
 
 
 
 
134
  if len(message["files"]) != 0:
135
  question = '<image>\n'+message["text"]
136
  else:
 
18
  import torch
19
  import spaces
20
  import subprocess
21
+ import os
22
+
23
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
24
 
25
  torch.set_default_device('cuda')
 
110
 
111
  @spaces.GPU
112
  def chat(message, history):
113
+ print("history",history)
114
+ print("message",message)
115
+
116
  if len(history) == 0 or len(message["files"]) != 0:
117
  test_image = message["files"][0]["path"]
118
+ pixel_values = load_image(test_image, max_num=6).to(torch.bfloat16).cuda()
119
+ elif len(history) == 0 or len(message["files"]) == 0:
120
+ pixel_values = None
121
+ elif history[0][0][0] is not None and os.path.isfile(history[0][0][0]):
122
  test_image = history[0][0][0]
123
+ pixel_values = load_image(test_image, max_num=6).to(torch.bfloat16).cuda()
124
+ else:
125
+ pixel_values = None
126
 
 
 
 
127
 
128
+ generation_config = dict(max_new_tokens= 512, do_sample=False, num_beams = 3, repetition_penalty=2.0)
129
 
130
  if len(history) == 0:
131
+ if pixel_values is not None:
132
+ question = '<image>\n'+message["text"]
133
+ else:
134
+ question = message["text"]
135
  response, conv_history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
136
  else:
137
+ ### get the last image
138
  conv_history = []
139
  for chat_pair in history:
140
  if chat_pair[1] is not None:
141
  if len(conv_history) == 0 and len(message["files"]) == 0:
142
  chat_pair[0] = '<image>\n' + chat_pair[0]
143
  conv_history.append(tuple(chat_pair))
144
+ ### not have image
145
+ if len(conv_history) == 0:
146
+ for chat_pair in history:
147
+ conv_history.append(tuple(chat_pair))
148
+
149
+ print("conv_history",conv_history)
150
  if len(message["files"]) != 0:
151
  question = '<image>\n'+message["text"]
152
  else: