t45_crexdata_demo / classifier.py
jayebaku's picture
Update classifier.py
65afd8a verified
raw
history blame
1.11 kB
import spaces
from transformers import pipeline as tpipeline
from optimum.pipelines import pipeline as opipline
#@spaces.GPU(duration=60)
def classify(tweet, event_model, hftoken, threshold):
results = {"text": None, "event": None, "score": None}
# event type prediction with transformers pipeline
# event_predictor = pipeline(task="text-classification", model=event_model,
# batch_size=512, token=hftoken, device="cpu")
# tokenizer_kwargs = {'padding': True, 'truncation': True, 'max_length': 512}
# prediction = event_predictor(tweet, **tokenizer_kwargs)[0]
# with onnx pipeline
onnx_classifier = pipeline("text-classification", model=event_model, accelerator="ort")
prediction = onnx_classifier(text)[0]
results["text"] = tweet
if prediction["label"] != "none" and round(prediction["score"], 2) <= threshold:
results["event"] = "none"
results["score"] = prediction["score"]
else:
results["event"] = prediction["label"]
results["score"] = prediction["score"]
return results