abdurafeyf commited on
Commit
cf633a8
·
verified ·
1 Parent(s): 298a680

added handler.py

Browse files
Files changed (1) hide show
  1. handler.py +113 -0
handler.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ import json
4
+
5
+ import torch
6
+ from unsloth import FastVisionModel
7
+ from PIL import Image
8
+
9
+ # Global variables to hold the model and tokenizer.
10
+ model = None
11
+ tokenizer = None
12
+
13
+ def initialize():
14
+ """
15
+ Called once when the model is loaded.
16
+ Loads the model and tokenizer from the pretrained checkpoint
17
+ and prepares the model for inference.
18
+ """
19
+ global model, tokenizer
20
+ model, tokenizer = FastVisionModel.from_pretrained(
21
+ "abdurafeyf/Radixpert",
22
+ device_map="cuda"
23
+ )
24
+ FastVisionModel.for_inference(model)
25
+
26
+ def inference(payload):
27
+ """
28
+ Expects a payload that is either a dict or a JSON string with the following format:
29
+
30
+ {
31
+ "data": {
32
+ "image": "<base64-encoded image string>",
33
+ "instruction": "<text instruction>"
34
+ }
35
+ }
36
+
37
+ The function decodes the image, applies the chat template to the instruction,
38
+ tokenizes both image and text, runs the model's generate method, and returns
39
+ the generated text as output.
40
+ """
41
+ global model, tokenizer
42
+ try:
43
+ # If payload is a JSON string, decode it.
44
+ if isinstance(payload, str):
45
+ payload = json.loads(payload)
46
+
47
+ data = payload.get("data")
48
+ if data is None:
49
+ return {"error": "Missing 'data' in payload."}
50
+
51
+ image_b64 = data.get("image")
52
+ instruction = data.get("instruction")
53
+ if image_b64 is None or instruction is None:
54
+ return {"error": "Both 'image' and 'instruction' are required in the payload."}
55
+
56
+ # Decode the base64-encoded image and load it.
57
+ image_bytes = base64.b64decode(image_b64)
58
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
59
+
60
+ # Construct the chat messages as expected by the tokenizer.
61
+ messages = [
62
+ {
63
+ "role": "user",
64
+ "content": [
65
+ {"type": "image"},
66
+ {"type": "text", "text": instruction}
67
+ ]
68
+ }
69
+ ]
70
+ input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
71
+
72
+ # Tokenize both image and text inputs.
73
+ inputs = tokenizer(
74
+ image,
75
+ input_text,
76
+ add_special_tokens=False,
77
+ return_tensors="pt",
78
+ ).to("cuda")
79
+
80
+ # Generate output tokens.
81
+ outputs = model.generate(
82
+ **inputs,
83
+ max_new_tokens=128,
84
+ use_cache=True,
85
+ temperature=1.5,
86
+ min_p=0.1
87
+ )
88
+
89
+ # Decode the tokens to obtain the generated text.
90
+ output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
91
+ return {"output": output_text}
92
+
93
+ except Exception as e:
94
+ return {"error": str(e)}
95
+
96
+ # Optional: For local testing of the handler.
97
+ if __name__ == "__main__":
98
+ # Run initialization.
99
+ initialize()
100
+
101
+ # Example payload (you can replace with an actual base64-encoded image string).
102
+ sample_payload = {
103
+ "data": {
104
+ "image": "", # Insert a valid base64-encoded image string here.
105
+ "instruction": (
106
+ "You are an expert radiologist. Describe accurately in detail like a radiology report "
107
+ "what you see in this X-Ray Scan of a Chest."
108
+ )
109
+ }
110
+ }
111
+
112
+ result = inference(sample_payload)
113
+ print(result)