arjunanand13 commited on
Commit
1c90074
1 Parent(s): 44c8217

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +100 -0
handler.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
10
+
11
+ def install(package):
12
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-warn-script-location", 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/PALD_Florence-15e"
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:
58
+ print(f"[ERROR] Error processing image: {str(e)}")
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)}