arjunanand13 commited on
Commit
f6a1b8c
·
verified ·
1 Parent(s): 4584971

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +145 -32
handler.py CHANGED
@@ -6,6 +6,7 @@ from io import BytesIO
6
  from PIL import Image
7
  import requests
8
  from transformers import AutoModelForCausalLM, AutoProcessor
 
9
  import os
10
 
11
  def install(package):
@@ -13,45 +14,60 @@ def install(package):
13
 
14
  class EndpointHandler:
15
  def __init__(self, path=""):
16
- required_packages = ['timm', 'einops', 'flash-attn', 'Pillow','-U transformers']
 
17
  for package in required_packages:
18
  try:
19
  install(package)
20
  print(f"Successfully installed {package}")
21
  except Exception as e:
22
  print(f"Failed to install {package}: {str(e)}")
23
-
 
24
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25
  print(f"Using device: {self.device}")
26
-
 
27
  self.model_name = "arjunanand13/florence-enphaseall2-25e"
28
  self.model = AutoModelForCausalLM.from_pretrained(
29
- self.model_name,
30
- trust_remote_code=True,
31
  ).to(self.device)
32
-
 
33
  self.processor = AutoProcessor.from_pretrained(
34
- self.model_name,
35
- trust_remote_code=True,
36
  )
37
-
 
 
 
38
  if torch.cuda.is_available():
39
  torch.cuda.empty_cache()
40
 
41
- def process_image(self,image_data):
 
 
 
 
 
 
 
 
 
 
42
  print("[DEBUG] Attempting to process image")
43
  try:
44
- # Check if image_data is a file path
45
  if isinstance(image_data, str) and len(image_data) < 256 and os.path.exists(image_data):
46
  with open(image_data, 'rb') as image_file:
47
  print("[DEBUG] File opened successfully")
48
  image = Image.open(image_file)
49
  else:
50
- # Assume image_data is base64 encoded
51
  print("[DEBUG] Decoding base64 image data")
52
  image_bytes = base64.b64decode(image_data)
53
  image = Image.open(BytesIO(image_bytes))
54
-
55
  print("[DEBUG] Image opened with PIL:", image.format, image.size, image.mode)
56
  return image
57
  except Exception as e:
@@ -59,42 +75,139 @@ class EndpointHandler:
59
  return None
60
 
61
  def __call__(self, data):
 
62
  try:
63
- # Extract inputs from the expected Hugging Face format
64
  inputs = data.pop("inputs", data)
65
-
66
- # Check if inputs is a dict or string
67
  if isinstance(inputs, dict):
68
  image_path = inputs.get("image", None)
69
  text_input = inputs.get("text", "")
70
  else:
71
- # If inputs is not a dict, assume it's the image path
72
  image_path = inputs
73
  text_input = "What is in this image?"
74
- print("[INFO]",image_path,text_input)
75
- # Process image
 
76
  image = self.process_image(image_path) if image_path else None
77
- print("[INFO]",image)
78
- # Prepare inputs for the model
79
  model_inputs = self.processor(
80
  images=image if image else None,
81
  text=text_input,
82
  return_tensors="pt"
83
  )
84
-
85
- # Move inputs to device
86
  model_inputs = {k: v.to(self.device) if isinstance(v, torch.Tensor) else v
87
- for k, v in model_inputs.items()}
88
-
89
- # Generate output
90
  with torch.no_grad():
91
  outputs = self.model.generate(**model_inputs)
92
-
93
- # Decode outputs
94
  decoded_outputs = self.processor.batch_decode(outputs, skip_special_tokens=True)
95
- print(f"[INFO],{decoded_outputs}")
96
- print(f"[INFO],{decoded_outputs[0]}")
97
  return {"generated_text": decoded_outputs[0]}
98
-
99
  except Exception as e:
100
- return {"error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from PIL import Image
7
  import requests
8
  from transformers import AutoModelForCausalLM, AutoProcessor
9
+ from tokenizers import Tokenizer, pre_tokenizers # Ensure tokenizers library is installed
10
  import os
11
 
12
  def install(package):
 
14
 
15
  class EndpointHandler:
16
  def __init__(self, path=""):
17
+ # Install all required packages
18
+ required_packages = ['timm', 'einops', 'flash-attn', 'Pillow', 'tokenizers', '-U transformers']
19
  for package in required_packages:
20
  try:
21
  install(package)
22
  print(f"Successfully installed {package}")
23
  except Exception as e:
24
  print(f"Failed to install {package}: {str(e)}")
25
+
26
+ # Set the device (GPU/CPU)
27
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
28
  print(f"Using device: {self.device}")
29
+
30
+ # Load the model
31
  self.model_name = "arjunanand13/florence-enphaseall2-25e"
32
  self.model = AutoModelForCausalLM.from_pretrained(
33
+ self.model_name,
34
+ trust_remote_code=True
35
  ).to(self.device)
36
+
37
+ # Load the processor
38
  self.processor = AutoProcessor.from_pretrained(
39
+ self.model_name,
40
+ trust_remote_code=True
41
  )
42
+
43
+ # Add a whitespace pre-tokenizer to prevent tokenizer issues
44
+ self.add_pre_tokenizer()
45
+
46
  if torch.cuda.is_available():
47
  torch.cuda.empty_cache()
48
 
49
+ def add_pre_tokenizer(self):
50
+ """Adds a whitespace pre-tokenizer to avoid issues with missing tokenizers."""
51
+ try:
52
+ tokenizer = Tokenizer.from_pretrained(self.model_name)
53
+ tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()
54
+ print("[INFO] Added Whitespace pre-tokenizer.")
55
+ except Exception as e:
56
+ print(f"[ERROR] Failed to add pre-tokenizer: {str(e)}")
57
+
58
+ def process_image(self, image_data):
59
+ """Processes an image from file path or base64-encoded string."""
60
  print("[DEBUG] Attempting to process image")
61
  try:
 
62
  if isinstance(image_data, str) and len(image_data) < 256 and os.path.exists(image_data):
63
  with open(image_data, 'rb') as image_file:
64
  print("[DEBUG] File opened successfully")
65
  image = Image.open(image_file)
66
  else:
 
67
  print("[DEBUG] Decoding base64 image data")
68
  image_bytes = base64.b64decode(image_data)
69
  image = Image.open(BytesIO(image_bytes))
70
+
71
  print("[DEBUG] Image opened with PIL:", image.format, image.size, image.mode)
72
  return image
73
  except Exception as e:
 
75
  return None
76
 
77
  def __call__(self, data):
78
+ """Processes the input data and generates text output."""
79
  try:
 
80
  inputs = data.pop("inputs", data)
81
+
 
82
  if isinstance(inputs, dict):
83
  image_path = inputs.get("image", None)
84
  text_input = inputs.get("text", "")
85
  else:
 
86
  image_path = inputs
87
  text_input = "What is in this image?"
88
+
89
+ print("[INFO] Image path:", image_path, "| Text input:", text_input)
90
+
91
  image = self.process_image(image_path) if image_path else None
92
+
 
93
  model_inputs = self.processor(
94
  images=image if image else None,
95
  text=text_input,
96
  return_tensors="pt"
97
  )
98
+
 
99
  model_inputs = {k: v.to(self.device) if isinstance(v, torch.Tensor) else v
100
+ for k, v in model_inputs.items()}
101
+
 
102
  with torch.no_grad():
103
  outputs = self.model.generate(**model_inputs)
104
+
 
105
  decoded_outputs = self.processor.batch_decode(outputs, skip_special_tokens=True)
106
+ print(f"[INFO] Generated text: {decoded_outputs[0]}")
 
107
  return {"generated_text": decoded_outputs[0]}
108
+
109
  except Exception as e:
110
+ return {"error": str(e)}
111
+
112
+
113
+
114
+ # import subprocess
115
+ # import sys
116
+ # import torch
117
+ # import base64
118
+ # from io import BytesIO
119
+ # from PIL import Image
120
+ # import requests
121
+ # from transformers import AutoModelForCausalLM, AutoProcessor
122
+ # import os
123
+
124
+ # def install(package):
125
+ # subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-warn-script-location", package])
126
+
127
+ # class EndpointHandler:
128
+ # def __init__(self, path=""):
129
+ # required_packages = ['timm', 'einops', 'flash-attn', 'Pillow','-U transformers']
130
+ # for package in required_packages:
131
+ # try:
132
+ # install(package)
133
+ # print(f"Successfully installed {package}")
134
+ # except Exception as e:
135
+ # print(f"Failed to install {package}: {str(e)}")
136
+
137
+ # self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
138
+ # print(f"Using device: {self.device}")
139
+
140
+ # self.model_name = "arjunanand13/florence-enphaseall2-25e"
141
+ # self.model = AutoModelForCausalLM.from_pretrained(
142
+ # self.model_name,
143
+ # trust_remote_code=True,
144
+ # ).to(self.device)
145
+
146
+ # self.processor = AutoProcessor.from_pretrained(
147
+ # self.model_name,
148
+ # trust_remote_code=True,
149
+ # )
150
+
151
+ # if torch.cuda.is_available():
152
+ # torch.cuda.empty_cache()
153
+
154
+ # def process_image(self,image_data):
155
+ # print("[DEBUG] Attempting to process image")
156
+ # try:
157
+ # # Check if image_data is a file path
158
+ # if isinstance(image_data, str) and len(image_data) < 256 and os.path.exists(image_data):
159
+ # with open(image_data, 'rb') as image_file:
160
+ # print("[DEBUG] File opened successfully")
161
+ # image = Image.open(image_file)
162
+ # else:
163
+ # # Assume image_data is base64 encoded
164
+ # print("[DEBUG] Decoding base64 image data")
165
+ # image_bytes = base64.b64decode(image_data)
166
+ # image = Image.open(BytesIO(image_bytes))
167
+
168
+ # print("[DEBUG] Image opened with PIL:", image.format, image.size, image.mode)
169
+ # return image
170
+ # except Exception as e:
171
+ # print(f"[ERROR] Error processing image: {str(e)}")
172
+ # return None
173
+
174
+ # def __call__(self, data):
175
+ # try:
176
+ # # Extract inputs from the expected Hugging Face format
177
+ # inputs = data.pop("inputs", data)
178
+
179
+ # # Check if inputs is a dict or string
180
+ # if isinstance(inputs, dict):
181
+ # image_path = inputs.get("image", None)
182
+ # text_input = inputs.get("text", "")
183
+ # else:
184
+ # # If inputs is not a dict, assume it's the image path
185
+ # image_path = inputs
186
+ # text_input = "What is in this image?"
187
+ # print("[INFO]",image_path,text_input)
188
+ # # Process image
189
+ # image = self.process_image(image_path) if image_path else None
190
+ # print("[INFO]",image)
191
+ # # Prepare inputs for the model
192
+ # model_inputs = self.processor(
193
+ # images=image if image else None,
194
+ # text=text_input,
195
+ # return_tensors="pt"
196
+ # )
197
+
198
+ # # Move inputs to device
199
+ # model_inputs = {k: v.to(self.device) if isinstance(v, torch.Tensor) else v
200
+ # for k, v in model_inputs.items()}
201
+
202
+ # # Generate output
203
+ # with torch.no_grad():
204
+ # outputs = self.model.generate(**model_inputs)
205
+
206
+ # # Decode outputs
207
+ # decoded_outputs = self.processor.batch_decode(outputs, skip_special_tokens=True)
208
+ # print(f"[INFO],{decoded_outputs}")
209
+ # print(f"[INFO],{decoded_outputs[0]}")
210
+ # return {"generated_text": decoded_outputs[0]}
211
+
212
+ # except Exception as e:
213
+ # return {"error": str(e)}