Views
No views yet
barbaroo/gptsw3_translate_synth_6.7B with its base model.1
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
4import re
5import pandas as pd
6
7# Model repo
8MODEL_NAME = "barbaroo/gptsw3-6.7B-translation-en-fo"
9
10# Quantization config (8-bit)
11bnb_config = BitsAndBytesConfig(
12 load_in_8bit=True
13)
14
15# Initialize tokenizer & model
16tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
17model = AutoModelForCausalLM.from_pretrained(
18 MODEL_NAME,
19 quantization_config=bnb_config,
20 device_map="auto",
21)
22model.eval()
23
24# Alpaca-style prompt template
25alpaca_prompt = """
26
27### Instruction:
28{}
29
30### Input:
31{}
32
33### Response:
34{}"""
35
36EOS_TOKEN = tokenizer.eos_token
37print("EOS token:", EOS_TOKEN)
38
39# Example sentences
40sentences = ["I love Faroese!"]
41translations = []
42
43for sentence in sentences:
44 inputs = tokenizer(
45 [
46 alpaca_prompt.format(
47 "Translate this sentence from English to Faroese:",
48 sentence,
49 "",
50 )
51 ],
52 return_tensors="pt"
53 ).to("cuda")
54
55 outputs = model.generate(
56 **inputs,
57 max_new_tokens=500,
58 use_cache=True,
59 do_sample=True,
60 temperature=0.1,
61 top_p=1,
62 )
63
64 output_string = tokenizer.batch_decode(outputs, skip_special_tokens=False)[0]
65
66 try:
67 response = output_string.split("Response:\n", 1)[1]
68 translation = response.replace(EOS_TOKEN, "")
69 except IndexError:
70 translation = ""
71
72 translations.append(translation)
73 print(translation)
74