Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import gradio as gr
|
2 |
import onnxruntime as rt
|
3 |
from transformers import AutoTokenizer
|
4 |
import torch, json
|
@@ -23,4 +23,47 @@ def classify_movie_genre(sinopse):
|
|
23 |
|
24 |
label = gr.outputs.Label(num_top_classes=5)
|
25 |
iface = gr.Interface(fn=classify_movie_genre, inputs="text", outputs=label)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
iface.launch(inline=False)
|
|
|
1 |
+
"""import gradio as gr
|
2 |
import onnxruntime as rt
|
3 |
from transformers import AutoTokenizer
|
4 |
import torch, json
|
|
|
23 |
|
24 |
label = gr.outputs.Label(num_top_classes=5)
|
25 |
iface = gr.Interface(fn=classify_movie_genre, inputs="text", outputs=label)
|
26 |
+
iface.launch(inline=False)"""
|
27 |
+
|
28 |
+
import gradio as gr
|
29 |
+
import onnxruntime as rt
|
30 |
+
from transformers import AutoTokenizer
|
31 |
+
import torch, json
|
32 |
+
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained("neuralmind/bert-large-portuguese-cased")
|
34 |
+
|
35 |
+
with open("genre_types_encoded.json", "r") as fp:
|
36 |
+
encode_genre_types = json.load(fp)
|
37 |
+
|
38 |
+
genres = list(encode_genre_types.keys())
|
39 |
+
|
40 |
+
inf_session = rt.InferenceSession('movie-classifier-quantized.onnx')
|
41 |
+
input_name = inf_session.get_inputs()[0].name
|
42 |
+
output_name = inf_session.get_outputs()[0].name
|
43 |
+
|
44 |
+
def classify_movie_genre(sinopse):
|
45 |
+
input_ids = tokenizer(sinopse)['input_ids'][:512]
|
46 |
+
logits = inf_session.run([output_name], {input_name: [input_ids]})[0]
|
47 |
+
logits = torch.FloatTensor(logits)
|
48 |
+
probs = torch.sigmoid(logits)[0]
|
49 |
+
return dict(zip(genres, map(float, probs)))
|
50 |
+
|
51 |
+
|
52 |
+
app_examples = [
|
53 |
+
["asasasa"],
|
54 |
+
["ddddd"],
|
55 |
+
["fffff"],
|
56 |
+
["ggggg"],
|
57 |
+
["aaaaaa"]
|
58 |
+
|
59 |
+
]
|
60 |
+
|
61 |
+
inputs = [
|
62 |
+
gr.Textbox(label="text", value=app_examples[0][0]),
|
63 |
+
|
64 |
+
]
|
65 |
+
|
66 |
+
|
67 |
+
label = gr.outputs.Label(num_top_classes=4)
|
68 |
+
iface = gr.Interface(fn=classify_movie_genre, inputs=inputs, outputs=label, examples=app_examples)
|
69 |
iface.launch(inline=False)
|