Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch
|
5 |
+
from datasets import load_dataset
|
6 |
+
|
7 |
+
# Load the model and tokenizer
|
8 |
+
model_name = "modelSamLowe/roberta-base-go_emotions"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Define the emotion labels (based on the GoEmotions dataset)
|
13 |
+
emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
|
14 |
+
"caring", "confusion", "curiosity", "desire", "disappointment",
|
15 |
+
"disapproval", "disgust", "embarrassment", "excitement", "fear",
|
16 |
+
"gratitude", "grief", "joy", "love", "nervousness", "optimism",
|
17 |
+
"pride", "realization", "relief", "remorse", "sadness", "surprise",
|
18 |
+
"neutral"]
|
19 |
+
|
20 |
+
# Function to classify emotion
|
21 |
+
def classify_emotion(text):
|
22 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
23 |
+
outputs = model(**inputs)
|
24 |
+
logits = outputs.logits
|
25 |
+
predicted_class_id = torch.argmax(logits, dim=-1).item()
|
26 |
+
return emotion_labels[predicted_class_id]
|
27 |
+
|
28 |
+
# Streamlit interface
|
29 |
+
st.title("Enron Emails Emotion Analysis")
|
30 |
+
|
31 |
+
# Button to run the inference script
|
32 |
+
if st.button("Run Inference"):
|
33 |
+
# Load the Enron dataset
|
34 |
+
with st.spinner('Loading dataset...'):
|
35 |
+
dataset = load_dataset("Hellisotherpeople/enron_emails_parsed")
|
36 |
+
enron_data = pd.DataFrame(dataset['train'])
|
37 |
+
|
38 |
+
# Apply emotion classification to the email content
|
39 |
+
with st.spinner('Running inference...'):
|
40 |
+
enron_data['emotion'] = enron_data['body'].apply(classify_emotion)
|
41 |
+
|
42 |
+
# Save the results to a CSV file
|
43 |
+
enron_data.to_csv("enron_emails_with_emotions.csv", index=False)
|
44 |
+
st.success("Inference completed and results saved!")
|
45 |
+
|
46 |
+
# Check if the results file exists and load it
|
47 |
+
try:
|
48 |
+
enron_data = pd.read_csv("enron_emails_with_emotions.csv")
|
49 |
+
|
50 |
+
# Dropdown for selecting an emotion
|
51 |
+
selected_emotion = st.selectbox("Select Emotion", emotion_labels)
|
52 |
+
|
53 |
+
# Filter emails based on the selected emotion
|
54 |
+
filtered_emails = enron_data[enron_data['emotion'] == selected_emotion].head(10)
|
55 |
+
|
56 |
+
# Display the filtered emails in a table
|
57 |
+
if not filtered_emails.empty:
|
58 |
+
st.write("Top 10 emails with emotion:", selected_emotion)
|
59 |
+
st.table(filtered_emails[['From', 'To', 'body', 'emotion']])
|
60 |
+
else:
|
61 |
+
st.write("No emails found with the selected emotion.")
|
62 |
+
except FileNotFoundError:
|
63 |
+
st.warning("Run inference first by clicking the 'Run Inference' button.")
|