How to Perform Sentiment Analysis Using PEFT with Llama-3-8B

Oct 28, 2024 | Educational

If you’re diving into the realm of Natural Language Processing (NLP) and sentiment analysis, you’ve landed in the right spot. In this guide, we will explore how to utilize the PEFT library and the robust Llama-3-8B model to analyze sentiments effectively. Buckle up as we walk you through the steps!

Prerequisites: Setting Up Your Environment

Before we delve into coding, ensure you have the necessary libraries installed. Here’s how you can set up your environment:

  • Run the following commands in your terminal or in a Google Colab notebook:
!pip install transformers==4.40.1 peft==0.4.0
!pip install sentencepiece
!pip install accelerate
!pip install torch
!pip install datasets
!pip install bitsandbytes

Loading the Model

Now that we have our libraries ready, it’s time to load the model. Think of this step like creating the perfect blend of coffee; combining the right ingredients can make all the difference!

from transformers import AutoModel, AutoTokenizer, LlamaForCausalLM, LlamaTokenizerFast
from peft import PeftModel  
import torch

# Load Models
base_model = "meta-llama/Meta-Llama-3-8B"
peft_model = "FinGPT/fingpt-mt_llama3-8b_lora"

tokenizer = LlamaTokenizerFast.from_pretrained(base_model, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
model = LlamaForCausalLM.from_pretrained(base_model, trust_remote_code=True, device_map="cuda:0")
model = PeftModel.from_pretrained(model, peft_model)
model = model.eval()

# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)

The code above is like preparing a gourmet recipe – it gathers all the right elements (models and tokenizers) and places them in your kitchen (device) for cooking (processing the text).

Preparing Your Sentiment Analysis Tasks

Next, we’ll set up our prompts for sentiment analysis. Picture this step as writing the screenplay for a movie – outlining exactly what story we want to analyze.

# Make prompts
prompt = [
    "Instruction: What is the sentiment of this news? Please choose an answer from negative, neutral, positive.\nInput: FINANCING OF ASPOCOMP S GROWTH. Aspocomp is aggressively pursuing its growth strategy by increasingly focusing on technologically more demanding HDI printed circuit boards (PCBs).\nAnswer: ",
    "Instruction: What is the sentiment of this news? Please choose an answer from negative, neutral, positive.\nInput: According to Gran, the company has no plans to move all production to Russia, although that is where the company is growing.\nAnswer: "
]

Running Inference: Generating Sentiment Predictions

We’re almost there! Now we will run our model to generate predictions. Just like baking, you now put everything in the oven and wait for the magic to happen:

tokens = tokenizer(prompt, return_tensors="pt", padding=True, max_length=512).to(device)
res = model.generate(**tokens, max_length=512)
res_sentences = [tokenizer.decode(i) for i in res]
out_text = [o.split("Answer: ")[1] for o in res_sentences]

# Show results
for sentiment in out_text:
    print(sentiment)

Here, the model processes the input prompts and generates the sentiment responses, which are displayed like the freshly baked cookies from the oven!

Troubleshooting Common Issues

If you encounter any issues while setting up or running your sentiment analysis, here are some troubleshooting tips:

  • Ensure that all necessary packages are installed correctly.
  • Check if your device is configured correctly (CPU vs. CUDA). You can test this by printing ‘torch.cuda.is_available()’.
  • Verify that the model names provided match with the ones available in the library.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By following this guide, you’ve successfully set up a sentiment analysis model using PEFT and Llama-3-8B! Feel free to experiment with different prompts to enhance your understanding of sentiment dynamics.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox