generated from guardrails-ai/validator-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp_inference_spec.py
75 lines (61 loc) · 2.19 KB
/
app_inference_spec.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
from fastapi import HTTPException
from pydantic import BaseModel
from typing import List, Tuple
import spacy
from models_host.base_inference_spec import BaseInferenceSpec
class InferenceData(BaseModel):
name: str
shape: List[int]
data: List
datatype: str
class InputRequest(BaseModel):
inputs: List[InferenceData]
class OutputResponse(BaseModel):
modelname: str
modelversion: str
outputs: List[InferenceData]
class InferenceSpec(BaseInferenceSpec):
model_name = "en_core_web_trf"
model = None
def load(self):
model_name = self.model_name
print(f"Loading model {model_name}...")
if not spacy.util.is_package(model_name):
print(
f"Spacy model {model_name} not installed. "
"Download should start now and take a few minutes."
)
spacy.cli.download(model_name) # type: ignore
self.model = spacy.load(model_name)
def process_request(self, input_request: InputRequest) -> Tuple[Tuple, dict]:
competitors = []
for inp in input_request.inputs:
if inp.name == "text":
text_vals = inp.data
elif inp.name == "competitors":
competitors = inp.data
if text_vals is None or competitors is None:
raise HTTPException(status_code=400, detail="Invalid input format")
args = (text_vals, competitors)
kwargs = {}
return args, kwargs
def infer(self, text_vals, competitors) -> OutputResponse:
outputs = []
for idx, text in enumerate(text_vals):
doc = self.model(text) # type: ignore
located_competitors = []
for ent in doc.ents:
if ent.text in competitors:
located_competitors.append(ent.text)
outputs.append(
InferenceData(
name=f"result{idx}",
datatype="BYTES",
shape=[1],
data=[located_competitors],
)
)
output_data = OutputResponse(
modelname=self.model_name, modelversion="1", outputs=outputs
)
return output_data