yashkens commited on
Commit
1a62c90
1 Parent(s): 9f6e2f8

add emotion classification

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +1 -1
  3. emotions.py +7 -12
README.md CHANGED
@@ -7,7 +7,7 @@ sdk: streamlit
7
  sdk_version: 1.10.0
8
  app_file: app.py
9
  pinned: false
10
- models: mrm8488/t5-base-finetuned-emotion
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  sdk_version: 1.10.0
8
  app_file: app.py
9
  pinned: false
10
+ models: bhadresh-savani/distilbert-base-uncased-emotion
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -8,6 +8,6 @@ name = st.text_input('Who are you?', 'Zeliboba')
8
  text = st.text_area('Submit your stories', '''Random symbols''')
9
 
10
  st.write('Here is your first text:', text)
11
- st.write(get_emotion(text))
12
 
13
 
 
8
  text = st.text_area('Submit your stories', '''Random symbols''')
9
 
10
  st.write('Here is your first text:', text)
11
+ st.write(get_emotion(text)['label'])
12
 
13
 
emotions.py CHANGED
@@ -1,16 +1,11 @@
1
- from transformers import AutoTokenizer, AutoModelWithLMHead
2
 
3
- tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion")
4
-
5
- model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-emotion")
6
 
7
 
8
  def get_emotion(text='No text yet'):
9
- input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
10
-
11
- output = model.generate(input_ids=input_ids,
12
- max_length=2)
13
-
14
- dec = [tokenizer.decode(ids) for ids in output]
15
- label = dec[0]
16
- return label
 
1
+ from transformers import pipeline
2
 
3
+ classifier = pipeline("text-classification",
4
+ model='bhadresh-savani/distilbert-base-uncased-emotion',
5
+ return_all_scores=True)
6
 
7
 
8
  def get_emotion(text='No text yet'):
9
+ prediction = classifier(text)[0]
10
+ result = max(prediction, key=lambda x: x['score'])
11
+ return result