MYTE commited on
Commit
60e2020
1 Parent(s): 5969592

entertainment category app ready

Browse files
Files changed (3) hide show
  1. app.py +33 -4
  2. category.json +1 -0
  3. example.json +5 -0
app.py CHANGED
@@ -1,9 +1,38 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
 
4
- def greet(name):
5
- return "Hello " + name + "!!"
 
6
 
7
 
8
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
9
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ]}