Add reademe
Browse files
README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Tradutor
|
| 2 |
+
|
| 3 |
+
A translation system from English to European Portuguese.
|
| 4 |
+
|
| 5 |
+
## Usage
|
| 6 |
+
|
| 7 |
+
```python
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
model_name = "liaad/Tradutor"
|
| 13 |
+
max_length = 1024
|
| 14 |
+
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
model_name,
|
| 18 |
+
device_map="auto",
|
| 19 |
+
torch_dtype=torch.bfloat16,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
text = "Hello, how are you?"
|
| 24 |
+
chat = [
|
| 25 |
+
{
|
| 26 |
+
"role": "system",
|
| 27 |
+
"content": "You are a translator from English to European Portuguese",
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": f"Translate this text from English to European Portuguese: {text}",
|
| 32 |
+
},
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
input_ids = tokenizer.apply_chat_template(
|
| 36 |
+
chat,
|
| 37 |
+
add_generation_prompt=True,
|
| 38 |
+
tokenize=True,
|
| 39 |
+
return_tensors="pt",
|
| 40 |
+
max_length=max_length,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
output_ids = model.generate(
|
| 44 |
+
input_ids,
|
| 45 |
+
max_length=max_length,
|
| 46 |
+
num_return_sequences=1,
|
| 47 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
generated_ids = output_ids[0, input_ids.shape[1] :]
|
| 51 |
+
translated_text = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 52 |
+
|
| 53 |
+
print(translated_text.strip())
|
| 54 |
+
```
|