curry tang commited on
Commit
bc1aa2f
·
1 Parent(s): 193c02b
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -30,6 +30,7 @@ def get_default_chat():
30
  def predict(message, history, chat):
31
  print('!!!!!', message, history, chat)
32
  history_len = len(history)
 
33
  if chat is None:
34
  chat = get_default_chat()
35
  history_messages = []
@@ -40,18 +41,19 @@ def predict(message, history, chat):
40
 
41
  if history_len == 0:
42
  history_messages.append(SystemMessage(content=web_prompt))
43
- history_messages.append(HumanMessage(content=message.text))
44
- # else:
45
- # file = message.files[0]
46
- # with Image.open(file.path) as img:
47
- # buffer = io.BytesIO()
48
- # img = img.convert('RGB')
49
- # img.save(buffer, format="JPEG")
50
- # image_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
51
- # history_messages.append(HumanMessage(content=[
52
- # {"type": "text", "text": message.text},
53
- # {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
54
- # ]))
 
55
 
56
  response_message = ''
57
  for chunk in chat.stream(history_messages):
 
30
  def predict(message, history, chat):
31
  print('!!!!!', message, history, chat)
32
  history_len = len(history)
33
+ files_len = len(message.files)
34
  if chat is None:
35
  chat = get_default_chat()
36
  history_messages = []
 
41
 
42
  if history_len == 0:
43
  history_messages.append(SystemMessage(content=web_prompt))
44
+ if files_len == 0:
45
+ history_messages.append(HumanMessage(content=message.text))
46
+ else:
47
+ file = message.files[0]
48
+ with Image.open(file.path) as img:
49
+ buffer = io.BytesIO()
50
+ img = img.convert('RGB')
51
+ img.save(buffer, format="JPEG")
52
+ image_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
53
+ history_messages.append(HumanMessage(content=[
54
+ {"type": "text", "text": message.text},
55
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
56
+ ]))
57
 
58
  response_message = ''
59
  for chunk in chat.stream(history_messages):