almayor commited on
Commit
da52aa3
·
1 Parent(s): e9eee90
Files changed (2) hide show
  1. app.py +105 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import (
3
+ AutoTokenizer,
4
+ AutoModelForSequenceClassification,
5
+ )
6
+
7
+ ### Loading models and tokenizers as global variables
8
+
9
+ tokenizer_title = AutoTokenizer.from_pretrained("./model_title")
10
+ model_title = AutoModelForSequenceClassification.from_pretrained("./model_title")
11
+
12
+ tokenizer_summary = AutoTokenizer.from_pretrained("./model_summary")
13
+ model_summary = AutoModelForSequenceClassification.from_pretrained("./model_summary")
14
+
15
+ categories = [
16
+ 'biology',
17
+ 'computer_science',
18
+ 'economics',
19
+ 'electrical_engineering',
20
+ 'finance',
21
+ 'mathematics',
22
+ 'physics',
23
+ 'statistics',
24
+ 'unknown'
25
+ ]
26
+
27
+ ### Auxiliary functions
28
+
29
+ def classify_title(input_text):
30
+ input_tokens = tokenizer_title.encode(
31
+ input_text,
32
+ truncation=True,
33
+ padding=True,
34
+ return_tensors='pt'
35
+ )
36
+
37
+ preds = model_title(input_tokens)
38
+ [probs] = preds.logits.softmax(dim=-1).tolist()
39
+ return {cat: p for (cat, p) in zip(categories, probs)}
40
+
41
+
42
+ def classify_summary(input_text):
43
+ input_tokens = tokenizer_summary.encode(
44
+ input_text,
45
+ truncation=True,
46
+ padding=True,
47
+ return_tensors='pt'
48
+ )
49
+
50
+ preds = model_summary(input_tokens)
51
+ [probs] = preds.logits.softmax(dim=-1).tolist()
52
+ return {cat: prob for (cat, prob) in zip(categories, probs)}
53
+
54
+
55
+ def classify_title_summary(title, summary, weight_title=3, weight_summary=1):
56
+ clsf_title = classify_title(title)
57
+ clsf_summary = classify_summary(summary)
58
+
59
+ clsf_both = dict()
60
+ for cat in categories:
61
+ clsf_p = clsf_title[cat] * weight_title + clsf_summary[cat] * weight_summary
62
+ clsf_both[cat] = clsf_p / (weight_title + weight_summary)
63
+
64
+ return clsf_both
65
+
66
+
67
+ def get_top_classifications(clsf, p_top=0.95):
68
+ clsf_top = dict()
69
+ p_total = 0
70
+
71
+ clsf_sorted = sorted(clsf.items(), key=lambda x: x[1], reverse=True)
72
+ for cat, p in clsf_sorted:
73
+ if p_total >= p_top:
74
+ break
75
+ clsf_top[cat] = p
76
+ p_total += p
77
+
78
+ return clsf_top
79
+
80
+
81
+ ### Gradio interface
82
+
83
+ def process_input(title, summary):
84
+ if not title:
85
+ return
86
+
87
+ clsf = classify_title(title) if not summary else classify_title_summary(title, summary)
88
+ clsf = get_top_classifications(clsf, p_top=0.95)
89
+ return clsf
90
+
91
+
92
+ title_box = gr.inputs.Textbox(lines=2, placeholder="Your title...", optional=False, label='Article Title')
93
+ summary_box = gr.inputs.Textbox(lines=12, placeholder="Your summary...", optional=True, label='Article Summary')
94
+ label_widget = gr.outputs.Label(label='Predicted Topics')
95
+
96
+ iface = gr.Interface(
97
+ fn=process_input,
98
+ inputs=[title_box, summary_box],
99
+ outputs=label_widget,
100
+ title="Article Classifier",
101
+ description="A transformer-based classifier that infers the topic of scientific articles based on a title and summary.",
102
+ allow_flagging="never"
103
+ )
104
+
105
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio
4
+ jinja2
5
+ numpy
6
+
7
+