Nymbo commited on
Commit
bc17fe3
·
verified ·
1 Parent(s): 70d58c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -101
app.py CHANGED
@@ -41,7 +41,7 @@ def encode_image(image_path):
41
 
42
  def respond(
43
  message,
44
- image_files, # Changed parameter name and structure
45
  history: list[tuple[str, str]],
46
  system_message,
47
  max_tokens,
@@ -83,79 +83,79 @@ def respond(
83
  if seed == -1:
84
  seed = None
85
 
86
- # Create multimodal content if images are present
 
 
 
 
 
 
 
 
 
 
87
  if image_files and len(image_files) > 0:
88
- # Process the user message to include images
89
- user_content = []
90
-
91
- # Add text part if there is any
92
- if message and message.strip():
93
- user_content.append({
94
- "type": "text",
95
- "text": message
96
- })
97
-
98
- # Add image parts
99
- for img in image_files:
100
- if img is not None:
101
- # Get raw image data from path
102
- try:
103
- encoded_image = encode_image(img)
104
- if encoded_image:
105
- user_content.append({
106
- "type": "image_url",
107
- "image_url": {
108
- "url": f"data:image/jpeg;base64,{encoded_image}"
109
- }
110
- })
111
- except Exception as e:
112
- print(f"Error encoding image: {e}")
113
- else:
114
- # Text-only message
115
- user_content = message
116
-
117
  # Prepare messages in the format expected by the API
118
  messages = [{"role": "system", "content": system_message}]
119
  print("Initial messages array constructed.")
120
 
121
  # Add conversation history to the context
122
  for val in history:
123
- user_part = val[0]
124
- assistant_part = val[1]
125
- if user_part:
126
- # Handle both text-only and multimodal messages in history
127
- if isinstance(user_part, tuple) and len(user_part) == 2:
128
- # This is a multimodal message with text and images
129
- history_content = []
130
- if user_part[0]: # Text
131
- history_content.append({
 
 
 
 
132
  "type": "text",
133
- "text": user_part[0]
134
  })
135
 
136
- for img in user_part[1]: # Images
137
- if img:
138
- try:
139
- encoded_img = encode_image(img)
140
- if encoded_img:
141
- history_content.append({
142
- "type": "image_url",
143
- "image_url": {
144
- "url": f"data:image/jpeg;base64,{encoded_img}"
145
- }
146
- })
147
- except Exception as e:
148
- print(f"Error encoding history image: {e}")
149
 
150
- messages.append({"role": "user", "content": history_content})
 
151
  else:
152
  # Regular text message
153
- messages.append({"role": "user", "content": user_part})
154
- print(f"Added user message to context (type: {type(user_part)})")
155
 
156
- if assistant_part:
157
- messages.append({"role": "assistant", "content": assistant_part})
158
- print(f"Added assistant message to context: {assistant_part}")
159
 
160
  # Append the latest user message
161
  messages.append({"role": "user", "content": user_content})
@@ -409,36 +409,26 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
409
  print("Empty message, skipping")
410
  return history
411
 
412
- # Prepare multimodal message format
413
  text_content = user_message.get("text", "").strip()
414
- files = user_message.get("files", [])
415
 
416
  print(f"Text content: {text_content}")
417
- print(f"Files: {files}")
418
 
419
- # If both text and files are empty, skip
420
- if not text_content and not files:
421
- print("No content to display")
422
- return history
423
-
424
- # Process multimodal content
425
- if files:
426
- # For multimodal messages with files
427
- for file_path in files:
428
- print(f"Processing file: {file_path}")
429
- if not file_path:
430
- continue
431
-
432
- # Add a combined message with text and file
433
- history.append([(text_content, file_path), None])
434
- # Reset text content for subsequent files if there are multiple
435
- text_content = ""
436
 
437
- return history
 
438
  else:
439
- # For text-only messages
440
- history.append([text_content, None])
441
- return history
 
442
 
443
  # Define bot response function
444
  def bot(history, system_msg, max_tokens, temperature, top_p, freq_penalty, seed, provider, api_key, custom_model, search_term, selected_model):
@@ -451,29 +441,19 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
451
  user_message = history[-1][0]
452
  print(f"Processing user message: {user_message}")
453
 
454
- # Determine if the message contains images
455
- text_content = ""
456
- image_files = []
457
-
458
- if isinstance(user_message, tuple):
459
- # Tuple format: (text, image_path)
460
- text_content = user_message[0] if user_message[0] else ""
461
- # Handle both single image path and list of paths
462
- if isinstance(user_message[1], list):
463
- image_files = user_message[1]
464
- else:
465
- image_files = [user_message[1]]
466
- print(f"Multimodal message detected - Text: {text_content}, Images: {image_files}")
467
  else:
468
- # Text-only message
469
- text_content = user_message
470
- print(f"Text-only message detected: {text_content}")
471
 
472
  # Process message through respond function
473
  history[-1][1] = ""
474
  for response in respond(
475
  text_content,
476
- image_files if image_files else None,
477
  history[:-1],
478
  system_msg,
479
  max_tokens,
 
41
 
42
  def respond(
43
  message,
44
+ image_files,
45
  history: list[tuple[str, str]],
46
  system_message,
47
  max_tokens,
 
83
  if seed == -1:
84
  seed = None
85
 
86
+ # Prepare messages for the API
87
+ user_content = []
88
+
89
+ # Add text if there is any
90
+ if message and message.strip():
91
+ user_content.append({
92
+ "type": "text",
93
+ "text": message
94
+ })
95
+
96
+ # Add images if any
97
  if image_files and len(image_files) > 0:
98
+ for file_path in image_files:
99
+ if not file_path:
100
+ continue
101
+
102
+ try:
103
+ print(f"Processing image file: {file_path}")
104
+ # For direct file paths, no need to encode as base64
105
+ user_content.append({
106
+ "type": "image_url",
107
+ "image_url": {
108
+ "url": f"file://{file_path}"
109
+ }
110
+ })
111
+ except Exception as e:
112
+ print(f"Error processing image file: {e}")
113
+
114
+ # If empty content, set to text only
115
+ if not user_content:
116
+ user_content = ""
117
+
 
 
 
 
 
 
 
 
 
118
  # Prepare messages in the format expected by the API
119
  messages = [{"role": "system", "content": system_message}]
120
  print("Initial messages array constructed.")
121
 
122
  # Add conversation history to the context
123
  for val in history:
124
+ user_msg = val[0]
125
+ assistant_msg = val[1]
126
+
127
+ # Process user message
128
+ if user_msg:
129
+ if isinstance(user_msg, dict) and "text" in user_msg:
130
+ # This is a MultimodalTextbox message
131
+ hist_text = user_msg.get("text", "")
132
+ hist_files = user_msg.get("files", [])
133
+
134
+ hist_content = []
135
+ if hist_text:
136
+ hist_content.append({
137
  "type": "text",
138
+ "text": hist_text
139
  })
140
 
141
+ for hist_file in hist_files:
142
+ if hist_file:
143
+ hist_content.append({
144
+ "type": "image_url",
145
+ "image_url": {
146
+ "url": f"file://{hist_file}"
147
+ }
148
+ })
 
 
 
 
 
149
 
150
+ if hist_content:
151
+ messages.append({"role": "user", "content": hist_content})
152
  else:
153
  # Regular text message
154
+ messages.append({"role": "user", "content": user_msg})
 
155
 
156
+ # Process assistant message
157
+ if assistant_msg:
158
+ messages.append({"role": "assistant", "content": assistant_msg})
159
 
160
  # Append the latest user message
161
  messages.append({"role": "user", "content": user_content})
 
409
  print("Empty message, skipping")
410
  return history
411
 
412
+ # Extract data from the MultimodalTextbox
413
  text_content = user_message.get("text", "").strip()
414
+ file_paths = user_message.get("files", [])
415
 
416
  print(f"Text content: {text_content}")
417
+ print(f"Files: {file_paths}")
418
 
419
+ # Process the message
420
+ if file_paths and len(file_paths) > 0:
421
+ # We have files - create a multimodal message
422
+ file_path = file_paths[0] # For simplicity, use the first file
423
+ print(f"Using file: {file_path}")
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
+ # Add the message with both text and file as separate components
426
+ history.append([user_message, None]) # Keep the original format for processing
427
  else:
428
+ # Text-only message
429
+ history.append([{"text": text_content, "files": []}, None])
430
+
431
+ return history
432
 
433
  # Define bot response function
434
  def bot(history, system_msg, max_tokens, temperature, top_p, freq_penalty, seed, provider, api_key, custom_model, search_term, selected_model):
 
441
  user_message = history[-1][0]
442
  print(f"Processing user message: {user_message}")
443
 
444
+ # Get text and files from the message
445
+ if isinstance(user_message, dict) and "text" in user_message:
446
+ text_content = user_message.get("text", "")
447
+ image_files = user_message.get("files", [])
 
 
 
 
 
 
 
 
 
448
  else:
449
+ text_content = ""
450
+ image_files = []
 
451
 
452
  # Process message through respond function
453
  history[-1][1] = ""
454
  for response in respond(
455
  text_content,
456
+ image_files,
457
  history[:-1],
458
  system_msg,
459
  max_tokens,