ryanhuangtw commited on
Commit
cc048b4
·
verified ·
1 Parent(s): 216c490

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +14 -22
handler.py CHANGED
@@ -1,28 +1,20 @@
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}
 
1
+ from ultralytics import YOLO
2
+ from huggingface_inference_toolkit.handler import BaseHandler
3
 
4
+ class CustomYOLOHandler(BaseHandler):
5
+ def __init__(self, model_dir, *args, **kwargs):
6
+ super().__init__(*args, **kwargs)
7
+ self.model = YOLO(f"{model_dir}/model.pt")
8
 
9
+ def preprocess(self, inputs):
10
+ # Preprocess inputs for YOLO
11
+ return inputs
 
 
 
 
 
 
12
 
13
  def inference(self, inputs):
14
+ # Perform inference
15
+ results = self.model(inputs)
16
+ return results
 
17
 
18
  def postprocess(self, outputs):
19
+ # Convert YOLO results to HuggingFace pipeline outputs
20
+ return outputs