akshay7 commited on
Commit
d1e307b
1 Parent(s): 59e533c

Add application file

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import numpy as np
4
+ import torch
5
+
6
+ def main():
7
+
8
+ st.set_page_config( # Alternate names: setup_page, page, layout
9
+ layout="centered", # Can be "centered" or "wide". In the future also "dashboard", etc.
10
+ initial_sidebar_state="auto", # Can be "auto", "expanded", "collapsed"
11
+ page_title="Emoji-motion!", # String or None. Strings get appended with "• Streamlit".
12
+ page_icon=None, # String, anything supported by st.image, or None.
13
+ )
14
+
15
+ st.title('Emoji-motion!')
16
+
17
+ example_prompts = [
18
+ "Today is going to be awesome!",
19
+ "Pity those who don't feel anything at all.",
20
+ "I envy people that know love.",
21
+ "Nature is so beautiful"]
22
+
23
+ example = st.selectbox("Choose a pre-defined example", example_prompts)
24
+
25
+ # Take the message which needs to be processed
26
+ message = st.text_area('Or type a sentence to see if our AL Algorithm can detect your emotion', example)
27
+ # st.title(message)
28
+ st.text('')
29
+ models_to_choose = ["AlekseyDorkin/xlm-roberta-en-ru-emoji"]
30
+
31
+ BASE_MODEL = st.selectbox("Choose a model", models_to_choose)
32
+ TOP_N = 5
33
+
34
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
35
+ model = AutoModelForSequenceClassification.from_pretrained(BASE_MODEL)
36
+
37
+ def preprocess(text):
38
+ new_text = []
39
+ for t in text.split(" "):
40
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
41
+ t = 'http' if t.startswith('http') else t
42
+ new_text.append(t)
43
+ return " ".join(new_text)
44
+
45
+ def get_top_emojis(text, top_n=TOP_N):
46
+ preprocessed = preprocess(text)
47
+ inputs = tokenizer(preprocessed, return_tensors="pt")
48
+ preds = model(**inputs).logits
49
+ scores = torch.nn.functional.softmax(preds, dim=-1).detach().numpy()
50
+ ranking = np.argsort(scores)
51
+ print(ranking)
52
+ ranking = ranking.squeeze()[::-1][:top_n]
53
+ print(scores)
54
+ print(ranking)
55
+ print(model.config.id2label)
56
+ emojis = [model.config.id2label[i] for i in ranking]
57
+ return ', '.join(map(str, emojis))
58
+
59
+ # Define function to run when submit is clicked
60
+ def submit(message):
61
+ if len(message)>0:
62
+ st.header(get_top_emojis(message))
63
+ else:
64
+ st.error("The text can't be empty")
65
+
66
+ # Run algo when submit button is clicked
67
+ if(st.button('Submit')):
68
+ submit(message)
69
+
70
+ st.text('')
71
+ st.markdown('<span style="color:blue; font-size:10px">App created by [@AlekseyDorkin](https://huggingface.co/AlekseyDorkin) \
72
+ and [@akshay7](https://huggingface.co/akshay7)</span>',unsafe_allow_html=True)
73
+
74
+
75
+ if __name__ == "__main__":
76
+ main()