Aliayub1995 commited on
Commit
a16ec50
1 Parent(s): 4c4dee6

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +18 -57
handler.py CHANGED
@@ -16,67 +16,28 @@ class EndpointHandler:
16
  self.model_path = 'DAMO-NLP-SG/VideoLLaMA2-7B'
17
  self.model, self.processor, self.tokenizer = model_init(self.model_path)
18
 
19
- def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
20
- """
21
- Handle inference requests.
22
-
23
- Args:
24
- data (Dict[str, Any]): The input data for inference. Expected keys:
25
- - 'modal' (str): 'video' or 'image'
26
- - 'modal_path' (str): Path to the video or image file
27
- - 'instruct' (str): The instruction/query to process
28
-
29
- Returns:
30
- List[Dict[str, Any]]: The output of the inference.
31
- """
32
- modal = data.get("modal", "video")
33
- modal_path = data.get("modal_path", "")
34
- instruct = data.get("instruct", "")
35
 
36
- if not modal_path or not instruct:
37
- raise ValueError("Both 'modal_path' and 'instruct' must be provided in the input data.")
 
38
 
39
- # Perform inference
40
- output = mm_infer(
41
- self.processor[modal](modal_path),
42
- instruct,
43
- model=self.model,
44
- tokenizer=self.tokenizer,
45
- do_sample=False,
46
- modal=modal
47
- )
48
 
49
- return [{"output": output}]
 
50
 
 
 
 
 
 
 
 
 
 
51
 
52
- # from transformers import pipeline
53
 
54
- # class EndpointHandler:
55
- # def __init__(self, path: str = ""):
56
- # """
57
- # Initialize the handler by setting up the environment and loading the model.
58
- # """
59
- # # Use a pipeline as a high-level helper to download and load the model
60
- # self.pipe = pipeline("visual-question-answering", model="DAMO-NLP-SG/VideoLLaMA2-8x7B")
61
- # print("Model downloaded and pipeline created successfully.")
62
 
63
- # def __call__(self, data):
64
- # """
65
- # Handle inference requests.
66
-
67
- # Args:
68
- # data (dict): Input data containing 'image' and 'question'.
69
-
70
- # Returns:
71
- # dict: The output from the model.
72
- # """
73
- # image = data.get("image")
74
- # question = data.get("question")
75
-
76
- # if not image or not question:
77
- # raise ValueError("Both 'image' and 'question' must be provided in the input data.")
78
-
79
- # # Use the pipeline to perform visual question answering
80
- # output = self.pipe(image=image, question=question)
81
-
82
- # return output
 
16
  self.model_path = 'DAMO-NLP-SG/VideoLLaMA2-7B'
17
  self.model, self.processor, self.tokenizer = model_init(self.model_path)
18
 
19
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
20
+ print(f"Received data: {data}") # Debugging: Print received data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ modal = data.get("modal", "video")
23
+ modal_path = data.get("modal_path", "")
24
+ instruct = data.get("instruct", "")
25
 
26
+ print(f"Modal: {modal}, Modal Path: {modal_path}, Instruct: {instruct}") # Debugging: Print extracted values
 
 
 
 
 
 
 
 
27
 
28
+ if not modal_path or not instruct:
29
+ raise ValueError("Both 'modal_path' and 'instruct' must be provided in the input data.")
30
 
31
+ # Perform inference
32
+ output = mm_infer(
33
+ self.processor[modal](modal_path),
34
+ instruct,
35
+ model=self.model,
36
+ tokenizer=self.tokenizer,
37
+ do_sample=False,
38
+ modal=modal
39
+ )
40
 
41
+ return [{"output": output}]
42
 
 
 
 
 
 
 
 
 
43