SebastianRuff commited on
Commit
5258acf
·
verified ·
1 Parent(s): 354cb05

Add custom handler.py for inference logic

Browse files
Files changed (1) hide show
  1. handler.py +51 -0
handler.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoProcessor
2
+ from PIL import Image
3
+ import requests
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, model_dir):
7
+ # Load the model with trust_remote_code=True
8
+ self.model = AutoModelForCausalLM.from_pretrained(
9
+ model_dir,
10
+ trust_remote_code=True
11
+ ).eval().cuda() # Use .cuda() if GPU is available, otherwise remove
12
+ self.processor = AutoProcessor.from_pretrained(
13
+ model_dir,
14
+ trust_remote_code=True
15
+ )
16
+
17
+ def __call__(self, data):
18
+ # Extract inputs from the request data
19
+ task_prompt = data.get("task_prompt", "<MORE_DETAILED_CAPTION>")
20
+ image_url = data.get("image_url")
21
+
22
+ # Load and process the image
23
+ image = self.load_image(image_url)
24
+
25
+ # Prepare inputs for the model
26
+ inputs = self.processor(
27
+ text=task_prompt,
28
+ images=image,
29
+ return_tensors="pt"
30
+ ).to("cuda") # Use "cpu" if GPU is not available
31
+
32
+ # Generate output
33
+ generated_ids = self.model.generate(
34
+ input_ids=inputs["input_ids"],
35
+ pixel_values=inputs["pixel_values"],
36
+ max_new_tokens=1024,
37
+ num_beams=3,
38
+ )
39
+
40
+ # Decode and post-process the output
41
+ generated_text = self.processor.batch_decode(
42
+ generated_ids,
43
+ skip_special_tokens=True
44
+ )[0]
45
+
46
+ return {"caption": generated_text}
47
+
48
+ def load_image(self, image_url):
49
+ # Load image from the provided URL
50
+ image = Image.open(requests.get(image_url, stream=True).raw).convert("RGB")
51
+ return image