wealthcoders commited on
Commit
83535a8
·
verified ·
1 Parent(s): 9920ad7

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +87 -0
handler.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
2
+ from typing import Dict, List, Any
3
+ import torch
4
+ import io
5
+ from PIL import Image
6
+ import base64
7
+ import time
8
+ import uuid
9
+
10
+ prompt = """**Task**:
11
+ Analyze this document image exhaustively and output in Markdown format.
12
+ **Rules**:
13
+ - Do not add any comments, provide content only;
14
+ - Extract ALL visible text exactly as written;
15
+ - Preserve possible additional languages;
16
+ - Maintain line breaks, indentation, and spacing;
17
+ - Never translate non-English text.
18
+ - Do not add unnecessary or additional information. Do not add any links or images. Do not add Chinese symbols.
19
+ **Important**: the output format must be Markdown (use bold text, headlines, so on)."""
20
+
21
+ class EndpointHandler:
22
+ def __init__(self, path: str = "Qwen/Qwen3-VL-2B-Instruct"):
23
+ # Load tokenizer and model
24
+ self.processor = AutoProcessor.from_pretrained(path)
25
+ self.model = Qwen3VLForConditionalGeneration.from_pretrained(path, device_map="auto")
26
+ self.model.eval()
27
+
28
+ def __call__(self, data: Dict[str, Any]) -> str:
29
+ # Prepare your messages with image and text
30
+ inputs = data.get("inputs")
31
+ base64image = inputs["base64"]
32
+
33
+ img_bytes = base64.b64decode(base64image)
34
+ pil_img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
35
+
36
+ messages = [
37
+ {
38
+ "role": "user",
39
+ "content": [
40
+ {"type": "image", "image": pil_img}, # pass PIL image directly
41
+ {"type": "text", "text": prompt},
42
+ ]
43
+ }
44
+ ]
45
+
46
+ # Process the input and generate a response
47
+ inputs = self.processor.apply_chat_template(
48
+ messages,
49
+ tokenize=True,
50
+ add_generation_prompt=True,
51
+ return_dict=True,
52
+ return_tensors="pt"
53
+ )
54
+ inputs = inputs.to(self.model.device)
55
+
56
+ generated_ids = self.model.generate(**inputs, max_new_tokens=2048)
57
+ generated_ids_trimmed = [
58
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
59
+ ]
60
+ output_text = self.processor.batch_decode(
61
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
62
+ )
63
+
64
+ response = {
65
+ "id": f"chatcmpl-{uuid.uuid4().hex}",
66
+ "object": "chat.completion",
67
+ "created": int(time.time()),
68
+ "model": "Qwen/Qwen3-VL-8B-Instruct",
69
+ "usage": {
70
+ # you might compute these if you can get token counts
71
+ "prompt_tokens": None,
72
+ "completion_tokens": None,
73
+ "total_tokens": None
74
+ },
75
+ "choices": [
76
+ {
77
+ "message": {
78
+ "role": "assistant",
79
+ "content": output_text[0]
80
+ },
81
+ "finish_reason": "stop",
82
+ "index": 0
83
+ }
84
+ ]
85
+ }
86
+
87
+ return response