|
"""import gradio as gr |
|
import onnxruntime as rt |
|
from transformers import AutoTokenizer |
|
import torch, json |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("neuralmind/bert-large-portuguese-cased") |
|
|
|
with open("genre_types_encoded.json", "r") as fp: |
|
encode_genre_types = json.load(fp) |
|
|
|
genres = list(encode_genre_types.keys()) |
|
|
|
inf_session = rt.InferenceSession('movie-classifier-quantized.onnx') |
|
input_name = inf_session.get_inputs()[0].name |
|
output_name = inf_session.get_outputs()[0].name |
|
|
|
def classify_movie_genre(sinopse): |
|
input_ids = tokenizer(sinopse)['input_ids'][:512] |
|
logits = inf_session.run([output_name], {input_name: [input_ids]})[0] |
|
logits = torch.FloatTensor(logits) |
|
probs = torch.sigmoid(logits)[0] |
|
return dict(zip(genres, map(float, probs))) |
|
|
|
label = gr.outputs.Label(num_top_classes=5) |
|
iface = gr.Interface(fn=classify_movie_genre, inputs="text", outputs=label) |
|
iface.launch(inline=False)""" |
|
|
|
import gradio as gr |
|
import onnxruntime as rt |
|
from transformers import AutoTokenizer |
|
import torch, json |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("neuralmind/bert-large-portuguese-cased") |
|
|
|
with open("genre_types_encoded.json", "r") as fp: |
|
encode_genre_types = json.load(fp) |
|
|
|
genres = list(encode_genre_types.keys()) |
|
|
|
inf_session = rt.InferenceSession('movie-classifier-quantized.onnx') |
|
input_name = inf_session.get_inputs()[0].name |
|
output_name = inf_session.get_outputs()[0].name |
|
|
|
def classify_movie_genre(sinopse): |
|
input_ids = tokenizer(sinopse)['input_ids'][:512] |
|
logits = inf_session.run([output_name], {input_name: [input_ids]})[0] |
|
logits = torch.FloatTensor(logits) |
|
probs = torch.sigmoid(logits)[0] |
|
return dict(zip(genres, map(float, probs))) |
|
|
|
|
|
app_examples = [ |
|
["asasasa"], |
|
["ddddd"], |
|
["fffff"], |
|
["ggggg"], |
|
["aaaaaa"] |
|
|
|
] |
|
|
|
inputs = [ |
|
gr.Textbox(label="text", value=app_examples[0][0]), |
|
|
|
] |
|
|
|
|
|
label = gr.outputs.Label(num_top_classes=4) |
|
iface = gr.Interface(fn=classify_movie_genre, inputs=inputs, outputs=label, examples=app_examples) |
|
iface.launch(inline=False) |