ryanhuangtw
commited on
Upload 2 files
Browse files- handler.py +28 -0
- requirements.txt +0 -0
handler.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForObjectDetection, AutoProcessor
|
3 |
+
|
4 |
+
class CustomHandler:
|
5 |
+
def __init__(self):
|
6 |
+
self.model = None
|
7 |
+
self.processor = None
|
8 |
+
|
9 |
+
def initialize(self, model_dir):
|
10 |
+
# 載入模型和處理器
|
11 |
+
self.model = AutoModelForObjectDetection.from_pretrained(model_dir)
|
12 |
+
self.processor = AutoProcessor.from_pretrained(model_dir)
|
13 |
+
|
14 |
+
def preprocess(self, request):
|
15 |
+
# 解析輸入資料
|
16 |
+
inputs = request.get("inputs")
|
17 |
+
return self.processor(images=inputs, return_tensors="pt")
|
18 |
+
|
19 |
+
def inference(self, inputs):
|
20 |
+
# 執行推理
|
21 |
+
with torch.no_grad():
|
22 |
+
outputs = self.model(**inputs)
|
23 |
+
return outputs
|
24 |
+
|
25 |
+
def postprocess(self, outputs):
|
26 |
+
# 從模型輸出轉換成人類可讀格式
|
27 |
+
results = outputs.logits.softmax(-1).tolist()
|
28 |
+
return {"predictions": results}
|
requirements.txt
ADDED
File without changes
|