|
import pandas as pd |
|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import torch |
|
from datasets import load_dataset |
|
|
|
|
|
model_name = "modelSamLowe/roberta-base-go_emotions" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval", |
|
"caring", "confusion", "curiosity", "desire", "disappointment", |
|
"disapproval", "disgust", "embarrassment", "excitement", "fear", |
|
"gratitude", "grief", "joy", "love", "nervousness", "optimism", |
|
"pride", "realization", "relief", "remorse", "sadness", "surprise", |
|
"neutral"] |
|
|
|
|
|
def classify_emotion(text): |
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class_id = torch.argmax(logits, dim=-1).item() |
|
return emotion_labels[predicted_class_id] |
|
|
|
|
|
st.title("Enron Emails Emotion Analysis") |
|
|
|
|
|
if st.button("Run Inference"): |
|
|
|
with st.spinner('Loading dataset...'): |
|
dataset = load_dataset("Hellisotherpeople/enron_emails_parsed") |
|
enron_data = pd.DataFrame(dataset['train']) |
|
|
|
|
|
with st.spinner('Running inference...'): |
|
enron_data['emotion'] = enron_data['body'].apply(classify_emotion) |
|
|
|
|
|
enron_data.to_csv("enron_emails_with_emotions.csv", index=False) |
|
st.success("Inference completed and results saved!") |
|
|
|
|
|
try: |
|
enron_data = pd.read_csv("enron_emails_with_emotions.csv") |
|
|
|
|
|
selected_emotion = st.selectbox("Select Emotion", emotion_labels) |
|
|
|
|
|
filtered_emails = enron_data[enron_data['emotion'] == selected_emotion].head(10) |
|
|
|
|
|
if not filtered_emails.empty: |
|
st.write("Top 10 emails with emotion:", selected_emotion) |
|
st.table(filtered_emails[['From', 'To', 'body', 'emotion']]) |
|
else: |
|
st.write("No emails found with the selected emotion.") |
|
except FileNotFoundError: |
|
st.warning("Run inference first by clicking the 'Run Inference' button.") |
|
|