Skip to content

Commit 54aa943

Browse files
Bugfix for PR 496 to add format_video_name function (#602)
* add format_video_name Signed-off-by: BaoHuiling <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add test for negative case Signed-off-by: BaoHuiling <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update file path Signed-off-by: BaoHuiling <[email protected]> --------- Signed-off-by: BaoHuiling <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b873cf8 commit 54aa943

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

comps/reranks/video-rag-qna/local_reranking.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
import logging
55
import os
6+
import re
67
import time
78

9+
from fastapi import HTTPException
10+
811
from comps import (
912
LVMVideoDoc,
1013
SearchedMultimodalDoc,
@@ -52,6 +55,25 @@ def find_timestamp_from_video(metadata_list, video):
5255
)
5356

5457

58+
def format_video_name(video_name):
59+
# Check for an existing file extension
60+
match = re.search(r"\.(\w+)$", video_name)
61+
62+
if match:
63+
extension = match.group(1)
64+
# If the extension is not 'mp4', raise an error
65+
if extension != "mp4":
66+
raise ValueError(f"Invalid file extension: .{extension}. Only '.mp4' is allowed.")
67+
68+
# Use regex to remove any suffix after the base name (e.g., '_interval_0', etc.)
69+
base_name = re.sub(r"(_interval_\d+)?(\.mp4)?$", "", video_name)
70+
71+
# Add the '.mp4' extension
72+
formatted_name = f"{base_name}.mp4"
73+
74+
return formatted_name
75+
76+
5577
@register_microservice(
5678
name="opea_service@reranking_visual_rag",
5779
service_type=ServiceType.RERANK,
@@ -64,22 +86,30 @@ def find_timestamp_from_video(metadata_list, video):
6486
@register_statistics(names=["opea_service@reranking_visual_rag"])
6587
def reranking(input: SearchedMultimodalDoc) -> LVMVideoDoc:
6688
start = time.time()
89+
try:
90+
# get top video name from metadata
91+
video_names = [meta["video"] for meta in input.metadata]
92+
top_video_names = get_top_doc(input.top_n, video_names)
93+
94+
# only use the first top video
95+
timestamp = find_timestamp_from_video(input.metadata, top_video_names[0])
96+
formatted_video_name = format_video_name(top_video_names[0])
97+
video_url = f"{file_server_endpoint.rstrip('/')}/{formatted_video_name}"
98+
99+
result = LVMVideoDoc(
100+
video_url=video_url,
101+
prompt=input.initial_query,
102+
chunk_start=timestamp,
103+
chunk_duration=float(chunk_duration),
104+
max_new_tokens=512,
105+
)
106+
except ValueError as e:
107+
raise HTTPException(status_code=400, detail=str(e))
108+
except Exception as e:
109+
logging.error(f"Unexpected error in reranking: {str(e)}")
110+
# Handle any other exceptions with a generic server error response
111+
raise HTTPException(status_code=500, detail="An unexpected error occurred.")
67112

68-
# get top video name from metadata
69-
video_names = [meta["video"] for meta in input.metadata]
70-
top_video_names = get_top_doc(input.top_n, video_names)
71-
72-
# only use the first top video
73-
timestamp = find_timestamp_from_video(input.metadata, top_video_names[0])
74-
video_url = f"{file_server_endpoint.rstrip('/')}/{top_video_names[0]}"
75-
76-
result = LVMVideoDoc(
77-
video_url=video_url,
78-
prompt=input.initial_query,
79-
chunk_start=timestamp,
80-
chunk_duration=float(chunk_duration),
81-
max_new_tokens=512,
82-
)
83113
statistics_dict["opea_service@reranking_visual_rag"].append_latency(time.time() - start, None)
84114

85115
return result

comps/retrievers/langchain/vdms/retriever_vdms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
if use_clip:
2929
import sys
3030

31-
sys.path.append("../../../embeddings/langchain_multimodal/")
31+
sys.path.append("../../../embeddings/multimodal_clip/")
3232
from embeddings_clip import vCLIP
3333

3434
# Debugging

tests/test_reranks_video-rag-qna.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,30 @@ function validate_microservice() {
4949
]
5050
}')
5151
if [[ $result == *"this is the query"* ]]; then
52-
echo "Result correct."
52+
echo "Result correct for the positive case."
5353
else
54-
echo "Result wrong."
54+
echo "Result wrong for the positive case. Received was $result"
55+
exit 1
56+
fi
57+
58+
# Add test for negative case
59+
result=$(\
60+
http_proxy="" \
61+
curl -X 'POST' \
62+
"http://${ip_address}:5037/v1/reranking" \
63+
-H 'accept: application/json' \
64+
-H 'Content-Type: application/json' \
65+
-d '{
66+
"retrieved_docs": [
67+
{"doc": [{"text": "this is the retrieved text"}]}
68+
],
69+
"initial_query": "this is the query",
70+
"top_n": 1,
71+
"metadata": [{"other_key": "value", "video":"top_video_name_bad_format.avi", "timestamp":"20"}]}')
72+
if [[ $result == *"Invalid file extension"* ]]; then
73+
echo "Result correct for the negative case."
74+
else
75+
echo "Result wrong for the negative case. Received was $result"
5576
exit 1
5677
fi
5778
}

0 commit comments

Comments
 (0)