kirillbogatiy commited on
Commit
c641155
·
1 Parent(s): ee2827e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
3
+ from transformers import pipeline
4
+
5
+
6
+ with open('labels.txt') as f:
7
+ LABEL2STR = f.readline().split()
8
+
9
+
10
+ @st.cache(allow_output_mutation=True)
11
+ def load_model():
12
+ tokenizer = AutoTokenizer.from_pretrained("kirillbogatiy/model_topics")
13
+ model = AutoModelForSequenceClassification.from_pretrained("kirillbogatiy/model_topics")
14
+ pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
15
+ return pipe
16
+
17
+
18
+ def pretty_output(predictions, thr=0.95):
19
+ cumulative_score = 0
20
+ st.write('Possible topics:')
21
+ for label, data in enumerate(sorted(predictions[0], key=lambda item: item['score'], reverse=True)):
22
+ score = data['score']
23
+ cumulative_score += score
24
+ st.write('{}: {} %'.format(LABEL2STR[label], round(100 * score, 2)))
25
+ if cumulative_score >= thr:
26
+ return
27
+
28
+
29
+
30
+ if __name__ == '__main__':
31
+ title = st.text_input('Input a title here:')
32
+ abstract = st.text_input('Input an abstract here:')
33
+ pipe = load_model()
34
+ if title:
35
+ predictions = pipe('Title: {}\n\nAbstract: {}'.format(title, abstract))
36
+ pretty_output(predictions)