Spaces:
Running
Running
Update README.md
Browse filesAdded info to the read me page
README.md
CHANGED
|
@@ -10,3 +10,128 @@ short_description: We will be translating one language to another
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
Developing a translation model using Hugging Face involves leveraging their extensive library of pre-trained models, particularly those from the Transformers family. Here’s a step-by-step guide to creating a simple translation model:
|
| 18 |
+
|
| 19 |
+
Step 1: Install the Transformers Library
|
| 20 |
+
First, ensure you have the Transformers library installed. If not, you can install it using pip:
|
| 21 |
+
|
| 22 |
+
bash
|
| 23 |
+
pip install transformers
|
| 24 |
+
Step 2: Choose a Pre-Trained Model
|
| 25 |
+
Hugging Face provides several pre-trained models for translation tasks. One popular choice is the t5-base model, which is versatile and can be fine-tuned for various translation tasks. However, for direct translation, models like Helsinki-NLP/opus-mt-en-fr are more suitable.
|
| 26 |
+
|
| 27 |
+
Step 3: Load the Model and Tokenizer
|
| 28 |
+
You can use the pipeline() function to load a pre-trained model for translation. Here’s how you can do it:
|
| 29 |
+
|
| 30 |
+
python
|
| 31 |
+
from transformers import pipeline
|
| 32 |
+
|
| 33 |
+
# Load a pre-trained translation model
|
| 34 |
+
translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
|
| 35 |
+
|
| 36 |
+
# Example text to translate
|
| 37 |
+
text = "Hello, how are you?"
|
| 38 |
+
|
| 39 |
+
# Translate the text
|
| 40 |
+
result = translator(text)
|
| 41 |
+
|
| 42 |
+
# Print the translation
|
| 43 |
+
print(result)
|
| 44 |
+
Step 4: Fine-Tune the Model (Optional)
|
| 45 |
+
If you want to improve the model's performance on a specific dataset or domain, you can fine-tune it. This involves loading the model and tokenizer, preparing your dataset, and then training the model on your data.
|
| 46 |
+
|
| 47 |
+
Here’s a simplified example of fine-tuning a translation model:
|
| 48 |
+
|
| 49 |
+
python
|
| 50 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 51 |
+
from torch.utils.data import Dataset, DataLoader
|
| 52 |
+
import torch
|
| 53 |
+
|
| 54 |
+
# Load pre-trained model and tokenizer
|
| 55 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
|
| 56 |
+
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
|
| 57 |
+
|
| 58 |
+
# Example dataset class
|
| 59 |
+
class TranslationDataset(Dataset):
|
| 60 |
+
def __init__(self, data, tokenizer):
|
| 61 |
+
self.data = data
|
| 62 |
+
self.tokenizer = tokenizer
|
| 63 |
+
|
| 64 |
+
def __len__(self):
|
| 65 |
+
return len(self.data)
|
| 66 |
+
|
| 67 |
+
def __getitem__(self, idx):
|
| 68 |
+
source_text, target_text = self.data[idx]
|
| 69 |
+
source_ids = self.tokenizer.encode(source_text, return_tensors="pt")
|
| 70 |
+
target_ids = self.tokenizer.encode(target_text, return_tensors="pt")
|
| 71 |
+
|
| 72 |
+
return {
|
| 73 |
+
"input_ids": source_ids,
|
| 74 |
+
"labels": target_ids,
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
# Example data
|
| 78 |
+
data = [
|
| 79 |
+
("Hello, how are you?", "Bonjour, comment vas-tu?"),
|
| 80 |
+
# Add more data here...
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
# Create dataset and data loader
|
| 84 |
+
dataset = TranslationDataset(data, tokenizer)
|
| 85 |
+
batch_size = 16
|
| 86 |
+
data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
|
| 87 |
+
|
| 88 |
+
# Training loop
|
| 89 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 90 |
+
model.to(device)
|
| 91 |
+
|
| 92 |
+
for epoch in range(5): # Number of epochs
|
| 93 |
+
model.train()
|
| 94 |
+
for batch in data_loader:
|
| 95 |
+
input_ids = batch["input_ids"].to(device)
|
| 96 |
+
labels = batch["labels"].to(device)
|
| 97 |
+
|
| 98 |
+
# Zero the gradients
|
| 99 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)
|
| 100 |
+
optimizer.zero_grad()
|
| 101 |
+
|
| 102 |
+
# Forward pass
|
| 103 |
+
outputs = model(input_ids, labels=labels)
|
| 104 |
+
loss = outputs.loss
|
| 105 |
+
|
| 106 |
+
# Backward pass
|
| 107 |
+
loss.backward()
|
| 108 |
+
|
| 109 |
+
# Update model parameters
|
| 110 |
+
optimizer.step()
|
| 111 |
+
|
| 112 |
+
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
|
| 113 |
+
|
| 114 |
+
# Save the fine-tuned model
|
| 115 |
+
model.save_pretrained("fine_tuned_model")
|
| 116 |
+
tokenizer.save_pretrained("fine_tuned_model")
|
| 117 |
+
Step 5: Use the Fine-Tuned Model for Translation
|
| 118 |
+
After fine-tuning, you can use the model for translating text:
|
| 119 |
+
|
| 120 |
+
python
|
| 121 |
+
# Load the fine-tuned model and tokenizer
|
| 122 |
+
fine_tuned_model = AutoModelForSeq2SeqLM.from_pretrained("fine_tuned_model")
|
| 123 |
+
fine_tuned_tokenizer = AutoTokenizer.from_pretrained("fine_tuned_model")
|
| 124 |
+
|
| 125 |
+
# Create a translation pipeline
|
| 126 |
+
def translate_text(text):
|
| 127 |
+
input_ids = fine_tuned_tokenizer.encode(text, return_tensors="pt")
|
| 128 |
+
output = fine_tuned_model.generate(input_ids)
|
| 129 |
+
return fine_tuned_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 130 |
+
|
| 131 |
+
# Example translation
|
| 132 |
+
text = "Hello, how are you?"
|
| 133 |
+
translation = translate_text(text)
|
| 134 |
+
print(translation)
|
| 135 |
+
This guide provides a basic overview of creating a translation model using Hugging Face. Depending on your specific needs, you might need to adjust the model choice, dataset preparation, and training parameters.
|
| 136 |
+
|
| 137 |
+
|