akhaliq HF staff commited on
Commit
7bc7ddc
1 Parent(s): 74018c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -18,14 +18,14 @@ def chat_with_image(message, image, history):
18
  messages = [{"role": "system", "content": "You are a helpful assistant that can analyze images and text."}]
19
 
20
  for human, assistant in history:
21
- if isinstance(human, list) and len(human) == 2 and human[1] is not None:
22
  # This is an image message
23
- encoded_image = encode_image(Image.open(human[1]))
24
  messages.append({
25
  "role": "user",
26
  "content": [
27
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
28
- {"type": "text", "text": human[0]}
29
  ]
30
  })
31
  else:
@@ -77,13 +77,18 @@ with gr.Blocks() as demo:
77
 
78
  def user(user_message, image, history):
79
  if image is not None:
80
- return "", None, history + [[user_message, image], None]
81
  else:
82
- return "", None, history + [[user_message, None], None]
83
 
84
  def bot(history):
85
- user_message, user_image = history[-1][0]
86
- bot_message = chat_with_image(user_message, user_image, history[:-1])
 
 
 
 
 
87
  history[-1][1] = ""
88
  for character in bot_message:
89
  history[-1][1] += character
 
18
  messages = [{"role": "system", "content": "You are a helpful assistant that can analyze images and text."}]
19
 
20
  for human, assistant in history:
21
+ if image is not None and human.startswith("Image uploaded: "):
22
  # This is an image message
23
+ encoded_image = encode_image(Image.open(image))
24
  messages.append({
25
  "role": "user",
26
  "content": [
27
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
28
+ {"type": "text", "text": message}
29
  ]
30
  })
31
  else:
 
77
 
78
  def user(user_message, image, history):
79
  if image is not None:
80
+ return "", None, history + [["Image uploaded: " + user_message, None]]
81
  else:
82
+ return "", None, history + [[user_message, None]]
83
 
84
  def bot(history):
85
+ user_message = history[-1][0]
86
+ image = None
87
+ if user_message.startswith("Image uploaded: "):
88
+ image = history[-2][0].split(": ", 1)[1] # Get the image path from the previous message
89
+ user_message = user_message.split(": ", 1)[1] # Get the actual message
90
+
91
+ bot_message = chat_with_image(user_message, image, history[:-1])
92
  history[-1][1] = ""
93
  for character in bot_message:
94
  history[-1][1] += character