generated from guardrails-ai/validator-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
104 lines (79 loc) · 2.77 KB
/
app.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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Union
from transformers import pipeline
import torch
import os
app = FastAPI()
env = os.environ.get("env", "dev")
torch_device = "cuda" if env == "prod" else "cpu"
model = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli",
device=torch.device(torch_device),
hypothesis_template="This example has to do with topic {}.",
multi_label=True,
)
class InferenceData(BaseModel):
name: str
shape: List[int]
data: Union[List[str], List[float]]
datatype: str
class InputRequest(BaseModel):
inputs: List[InferenceData]
class OutputResponse(BaseModel):
modelname: str
modelversion: str
outputs: List[InferenceData]
@app.get("/")
async def hello_world():
return "restrict_to_topic"
@app.post("/validate", response_model=OutputResponse)
async def restrict_to_topic(input_request: InputRequest):
text_vals = None
candidate_topics = None
zero_shot_threshold = 0.5
for inp in input_request.inputs:
if inp.name == "text":
text_vals = inp.data
elif inp.name == "candidate_topics":
candidate_topics = inp.data
elif inp.name == "zero_shot_threshold":
zero_shot_threshold = float(inp.data[0])
if text_vals is None or candidate_topics is None:
raise HTTPException(status_code=400, detail="Invalid input format")
return RestrictToTopic.infer(text_vals, candidate_topics, zero_shot_threshold)
class RestrictToTopic:
model_name = "facebook/bart-large-mnli"
device = torch.device(torch_device)
model = pipeline(
"zero-shot-classification",
model=model_name,
device=device,
hypothesis_template="This example has to do with topic {}.",
multi_label=True,
)
@staticmethod
def infer(text_vals, candidate_topics, threshold) -> OutputResponse:
outputs = []
for idx, text in enumerate(text_vals):
results = RestrictToTopic.model(text, candidate_topics)
pred_labels = [
label for label, score in zip(results["labels"], results["scores"]) if score > threshold
]
if not pred_labels:
pred_labels = ["No valid topic found."]
outputs.append(
InferenceData(
name=f"result{idx}",
datatype="BYTES",
shape=[len(pred_labels)],
data=pred_labels,
)
)
output_data = OutputResponse(
modelname="RestrictToTopicModel", modelversion="1", outputs=outputs
)
return output_data
# Run the app with uvicorn
# Save this script as app.py and run with: uvicorn app:app --reload