arjunanand13 commited on
Commit
62dfa41
·
verified ·
1 Parent(s): afa5b9b

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +151 -0
handler.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import sys
3
+ import torch
4
+ import base64
5
+ from io import BytesIO
6
+ from PIL import Image
7
+ import requests
8
+ from transformers import AutoModelForCausalLM, AutoProcessor
9
+
10
+ def install(package):
11
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-warn-script-location", package])
12
+
13
+ class EndpointHandler:
14
+ def __init__(self, path=""):
15
+ required_packages = ['timm', 'einops', 'flash-attn', 'Pillow']
16
+ for package in required_packages:
17
+ try:
18
+ install(package)
19
+ print(f"Successfully installed {package}")
20
+ except Exception as e:
21
+ print(f"Failed to install {package}: {str(e)}")
22
+
23
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
24
+ print(f"Using device: {self.device}")
25
+
26
+ self.model_name = "microsoft/Florence-2-base-ft"
27
+ self.model = AutoModelForCausalLM.from_pretrained(
28
+ self.model_name,
29
+ trust_remote_code=True,
30
+ revision='refs/pr/6'
31
+ ).to(self.device)
32
+
33
+ self.processor = AutoProcessor.from_pretrained(
34
+ self.model_name,
35
+ trust_remote_code=True,
36
+ revision='refs/pr/6'
37
+ )
38
+
39
+ if torch.cuda.is_available():
40
+ torch.cuda.empty_cache()
41
+
42
+ def process_image(self, image_path):
43
+ try:
44
+ with open(image_path, 'rb') as image_file:
45
+ image = Image.open(image_file)
46
+ return image
47
+ except Exception as e:
48
+ print(f"Error processing image: {str(e)}")
49
+ return None
50
+
51
+ def __call__(self, data):
52
+ try:
53
+ # Extract inputs from the expected Hugging Face format
54
+ inputs = data.pop("inputs", data)
55
+
56
+ # Check if inputs is a dict or string
57
+ if isinstance(inputs, dict):
58
+ image_path = inputs.get("image", None)
59
+ text_input = inputs.get("text", "")
60
+ else:
61
+ # If inputs is not a dict, assume it's the image path
62
+ image_path = inputs
63
+ text_input = "What is in this image?"
64
+
65
+ # Process image
66
+ image = self.process_image(image_path) if image_path else None
67
+
68
+ # Prepare inputs for the model
69
+ model_inputs = self.processor(
70
+ images=image if image else None,
71
+ text=text_input,
72
+ return_tensors="pt"
73
+ )
74
+
75
+ # Move inputs to device
76
+ model_inputs = {k: v.to(self.device) if isinstance(v, torch.Tensor) else v
77
+ for k, v in model_inputs.items()}
78
+
79
+ # Generate output
80
+ with torch.no_grad():
81
+ outputs = self.model.generate(**model_inputs)
82
+
83
+ # Decode outputs
84
+ decoded_outputs = self.processor.batch_decode(outputs, skip_special_tokens=True)
85
+
86
+ return {"generated_text": decoded_outputs[0]}
87
+
88
+ except Exception as e:
89
+ return {"error": str(e)}
90
+ # import subprocess
91
+ # import sys
92
+ # import torch
93
+ # from transformers import AutoModelForCausalLM, AutoProcessor
94
+
95
+ # def install(package):
96
+ # subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-warn-script-location", package])
97
+
98
+ # class EndpointHandler:
99
+ # def __init__(self, path=""):
100
+
101
+ # required_packages = ['timm', 'einops', 'flash-attn']
102
+ # for package in required_packages:
103
+ # try:
104
+ # install(package)
105
+ # print(f"Successfully installed {package}")
106
+ # except Exception as e:
107
+ # print(f"Failed to install {package}: {str(e)}")
108
+
109
+
110
+ # self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
111
+ # print(f"Using device: {self.device}")
112
+
113
+
114
+ # self.model_name = "microsoft/Florence-2-base-ft"
115
+ # self.model = AutoModelForCausalLM.from_pretrained(
116
+ # self.model_name,
117
+ # trust_remote_code=True,
118
+ # revision='refs/pr/6'
119
+ # ).to(self.device)
120
+
121
+ # self.processor = AutoProcessor.from_pretrained(
122
+ # self.model_name,
123
+ # trust_remote_code=True,
124
+ # revision='refs/pr/6'
125
+ # )
126
+
127
+
128
+ # if torch.cuda.is_available():
129
+ # torch.cuda.empty_cache()
130
+
131
+ # def __call__(self, data):
132
+ # try:
133
+
134
+ # inputs = data.pop("inputs", data)
135
+
136
+
137
+ # processed_inputs = self.processor(inputs, return_tensors="pt")
138
+
139
+
140
+ # processed_inputs = {k: v.to(self.device) for k, v in processed_inputs.items()}
141
+
142
+
143
+ # with torch.no_grad():
144
+ # outputs = self.model.generate(**processed_inputs)
145
+
146
+
147
+ # decoded_outputs = self.processor.batch_decode(outputs, skip_special_tokens=True)
148
+
149
+ # return {"outputs": decoded_outputs}
150
+ # except Exception as e:
151
+ # return {"error": str(e)}