Lord-Raven
Playing with models.
f4c9eb8
raw
history blame
1.86 kB
import gradio
import json
import torch
from transformers import AutoTokenizer
from transformers import pipeline
from optimum.onnxruntime import ORTModelForSequenceClassification
# "xenova/mobilebert-uncased-mnli" "typeform/mobilebert-uncased-mnli" Fast but small
# "xenova/deberta-v3-base-tasksource-nli" Not impressed
# "Xenova/bart-large-mnli" A bit slow
# "Xenova/distilbert-base-uncased-mnli" "typeform/distilbert-base-uncased-mnli" Bad answers
# "Xenova/deBERTa-v3-base-mnli" "MoritzLaurer/DeBERTa-v3-base-mnli" Still a bit slow and not great answers
model_name = "MoritzLaurer/deberta-v3-large-zeroshot-v2.0"
file_name = "onnx/model_quantized.onnx"
tokenizer_name = model_name
model = ORTModelForSequenceClassification.from_pretrained(model_name) #, file_name=file_name)
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name, model_max_length=512)
# file = cached_download("https://huggingface.co/" + model_name + "")
# sess = InferenceSession(file)
classifier = pipeline(task="zero-shot-classification", model=model, tokenizer=tokenizer)
def zero_shot_classification(data_string, request: gradio.Request):
if request:
print("Request headers dictionary:", request.headers)
if request.headers["origin"] not in ["https://statosphere-3704059fdd7e.c5v4v4jx6pq5.win", "https://jhuhman-statosphere-backend.hf.space"]:
return "{}"
print(data_string)
data = json.loads(data_string)
print(data)
results = classifier(data['sequence'], candidate_labels=data['candidate_labels'], hypothesis_template=data['hypothesis_template'], multi_label=data['multi_label'])
response_string = json.dumps(results)
return response_string
gradio_interface = gradio.Interface(
fn = zero_shot_classification,
inputs = gradio.Textbox(label="JSON Input"),
outputs = gradio.Textbox()
)
gradio_interface.launch()