|
import pandas as pd |
|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import torch |
|
from datasets import load_dataset |
|
import time |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
model_name = "SamLowe/roberta-base-go_emotions" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device) |
|
|
|
|
|
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_emotions_in_batches(texts, batch_size=64, output_file="enron_emails_with_emotions.csv"): |
|
results = [] |
|
start_time = time.time() |
|
|
|
|
|
result_df = pd.DataFrame(columns=['From', 'To', 'body', 'emotion']) |
|
|
|
for i in range(0, len(texts), batch_size): |
|
batch = texts[i:i+batch_size] |
|
inputs = tokenizer(batch, return_tensors="pt", truncation=True, padding=True).to(device) |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class_ids = torch.argmax(logits, dim=-1).tolist() |
|
results.extend(predicted_class_ids) |
|
|
|
|
|
batch_results = { |
|
'From': enron_data['From'][i:i+batch_size], |
|
'To': enron_data['To'][i:i+batch_size], |
|
'body': batch, |
|
'emotion': [emotion_labels[idx] for idx in predicted_class_ids] |
|
} |
|
batch_df = pd.DataFrame(batch_results) |
|
result_df = pd.concat([result_df, batch_df]) |
|
|
|
|
|
result_df.to_csv(output_file, index=False) |
|
|
|
|
|
batch_time = time.time() - start_time |
|
st.write(f"Processed batch {i//batch_size + 1} of {len(texts)//batch_size + 1} in {batch_time:.2f} seconds") |
|
start_time = time.time() |
|
|
|
return result_df |
|
|
|
|
|
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...'): |
|
email_texts = enron_data['body'].tolist() |
|
classify_emotions_in_batches(email_texts, batch_size=64) |
|
|
|
st.success("Inference completed and results saved!") |
|
|