Unlocking the Power of Sabiá-7B: A Guide to Text Generation with Portuguese Language Models

Apr 4, 2024 | Educational

In this article, we’ll take a deep dive into how to effectively utilize the Sabiá-7B model, a state-of-the-art language model tailored specifically for Portuguese text generation. Whether you’re a researcher, developer, or AI enthusiast, this guide will walk you through the setup, usage, and troubleshooting of Sabiá-7B, ensuring you get the most from this incredible model.

Understanding Sabiá-7B

Sabiá-7B is an auto-regressive language model developed upon the architecture of LLaMA-1-7B. Its capacity to process and generate text based on the understanding of language makes it a powerful tool in the field of natural language processing (NLP). Think of Sabiá-7B as a highly trained assistant who can write, summarize, or categorize texts in Portuguese based on the input you provide.

Getting Started: Setup and Usage

To properly leverage Sabiá-7B for text generation, follow these steps:

Pre-requisites

  • Ensure you have Python installed on your system.
  • Install the necessary libraries using pip:
  • pip install torch transformers accelerate bitsandbytes

How to Use Sabiá-7B

Here’s how you can implement the model for a few-shot text classification task:


import torch
from transformers import LlamaTokenizer, LlamaForCausalLM

tokenizer = LlamaTokenizer.from_pretrained("maritaca-aisabia-7b")
model = LlamaForCausalLM.from_pretrained(
    "maritaca-aisabia-7b",
    device_map="auto",  # Automatically loads the model on the GPU if available
    low_cpu_mem_usage=True,
    torch_dtype=torch.bfloat16  # Change to torch.float16 if bfloat16 is unsupported
)

prompt = "Classifique a resenha de filme como positiva ou negativa.\nResenha: Gostei muito do filme, é o melhor do ano!\nClasse: positiva\nResenha: O filme deixa muito a desejar.\nClasse: negativa\nResenha: Apesar de longo, valeu o ingresso.\nClasse:"
input_ids = tokenizer(prompt, return_tensors="pt")
output = model.generate(
    input_ids["input_ids"].to("cuda"),
    max_length=1024,
    eos_token_id=tokenizer.encode("\n")  # Stop generation upon detecting a newline token
)

output = output[0][len(input_ids["input_ids"][0]):]
print(tokenizer.decode(output, skip_special_tokens=True))

Code Explanation through Analogy

Think of the code snippet above as planting a seed in a garden. The seed (the model) needs a suitable environment (the pre-trained settings and configurations) to thrive. Here’s how it works:

  • The first part involves preparing the soil. This is akin to importing necessary libraries and loading your tokenizer and model, ensuring your environment is ready for cultivation.
  • Next, you create a prompt—the seed itself. This prompt is what you want your garden (the model) to focus on and learn from.
  • As the model generates output based on your input, it’s like waiting for the flowers (the generated text) to bloom. You ask it to follow specific instructions, and based on the prompt you’ve provided, it produces an output.
  • Finally, just as you would harvest your crops, you decode the output to present it in an understandable format.

Troubleshooting

While working with Sabiá-7B, you may encounter some common issues. Here are some troubleshooting tips to help you resolve them:

  • Insufficient GPU Memory: If you find that your GPU crashes or runs out of memory, consider using 8-bit precision. Change the model loading line to include load_in_8bit=True.
  • Slow Performance: Ensure that you have installed the library bitsandbytes for efficient CPU memory usage, which can significantly improve performance.
  • Model Not Generating Predictions: Ensure your prompt follows the correct format. Any deviations can lead to unexpected outputs.

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

Results Overview

The performance of Sabiá-7B has been evaluated against several Portuguese datasets, achieving promising results:

  • ENEM Challenge (No Images): 55.07%
  • BLUEX (No Images): 47.71%
  • OAB Exams: 41.41%
  • Hate Speech Binary: 64.13%
  • tweetSentBR: 46.64%

Conclusion

Exploring and utilizing Sabiá-7B can revolutionize how we approach text generation in Portuguese. With its robust architecture and practical implementation, it stands as a testament to the advancements being made in natural language processing. 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