Views
No views yet
RichNachos/georgian-corpus-tokenizer-testEncoderDecoderTokenizer that is included in the repository. You need to download the repo locally to access it.1import sys
2from transformers import EncoderDecoderModel
3import torch
4import re
5from huggingface_hub import snapshot_download
6
7# Download the repo to a local folder
8path_to_downloaded = snapshot_download(
9 repo_id="Darsala/Georgian-Translation",
10 local_dir="./Georgian-Translation",
11 local_dir_use_symlinks=False
12)
13
14# Add the downloaded folder to Python path so we can import the custom tokenizer
15sys.path.append(path_to_downloaded)
16from encoder_decoder_tokenizer import EncoderDecoderTokenizer
17
18# Load the model and tokenizer from the downloaded folder
19model = EncoderDecoderModel.from_pretrained(path_to_downloaded)
20tokenizer = EncoderDecoderTokenizer.from_pretrained(path_to_downloaded)
21device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22model.to(device)
23
24def translate(
25 text: str,
26 num_beams: int = 5,
27 max_length: int = 256,
28) -> str:
29 """
30 Translate a single string with the given EncoderDecoderModel.
31 """
32 text = text.lower()
33 text = re.sub(r'\s+', ' ', text)
34
35 # tokenize & move to device
36 inputs = tokenizer(
37 text,
38 return_tensors="pt",
39 truncation=True,
40 padding="longest"
41 ).to(device)
42
43 # generation
44 generated_ids = model.generate(
45 input_ids=inputs.input_ids,
46 attention_mask=inputs.attention_mask,
47 num_beams=num_beams,
48 max_length=max_length,
49 early_stopping=True,
50 )
51
52 output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
53 print(f"English: {text}")
54 print(f"Translated: {output}")
55
56 return output
57
58# Example usage
59translation = translate("Hello, how are you?")EncoderDecoderTokenizer that is included in the repository.1@mastersthesis{darsalia2025georgian,
2 title={English Translation Quality Assessment and Computer Translation},
3 author={Luka Darsalia},
4 year={2025},
5 school={Tbilisi University},
6 note={Bachelor's Thesis - Computer Science}
7}