Upload handler_template.py with huggingface_hub
Browse files- handler_template.py +65 -0
handler_template.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import base64
|
| 6 |
+
import torch
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
class BaseHandler:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
"""Initialize the handler with model-specific configurations"""
|
| 14 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
self.model = None
|
| 16 |
+
self.initialized = False
|
| 17 |
+
|
| 18 |
+
def initialize(self):
|
| 19 |
+
"""Load model and other resources"""
|
| 20 |
+
# This method should be implemented by each specific handler
|
| 21 |
+
raise NotImplementedError
|
| 22 |
+
|
| 23 |
+
def preprocess(self, data):
|
| 24 |
+
"""Preprocess the input data"""
|
| 25 |
+
# This method should be implemented by each specific handler
|
| 26 |
+
raise NotImplementedError
|
| 27 |
+
|
| 28 |
+
def inference(self, inputs):
|
| 29 |
+
"""Run inference with the preprocessed inputs"""
|
| 30 |
+
# This method should be implemented by each specific handler
|
| 31 |
+
raise NotImplementedError
|
| 32 |
+
|
| 33 |
+
def postprocess(self, inference_output):
|
| 34 |
+
"""Post-process the model output"""
|
| 35 |
+
# This method should be implemented by each specific handler
|
| 36 |
+
raise NotImplementedError
|
| 37 |
+
|
| 38 |
+
def __call__(self, data):
|
| 39 |
+
"""Handle a request to the model"""
|
| 40 |
+
# Initialize the model if not already done
|
| 41 |
+
if not self.initialized:
|
| 42 |
+
self.initialize()
|
| 43 |
+
|
| 44 |
+
# Process the request
|
| 45 |
+
preprocessed_data = self.preprocess(data)
|
| 46 |
+
inference_output = self.inference(preprocessed_data)
|
| 47 |
+
output = self.postprocess(inference_output)
|
| 48 |
+
|
| 49 |
+
return output
|
| 50 |
+
|
| 51 |
+
def encode_image(self, image):
|
| 52 |
+
"""Encode a PIL Image to base64"""
|
| 53 |
+
buffered = io.BytesIO()
|
| 54 |
+
image.save(buffered, format="PNG")
|
| 55 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 56 |
+
return img_str
|
| 57 |
+
|
| 58 |
+
def decode_image(self, image_str):
|
| 59 |
+
"""Decode a base64 string to PIL Image"""
|
| 60 |
+
img_data = base64.b64decode(image_str)
|
| 61 |
+
return Image.open(io.BytesIO(img_data))
|
| 62 |
+
|
| 63 |
+
def svg_to_base64(self, svg_content):
|
| 64 |
+
"""Convert SVG content to base64"""
|
| 65 |
+
return base64.b64encode(svg_content.encode("utf-8")).decode("utf-8")
|