kiddobellamy
commited on
Commit
•
67e8921
1
Parent(s):
ebfa455
Update handler.py
Browse files- handler.py +35 -71
handler.py
CHANGED
@@ -1,88 +1,52 @@
|
|
1 |
-
# handler.py
|
2 |
-
|
3 |
import torch
|
4 |
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
5 |
from PIL import Image
|
6 |
import base64
|
7 |
import io
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
self.model = None
|
12 |
-
self.processor = None
|
13 |
-
|
14 |
-
def initialize(self):
|
15 |
-
# Cargar el modelo y el procesador
|
16 |
-
model_id = "meta-llama/Llama-3.2-90B-Vision-Instruct"
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
self.model.eval()
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
text_input = request.get('text', '')
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
if isinstance(image_data, str):
|
39 |
-
image_bytes = base64.b64decode(image_data)
|
40 |
-
image = Image.open(io.BytesIO(image_bytes))
|
41 |
-
else:
|
42 |
-
# Si los datos de imagen son bytes crudos
|
43 |
-
image = Image.open(io.BytesIO(image_data))
|
44 |
-
else:
|
45 |
-
image = None # Manejar casos donde no se proporciona imagen
|
46 |
|
47 |
-
#
|
48 |
messages = [
|
49 |
-
{
|
50 |
-
"
|
51 |
-
"
|
52 |
-
|
53 |
-
{"type": "text", "text": text_input}
|
54 |
-
]
|
55 |
-
}
|
56 |
]
|
|
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
# Procesar las entradas
|
61 |
-
inputs = self.processor(image, input_text, return_tensors="pt").to(self.model.device)
|
62 |
-
|
63 |
-
# Generar salida
|
64 |
-
with torch.no_grad():
|
65 |
-
outputs = self.model.generate(**inputs, max_new_tokens=50)
|
66 |
-
|
67 |
-
# Decodificar la salida
|
68 |
-
response = self.processor.decode(outputs[0], skip_special_tokens=True)
|
69 |
-
return response
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
# Cargar una imagen de ejemplo y codificarla en base64
|
75 |
-
with open('ruta_a_tu_imagen.jpg', 'rb') as f:
|
76 |
-
image_bytes = f.read()
|
77 |
-
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
'image': image_base64,
|
82 |
-
'text': 'Por favor, describe esta imagen en detalle.'
|
83 |
-
}
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
#000
|
|
|
|
|
|
|
1 |
import torch
|
2 |
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
3 |
from PIL import Image
|
4 |
import base64
|
5 |
import io
|
6 |
|
7 |
+
# Load model and processor globally
|
8 |
+
model_id = "meta-llama/Llama-3.2-90B-Vision-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
model = MllamaForConditionalGeneration.from_pretrained(
|
11 |
+
model_id,
|
12 |
+
torch_dtype=torch.bfloat16,
|
13 |
+
device_map="auto",
|
14 |
+
)
|
15 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
|
|
16 |
|
17 |
+
def handler(event, context):
|
18 |
+
try:
|
19 |
+
# Parse inputs
|
20 |
+
inputs = event.get('inputs', {})
|
21 |
+
image_base64 = inputs.get('image')
|
22 |
+
prompt = inputs.get('prompt', '')
|
23 |
|
24 |
+
if not image_base64 or not prompt:
|
25 |
+
return {'error': 'Both "image" and "prompt" are required in inputs.'}
|
|
|
26 |
|
27 |
+
# Decode the base64 image
|
28 |
+
image_bytes = base64.b64decode(image_base64)
|
29 |
+
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Prepare the message
|
32 |
messages = [
|
33 |
+
{"role": "user", "content": [
|
34 |
+
{"type": "image"},
|
35 |
+
{"type": "text", "text": prompt}
|
36 |
+
]}
|
|
|
|
|
|
|
37 |
]
|
38 |
+
input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
|
39 |
|
40 |
+
# Process inputs
|
41 |
+
inputs = processor(image, input_text, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# Generate output
|
44 |
+
output_ids = model.generate(**inputs, max_new_tokens=50)
|
45 |
+
generated_text = processor.decode(output_ids[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Return the result
|
48 |
+
return {'generated_text': generated_text}
|
|
|
|
|
|
|
49 |
|
50 |
+
except Exception as e:
|
51 |
+
return {'error': str(e)}
|
52 |
+
#111
|
|