-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathservice.py
111 lines (91 loc) · 2.86 KB
/
service.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import typing as t
from pathlib import Path
import numpy as np
import pandas as pd
import torch
from PIL import Image as im
from PIL.Image import Image
from pydantic import Field
import bentoml
from bentoml.validators import DataframeSchema
from bentoml.validators import DType
@bentoml.service()
class ImageResize:
@bentoml.api()
def generate(self, image: Image, height: int = 64, width: int = 64) -> Image:
size = height, width
return image.resize(size, im.LANCZOS)
@bentoml.api()
def generate_with_path(
self,
image: t.Annotated[Path, bentoml.validators.ContentType("image/jpeg")],
height: int = 64,
width: int = 64,
) -> Image:
size = height, width
image = im.open(image)
return image.resize(size, im.LANCZOS)
@bentoml.service()
class AdditionService:
@bentoml.api()
def add(self, num1: float, num2: float) -> float:
return num1 + num2
@bentoml.service()
class AppendStringToFile:
@bentoml.api()
def append_string_to_eof(
self,
context: bentoml.Context,
txt_file: t.Annotated[Path, bentoml.validators.ContentType("text/plain")],
input_string: str,
) -> t.Annotated[Path, bentoml.validators.ContentType("text/plain")]:
with open(txt_file, "a") as file:
file.write(input_string)
return txt_file
@bentoml.service()
class PDFtoImage:
@bentoml.api()
def pdf_first_page_as_image(
self,
pdf: t.Annotated[Path, bentoml.validators.ContentType("application/pdf")],
) -> Image:
from pdf2image import convert_from_path
pages = convert_from_path(pdf)
return pages[0].resize(pages[0].size, im.ANTIALIAS)
@bentoml.service()
class AudioSpeedUp:
@bentoml.api()
def speed_up_audio(
self,
context: bentoml.Context,
audio: t.Annotated[Path, bentoml.validators.ContentType("audio/mpeg")],
velocity: float,
) -> t.Annotated[Path, bentoml.validators.ContentType("audio/mp3")]:
import os
from pydub import AudioSegment
output_path = os.path.join(context.temp_dir, "output.mp3")
sound = AudioSegment.from_file(audio)
sound = sound.speedup(velocity)
sound.export(output_path, format="mp3")
return Path(output_path)
@bentoml.service()
class TransposeTensor:
@bentoml.api()
def transpose(
self,
tensor: t.Annotated[torch.Tensor, DType("float32")] = Field(
description="A 2x4 tensor with float32 dtype"
),
) -> np.ndarray:
return torch.transpose(tensor, 0, 1).numpy()
@bentoml.service()
class CountRowsDF:
@bentoml.api()
def count_rows(
self,
input: t.Annotated[
pd.DataFrame,
DataframeSchema(orient="records", columns=["dummy1", "dummy2"]),
],
) -> int:
return len(input)