Spaces:
Running
on
Zero
Running
on
Zero
Update vlm.py
Browse files
vlm.py
CHANGED
|
@@ -51,12 +51,29 @@ def encode_image(image_path):
|
|
| 51 |
# Build messages
|
| 52 |
#
|
| 53 |
def normalize_message_content(msg: dict) -> dict:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return {
|
| 56 |
"role": msg["role"],
|
| 57 |
-
"content": [
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def build_messages(message: dict, history: list[dict]):
|
| 62 |
"""Build messages given message & history from a **multimodal** chat interface.
|
|
|
|
| 51 |
# Build messages
|
| 52 |
#
|
| 53 |
def normalize_message_content(msg: dict) -> dict:
|
| 54 |
+
content = msg.get("content")
|
| 55 |
+
|
| 56 |
+
# Case 1: Already in expected format
|
| 57 |
+
if isinstance(content, list) and all(isinstance(item, dict) for item in content):
|
| 58 |
+
return {"role": msg["role"], "content": content}
|
| 59 |
+
|
| 60 |
+
# Case 2: String (assume text)
|
| 61 |
+
if isinstance(content, str):
|
| 62 |
+
return {"role": msg["role"], "content": [{"type": "text", "text": content}]}
|
| 63 |
+
|
| 64 |
+
# Case 3: Tuple with image path(s)
|
| 65 |
+
if isinstance(content, tuple):
|
| 66 |
return {
|
| 67 |
"role": msg["role"],
|
| 68 |
+
"content": [
|
| 69 |
+
{"type": "image", "image": encode_image(path)} # your `encode_image()` function
|
| 70 |
+
for path in content if isinstance(path, str)
|
| 71 |
+
]
|
| 72 |
}
|
| 73 |
+
|
| 74 |
+
logger.warning(f"Unexpected content format in message: {msg}")
|
| 75 |
+
return {"role": msg["role"], "content": [{"type": "text", "text": str(content)}]}
|
| 76 |
+
|
| 77 |
|
| 78 |
def build_messages(message: dict, history: list[dict]):
|
| 79 |
"""Build messages given message & history from a **multimodal** chat interface.
|