sitammeur commited on
Commit
5fc0263
·
verified ·
1 Parent(s): 0dc8cc8

Update src/app/response.py

Browse files
Files changed (1) hide show
  1. src/app/response.py +79 -79
src/app/response.py CHANGED
@@ -1,79 +1,79 @@
1
- # Necessary imports
2
- import sys
3
- from typing import Any, Dict
4
- import spaces
5
-
6
- # Local imports
7
- from src.utils.video_processing import encode_video
8
- from src.config import (
9
- device,
10
- model_name,
11
- system_prompt,
12
- sampling,
13
- stream,
14
- top_p,
15
- top_k,
16
- temperature,
17
- repetition_penalty,
18
- max_new_tokens,
19
- )
20
- from src.app.model import load_model_and_tokenizer
21
- from src.logger import logging
22
- from src.exception import CustomExceptionHandling
23
-
24
-
25
- # Model and tokenizer
26
- model, tokenizer = load_model_and_tokenizer(model_name, device)
27
-
28
-
29
- @spaces.GPU()
30
- def describe_video(video: str, question: str) -> str:
31
- """
32
- Describes a video by generating an answer to a given question.
33
-
34
- Args:
35
- - video (str): The path to the video file.
36
- - question (str): The question to be answered about the video.
37
-
38
- Returns:
39
- str: The generated answer to the question.
40
- """
41
- try:
42
- # Encode the video frames
43
- frames = encode_video(video)
44
-
45
- # Message format for the model
46
- msgs = [{"role": "user", "content": frames + [question]}]
47
-
48
- # Set decode params for video
49
- params: Dict[str, Any] = {
50
- "use_image_id": False,
51
- "max_slice_nums": 1, # Use 1 if CUDA OOM and video resolution > 448*448
52
- }
53
-
54
- # Generate the answer
55
- answer = model.chat(
56
- image=None,
57
- msgs=msgs,
58
- tokenizer=tokenizer,
59
- sampling=sampling,
60
- stream=stream,
61
- top_p=top_p,
62
- top_k=top_k,
63
- temperature=temperature,
64
- repetition_penalty=repetition_penalty,
65
- max_new_tokens=max_new_tokens,
66
- system_prompt=system_prompt,
67
- **params
68
- )
69
-
70
- # Log the successful generation of the answer
71
- logging.info("Answer generated successfully.")
72
-
73
- # Return the answer
74
- return " ".join(answer)
75
-
76
- # Handle exceptions that may occur during answer generation
77
- except Exception as e:
78
- # Custom exception handling
79
- raise CustomExceptionHandling(e, sys) from e
 
1
+ # Necessary imports
2
+ import sys
3
+ from typing import Any, Dict
4
+ import spaces
5
+
6
+ # Local imports
7
+ from src.utils.video_processing import encode_video
8
+ from src.config import (
9
+ device,
10
+ model_name,
11
+ system_prompt,
12
+ sampling,
13
+ stream,
14
+ top_p,
15
+ top_k,
16
+ temperature,
17
+ repetition_penalty,
18
+ max_new_tokens,
19
+ )
20
+ from src.app.model import load_model_and_tokenizer
21
+ from src.logger import logging
22
+ from src.exception import CustomExceptionHandling
23
+
24
+
25
+ # Model and tokenizer
26
+ model, tokenizer = load_model_and_tokenizer(model_name, device)
27
+
28
+
29
+ @spaces.GPU(duration=120)
30
+ def describe_video(video: str, question: str) -> str:
31
+ """
32
+ Describes a video by generating an answer to a given question.
33
+
34
+ Args:
35
+ - video (str): The path to the video file.
36
+ - question (str): The question to be answered about the video.
37
+
38
+ Returns:
39
+ str: The generated answer to the question.
40
+ """
41
+ try:
42
+ # Encode the video frames
43
+ frames = encode_video(video)
44
+
45
+ # Message format for the model
46
+ msgs = [{"role": "user", "content": frames + [question]}]
47
+
48
+ # Set decode params for video
49
+ params: Dict[str, Any] = {
50
+ "use_image_id": False,
51
+ "max_slice_nums": 1, # Use 1 if CUDA OOM and video resolution > 448*448
52
+ }
53
+
54
+ # Generate the answer
55
+ answer = model.chat(
56
+ image=None,
57
+ msgs=msgs,
58
+ tokenizer=tokenizer,
59
+ sampling=sampling,
60
+ stream=stream,
61
+ top_p=top_p,
62
+ top_k=top_k,
63
+ temperature=temperature,
64
+ repetition_penalty=repetition_penalty,
65
+ max_new_tokens=max_new_tokens,
66
+ system_prompt=system_prompt,
67
+ **params
68
+ )
69
+
70
+ # Log the successful generation of the answer
71
+ logging.info("Answer generated successfully.")
72
+
73
+ # Return the answer
74
+ return " ".join(answer)
75
+
76
+ # Handle exceptions that may occur during answer generation
77
+ except Exception as e:
78
+ # Custom exception handling
79
+ raise CustomExceptionHandling(e, sys) from e