Spaces:
Sleeping
Sleeping
entertainment category app ready
Browse files- app.py +33 -4
- category.json +1 -0
- example.json +5 -0
app.py
CHANGED
@@ -1,9 +1,38 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
-
|
5 |
-
|
|
|
6 |
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import json
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer
|
6 |
+
import onnxruntime as rt
|
7 |
|
8 |
|
9 |
+
model_path = "entertainment-category-quantized.onnx"
|
10 |
+
with open("category.json", "r") as file:
|
11 |
+
categories = json.load(file)["categories"]
|
12 |
|
13 |
|
14 |
+
inf_session = rt.InferenceSession(model_path)
|
15 |
+
input_name = inf_session.get_inputs()[0].name
|
16 |
+
output_name = inf_session.get_outputs()[0].name
|
17 |
+
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained("distilroberta-base")
|
19 |
+
|
20 |
+
|
21 |
+
def entertainment_category(description):
|
22 |
+
input_ids = tokenizer(description)["input_ids"][:512]
|
23 |
+
probs = inf_session.run([output_name], {input_name: [input_ids]})[0]
|
24 |
+
|
25 |
+
mask = np.where(probs[0] == probs.max())[0][0]
|
26 |
+
cat = categories[mask]
|
27 |
+
cat_prob = torch.sigmoid(torch.FloatTensor(probs))[0]
|
28 |
+
|
29 |
+
return dict(zip(categories, map(float, cat_prob)))
|
30 |
+
|
31 |
+
|
32 |
+
with open("example.json", "r") as file:
|
33 |
+
examples = json.load(file)["examples"]
|
34 |
+
|
35 |
+
|
36 |
+
label = gr.components.Label(num_top_classes=5)
|
37 |
+
iface = gr.Interface(fn=entertainment_category, inputs="text", outputs=label, examples=examples)
|
38 |
+
iface.launch(inline=False)
|
category.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"categories": ["anime", "book", "game", "movie", "music", "tv show"]}
|
example.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{"examples": [
|
2 |
+
["March Of Soldiers is a real time strategy single player , It is a military game based on the player's skill and the strength of his financial economy"],
|
3 |
+
["Don Vito Corleone, head of a mafia family, decides to hand over his empire to his youngest son Michael. However, his decision unintentionally puts the lives of his loved ones in grave danger."],
|
4 |
+
["When I wake up, I look into the mirror, I can see a clearer, vision, I should start living today, Cause today is gonna be the day, is gonna be the day, Cause today is gonna be the day, is gonna be the day"]
|
5 |
+
]}
|