Spaces:
Running
on
Zero
Running
on
Zero
added history processing
Browse files- src/app.py +40 -3
src/app.py
CHANGED
|
@@ -59,6 +59,7 @@ def get_frames(video_path: str, max_images: int) -> list[tuple[Image.Image, floa
|
|
| 59 |
capture.release()
|
| 60 |
return frames
|
| 61 |
|
|
|
|
| 62 |
def process_video(video_path: str, max_images: int) -> list[dict]:
|
| 63 |
result_content = []
|
| 64 |
# TODO: Change max_image to slider
|
|
@@ -69,17 +70,53 @@ def process_video(video_path: str, max_images: int) -> list[dict]:
|
|
| 69 |
image.save(temp_file.name)
|
| 70 |
result_content.append({"type": "text", "text": f"Frame {timestamp}:"})
|
| 71 |
result_content.append({"type": "image", "url": temp_file.name})
|
| 72 |
-
logger.debug(
|
|
|
|
|
|
|
| 73 |
return result_content
|
| 74 |
|
|
|
|
| 75 |
def process_user_input(message: dict, max_images: int) -> list[dict]:
|
| 76 |
if not message["files"]:
|
| 77 |
return [{"type": "text", "text": message["text"]}]
|
| 78 |
|
| 79 |
if message["files"][0].endswith(".mp4"):
|
| 80 |
-
return [
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
return [
|
| 83 |
{"type": "text", "text": message["text"]},
|
| 84 |
*[{"type": "image", "url": path} for path in message["files"]],
|
| 85 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
capture.release()
|
| 60 |
return frames
|
| 61 |
|
| 62 |
+
|
| 63 |
def process_video(video_path: str, max_images: int) -> list[dict]:
|
| 64 |
result_content = []
|
| 65 |
# TODO: Change max_image to slider
|
|
|
|
| 70 |
image.save(temp_file.name)
|
| 71 |
result_content.append({"type": "text", "text": f"Frame {timestamp}:"})
|
| 72 |
result_content.append({"type": "image", "url": temp_file.name})
|
| 73 |
+
logger.debug(
|
| 74 |
+
f"Processed {len(frames)} frames from video {video_path} with frames {result_content}"
|
| 75 |
+
)
|
| 76 |
return result_content
|
| 77 |
|
| 78 |
+
|
| 79 |
def process_user_input(message: dict, max_images: int) -> list[dict]:
|
| 80 |
if not message["files"]:
|
| 81 |
return [{"type": "text", "text": message["text"]}]
|
| 82 |
|
| 83 |
if message["files"][0].endswith(".mp4"):
|
| 84 |
+
return [
|
| 85 |
+
{"type": "text", "text": message["text"]},
|
| 86 |
+
*process_video(message["files"][0], max_images),
|
| 87 |
+
]
|
| 88 |
|
| 89 |
return [
|
| 90 |
{"type": "text", "text": message["text"]},
|
| 91 |
*[{"type": "image", "url": path} for path in message["files"]],
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def process_history(history: list[dict]) -> list[dict]:
|
| 96 |
+
messages = []
|
| 97 |
+
user_content_buffer = []
|
| 98 |
+
|
| 99 |
+
for item in history:
|
| 100 |
+
if item["role"] == "assistant":
|
| 101 |
+
if user_content_buffer:
|
| 102 |
+
messages.append({"role": "user", "content": user_content_buffer})
|
| 103 |
+
user_content_buffer = []
|
| 104 |
+
|
| 105 |
+
messages.append(
|
| 106 |
+
{
|
| 107 |
+
"role": "assistant",
|
| 108 |
+
"content": [{"type": "text", "text": item["content"]}],
|
| 109 |
+
}
|
| 110 |
+
)
|
| 111 |
+
else:
|
| 112 |
+
content = item["content"]
|
| 113 |
+
user_content_buffer.append(
|
| 114 |
+
{"type": "text", "text": content}
|
| 115 |
+
if isinstance(content, str)
|
| 116 |
+
else {"type": "image", "url": content[0]}
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
if user_content_buffer:
|
| 120 |
+
messages.append({"role": "user", "content": user_content_buffer})
|
| 121 |
+
|
| 122 |
+
return messages
|