barbaroo/Sprotin_parallel
Viewer • Updated • 126k • 19
How to use barbaroo/gptsw3_translate_synth_6.7B with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("AI-Sweden-Models/gpt-sw3-6.7b-v2")
model = PeftModel.from_pretrained(base_model, "barbaroo/gptsw3_translate_synth_6.7B")Model Description
This adapter is intended to perform English→Faroese translation, leveraging a parameter-efficient fine-tuning (PEFT) approach.
import torch
from peft import AutoPeftModelForCausalLM
from transformers import AutoTokenizer
import pandas as pd
ADAPTER_REPO = "barbaroo/gptsw3_translate_synth_6.7B"
BASE_MODEL = "AI-Sweden-Models/gpt-sw3-6.7b-v2"
# 1. Load the tokenizer from the base model
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
model = AutoPeftModelForCausalLM.from_pretrained(
ADAPTER_REPO,
load_in_8bit=True, # Optional: 8-bit quantization for GPU memory efficiency
device_map="auto", # Automatically spread layers across available GPUs
dtype=torch.float16,
)
# Ensure the model is in evaluation mode
model.eval()
# Alpaca-style prompt template
alpaca_prompt = """
### Instruction:
{}
### Input:
{}
### Response:
{}
"""
# EOS token from the tokenizer
EOS_TOKEN = tokenizer.eos_token
print(EOS_TOKEN)
sentences = ['hello world']
translations = []
for sentence in sentences:
# Tokenize the input sentence and prepare the prompt for each sentence
inputs = tokenizer(
[
alpaca_prompt.format(
"Translate this sentence from English to Faroese:", # instruction
sentence, # input sentence to translate
"", # output - leave blank for generation
)
],
return_tensors="pt"
).to("cuda")
# Generate the output
outputs = model.generate(**inputs,
max_new_tokens=500,
eos_token_id=tokenizer.eos_token_id, # Ensure EOS token is used
pad_token_id=tokenizer.pad_token_id, # Ensure padding token is used
use_cache=True,
do_sample = True,
temperature = 0.1,
top_p=1)
# Decode the generated tokens into a string
output_string = tokenizer.batch_decode(outputs, skip_special_tokens=False)[0]
#print(output_string)
# Use a regular expression to extract the response part
try:
spl_word_1 = 'Response:\n'
res = output_string.split(spl_word_1, 1)
response = res[1]
translation = response.replace(EOS_TOKEN, '')
translations.append(translation)
except:
translation = ''
translations.append(translation)
print(translation)
We used the Sprotin parallel corpus, and synthetic data: barbaroo/Sprotin_parallel, barbaroo/fo_en_synthetic.
AI-Sweden-Models/gpt-sw3-6.7b-v2.[More Information Needed]
Base model
AI-Sweden-Models/gpt-sw3-6.7b-v2