Sreekannan commited on
Commit
ff6986a
1 Parent(s): 34efa22

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +66 -0
  2. emotion_analysis.py +17 -0
  3. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from emotion_analysis import get_emotion
4
+ import base64
5
+
6
+
7
+ def read_data(file_path):
8
+ file_extension = file_path.split('.')[-1].lower()
9
+
10
+ if file_extension == 'xlsx' or file_extension == 'xls':
11
+ data = pd.read_excel(file_path)
12
+ elif file_extension == 'csv':
13
+ data = pd.read_csv(file_path)
14
+ else:
15
+ raise ValueError("Unsupported file format. Only Excel (xlsx, xls) and CSV (csv) files are supported.")
16
+
17
+ return data
18
+
19
+
20
+ # Streamlit app
21
+ def main():
22
+ st.title("Text Emotion Detection")
23
+ menu = ["Input Text", "Batch Processing"]
24
+ option = st.sidebar.radio("Select an option", menu)
25
+
26
+
27
+ if option == "Input Text":
28
+ text = st.text_area("Enter your text:")
29
+ if st.button("Submit"):
30
+ if text.strip() != "":
31
+ emotion_detail, confidence_score = get_emotion(text)
32
+ st.write("Detected Emotion")
33
+ st.write(f"{emotion_detail[0]} - {confidence_score}")
34
+ else:
35
+ st.write("Please enter some text.")
36
+
37
+ elif option == "Batch Processing":
38
+ uploaded_file = st.file_uploader("Upload CSV or Excel file", type=["csv", "xlsx"])
39
+
40
+ if uploaded_file is not None:
41
+ file_name = uploaded_file.name
42
+ file_extension = file_name.split('.')[-1].lower()
43
+ file_name = uploaded_file.name
44
+ if file_extension == 'xlsx' or file_extension == 'xls':
45
+ dataframe = pd.read_excel(uploaded_file)
46
+ elif file_extension == 'csv':
47
+ dataframe = pd.read_csv(uploaded_file)
48
+ else:
49
+ raise ValueError("Unsupported file format. Only Excel (xlsx, xls) and CSV (csv) files are supported.")
50
+ # dataframe = pd.read_excel(uploaded_file)
51
+ if "text" not in dataframe.columns:
52
+ st.write("CSV file should have a 'text' column.")
53
+ else:
54
+ dataframe["emotion"], dataframe["confidence"] = zip(*dataframe["text"].map(get_emotion))
55
+ st.write("Detected Emotions")
56
+ st.write(dataframe)
57
+ # Download button
58
+ csv = dataframe.to_csv(index=False)
59
+ b64 = base64.b64encode(csv.encode()).decode() # Convert DataFrame to CSV string
60
+ href = f'<a href="data:file/csv;base64,{b64}" download="processed_data.csv">Download</a>'
61
+ st.markdown(href, unsafe_allow_html=True)
62
+ else:
63
+ pass
64
+
65
+ if __name__ == '__main__':
66
+ main()
emotion_analysis.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import RobertaTokenizerFast, TFRobertaForSequenceClassification, pipeline
3
+
4
+ tokenizer = RobertaTokenizerFast.from_pretrained("arpanghoshal/EmoRoBERTa")
5
+ model = TFRobertaForSequenceClassification.from_pretrained("arpanghoshal/EmoRoBERTa")
6
+
7
+ emotion = pipeline('sentiment-analysis',
8
+ model='arpanghoshal/EmoRoBERTa')
9
+
10
+
11
+ def get_emotion(text):
12
+ emotion_labels = emotion(text)
13
+ emotion_detail = [item['label'] for item in emotion_labels]
14
+ print("The detected emotion is:", emotion_detail)
15
+ confidence_score = str(round([item['score'] for item in emotion_labels][0]*100, 2)) + "%"
16
+ print("The confidence score is:", confidence_score)
17
+ return emotion_detail[0], confidence_score
requirements.txt ADDED
Binary file (3.67 kB). View file