clezcano commited on
Commit
b6a6e61
1 Parent(s): 838f20c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+
5
+ # Set up Streamlit
6
+ st.title("Emotion Detection with Transformers")
7
+
8
+ # Text input
9
+ user_input = st.text_area("Enter your text:")
10
+
11
+
12
+ # Function to load model and tokenizer using @st.cache_data
13
+ @st.cache_data()
14
+ def load_model_and_tokenizer():
15
+ model_name = "mrm8488/t5-base-finetuned-emotion"
16
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
17
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
18
+ return tokenizer, model
19
+
20
+
21
+ tokenizer, model = load_model_and_tokenizer()
22
+
23
+
24
+ # Function to analyze emotion
25
+ def analyze_emotion(text):
26
+ if text.strip() == "":
27
+ return "Please enter some text to analyze."
28
+
29
+ input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
30
+
31
+ output = model.generate(input_ids=input_ids,
32
+ max_length=2)
33
+
34
+ dec = [tokenizer.decode(ids, skip_special_tokens=True) for ids in output]
35
+ label = dec[0]
36
+
37
+ return f"Emotion: {label.capitalize()}"
38
+
39
+
40
+ # Analyze button
41
+ if st.button("Analyze Emotion"):
42
+ result = analyze_emotion(user_input)
43
+ st.write(result)