Creating an AI model to parse emotions was a challenging yet rewarding experience that deepened my understanding of machine learning workflows. Using Hugging Face’s robust ecosystem, I built and fine-tuned a mood parser model capable of analyzing text and categorizing emotions such as happiness, sadness, and anger. Here’s how I approached this project and the lessons I learned along the way.
Step 1: Setting Up the Environment
I started by setting up my Python environment and installing the necessary libraries:
pip install transformers datasets transformers[torch]
The Hugging Face Transformers library, combined with PyTorch, provided all the tools I needed to handle pre-trained models and datasets efficiently.
Step 2: Dataset Preparation
For this project, I used the Finance Emotions Dataset, hosted on Hugging Face. It contains labeled text samples, which made it ideal for emotion classification tasks. Splitting the dataset into training and testing subsets, I ensured proper evaluation metrics:
from datasets import load_dataset
dataset_name = "vamossyd/finance_emotions"
train_dataset = load_dataset(dataset_name, split="train[:90%]")
test_dataset = load_dataset(dataset_name, split="train[90%:]")
Exploring the dataset, I discovered fascinating examples of labeled text, highlighting the nuances of different emotional tones.
Step 3: Tokenization
To process the text efficiently, I employed a pre-trained BERT tokenizer. It converts sentences into numerical representations that the model can understand. I also created a mapping between emotion labels (e.g., “happy,” “sad”) and integer values:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
str_to_int = {"neutral": 0, "sad": 1, "anger": 2, "disgust": 3, "surprise": 4, "fear": 5, "happy": 6}def tokenize_function(batch):
tokenized_batch = tokenizer(batch['cleaned_text'], padding="max_length", truncation=True)
tokenized_batch['label'] = [str_to_int[label] for label in batch['label']]
return tokenized_batchtokenized_train_data = train_dataset.map(tokenize_function, batched=True)
tokenized_test_data = test_dataset.map(tokenize_function, batched=True)
This step was crucial in ensuring uniform input size for training, capped at a maximum of 512 tokens.
Step 4: Model Initialization
Using the BERT-base-cased model, I initialized a sequence classification architecture with 7 output labels corresponding to the emotions:
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=7)
This pre-trained model provided a strong foundation, requiring only fine-tuning to adapt to the emotion parsing task.
Step 5: Training the Model
To train the model, I defined training arguments and configured evaluation metrics (accuracy in this case):
from transformers import TrainingArguments, Trainer
import evaluate
import numpy as np
metric = evaluate.load("accuracy")def compute_accuracy(eval_pred):
logits, labels = eval_pred
predictions = np.argmax(logits, axis=-1)
return metric.compute(predictions=predictions, references=labels)training_args = TrainingArguments(
output_dir="Mood-Parsers",
evaluation_strategy="epoch",
run_name="my_mood_parser_experiment"
)trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_train_data,
eval_dataset=tokenized_test_data,
compute_metrics=compute_accuracy
)trainer.train()
Training the model was a multi-epoch process. Monitoring metrics like accuracy helped identify when the model reached optimal performance.
Step 6: Saving and Deploying the Model
Once training was complete, I saved and pushed the model to the Hugging Face Model Hub:
model.save_pretrained("Mood-Parser-Ai", push_to_hub=True, private=False)
This made my model publicly available, allowing others to access and use it for mood analysis tasks.
Lessons Learned
- Preprocessing Matters: Proper tokenization and label mapping were critical for successful training.
- Model Selection: Leveraging pre-trained models like BERT accelerated the process significantly.
- Dataset Quality: A well-labeled dataset was essential for high accuracy and generalization.
Try It Out
The Mood Parser AI model is now live on Hugging Face! Check it out here and test its ability to classify emotions from text inputs.
Closing Thoughts
This project was a significant milestone in my machine learning journey. Hugging Face’s tools made it possible to go from concept to deployment efficiently. If you’re interested in building AI models, I highly recommend diving into Hugging Face’s ecosystem — it’s a game changer!
Let me know what you think or if you have any questions about the process!