File size: 1,064 Bytes
cd02163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Hugging Face Inference Endpoints handler (custom container task).

Deploy: on the model page choose Deploy -> Inference Endpoints, pick a GPU (or CPU) instance; the
endpoint installs requirements.txt and serves this class. Send a Jev/SystemOne request as the JSON
body, either directly or under "inputs"; a list of requests returns a list of responses.
"""
import os
import sys
from typing import Any

import torch

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from decider_model import load_release_model, systemone  # noqa: E402


class EndpointHandler:
    def __init__(self, path: str = ""):
        path = path or os.path.dirname(os.path.abspath(__file__))
        self.model = load_release_model(path, device="cuda" if torch.cuda.is_available() else "cpu")

    def __call__(self, data: dict[str, Any]) -> Any:
        request = data.get("inputs", data) if isinstance(data, dict) else data
        if isinstance(request, list):
            return [systemone(self.model, r) for r in request]
        return systemone(self.model, request)