hanzla commited on
Commit
babdbe7
1 Parent(s): cff67c8

chat interface new

Browse files
Files changed (1) hide show
  1. app_v2.py +12 -7
app_v2.py CHANGED
@@ -37,23 +37,21 @@ def bot(history):
37
  # Reverse search through the last 5 messages for an image file
38
  last_five_messages = history[-5:] # Get the last five messages
39
  image_path = None
 
40
  for message in reversed(last_five_messages):
41
  if isinstance(message[0], tuple) and isinstance(message[0][0], str):
42
  image_path = message[0][0]
43
- break
44
-
45
  if image_path:
46
  try:
47
  image = Image.open(image_path) # Try to open the image using Pillow
48
  image_embeds = moondream.encode_image(image)
49
- print(moondream.answer_question(image_embeds, "Describe this image.", tokenizer))
50
- response = f"Successfully loaded image from path: {image_path}"
51
  except IOError:
52
  response = "Failed to open image. Please check the image path or file permissions."
53
- elif isinstance(history[-1][0], str):
54
- response = "HOLA, it's a string" # Handle text messages
55
  else:
56
- response = "**I can only process text messages and images. Please send some text or upload an image!**"
57
 
58
  history[-1][1] = ""
59
  for character in response:
@@ -61,6 +59,13 @@ def bot(history):
61
  yield history
62
 
63
  with gr.Blocks(theme="Monochrome") as demo:
 
 
 
 
 
 
 
64
  chatbot = gr.Chatbot(
65
  [],
66
  elem_id="chatbot",
 
37
  # Reverse search through the last 5 messages for an image file
38
  last_five_messages = history[-5:] # Get the last five messages
39
  image_path = None
40
+ last_message = None
41
  for message in reversed(last_five_messages):
42
  if isinstance(message[0], tuple) and isinstance(message[0][0], str):
43
  image_path = message[0][0]
44
+ if isinstance(message[0],str):
45
+ last_message = message[0]
46
  if image_path:
47
  try:
48
  image = Image.open(image_path) # Try to open the image using Pillow
49
  image_embeds = moondream.encode_image(image)
50
+ response = moondream.answer_question(image_embeds, last_message, tokenizer)
 
51
  except IOError:
52
  response = "Failed to open image. Please check the image path or file permissions."
 
 
53
  else:
54
+ response = moondream.answer_question(question=last_message,tokenizer=tokenizer)
55
 
56
  history[-1][1] = ""
57
  for character in response:
 
59
  yield history
60
 
61
  with gr.Blocks(theme="Monochrome") as demo:
62
+ gr.Markdown(
63
+ """
64
+ # AskMoondream: Moondream 2 Demonstration Space
65
+ Moondream2 is a 1.86B parameter model initialized with weights from SigLIP and Phi 1.5.
66
+ Modularity AI presents this open source huggingface space for running fast experimental inferences on Moondream2.
67
+ """
68
+ )
69
  chatbot = gr.Chatbot(
70
  [],
71
  elem_id="chatbot",