Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import onnxruntime
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
import torch, json
|
| 5 |
+
|
| 6 |
+
token = AutoTokenizer.from_pretrained('pablocosta/bertabaporu-large-uncased')
|
| 7 |
+
|
| 8 |
+
with open("genre_types_encoded.json", "r") as fp:
|
| 9 |
+
types = json.load(fp)
|
| 10 |
+
|
| 11 |
+
types = list(types)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
inf_session = onnxruntime.InferenceSession('https://drive.google.com/file/d/1-8Iru2d_NjNmZ_czxu87IjIX1meBea4E/view?usp=sharing')
|
| 15 |
+
input_name = inf_session.get_inputs()[0].name
|
| 16 |
+
output_name = inf_session.get_outputs()[0].name
|
| 17 |
+
|
| 18 |
+
def classify(review):
|
| 19 |
+
input_ids = token(review)['input_ids'][:512]
|
| 20 |
+
logits = inf_session.run([output_name], {input_name: [input_ids]})[0]
|
| 21 |
+
logits = torch.FloatTensor(logits)
|
| 22 |
+
probs = torch.sigmoid(logits)[0]
|
| 23 |
+
return dict(zip(types, map(float, probs)))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
label = gr.outputs.Label(num_top_classes=5)
|
| 27 |
+
iface = gr.Interface(fn=classify, inputs="text", outputs=label)
|
| 28 |
+
iface.launch(inline=False)
|