hynky HF Staff commited on
Commit
9746697
·
1 Parent(s): e7301c5
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from html import unescape
2
+ from unicodedata import normalize
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+ import re
6
+
7
+ re_multispace = re.compile(r"\s+")
8
+
9
+ def normalize_text(text):
10
+ if text == None:
11
+ return None
12
+
13
+ text = text.strip()
14
+ text = text.replace("\n", " ")
15
+ text = text.replace("\t", " ")
16
+ text = text.replace("\r", " ")
17
+ text = re_multispace.sub(" ", text)
18
+ text = unescape(text)
19
+ text = normalize("NFKC", text)
20
+ return text
21
+
22
+
23
+ models = [
24
+ "Server", "Category", "Gender", "Day Of Week"
25
+ ]
26
+
27
+ pipelines = {model: pipeline(task="text-classification",
28
+ model=f"hynky/{model.replace(' ', '_')}", tokenizer="ufal/robeczech-base",
29
+ return_all_scores=True
30
+ ) for model in models}
31
+
32
+
33
+ def predict(article):
34
+ article = normalize_text(article)
35
+ predictions = {model: pipelines[model](article) for model in models}
36
+ return predictions
37
+
38
+ gr.Interface(
39
+ predict,
40
+ inputs=gr.inputs.Textbox(lines=2, placeholder="Paste a news article here..."),
41
+ # multioutput of gradio text
42
+ outputs=[gr.outputs.Label(num_top_classses=5)
43
+
44
+ for model in models],
45
+ title="News Article Classifier",
46
+ ).launch()