Upload folder using huggingface_hub
Browse files- README.md +1 -1
- __init__.py +6 -0
- example_usage.py +2 -1
- modeling_seamless_translation.py +118 -0
README.md
CHANGED
|
@@ -72,7 +72,7 @@ import torch
|
|
| 72 |
import numpy as np
|
| 73 |
import importlib.util
|
| 74 |
|
| 75 |
-
# Load model
|
| 76 |
model = AutoModel.from_pretrained("videoloc/seamless-translation")
|
| 77 |
config = AutoConfig.from_pretrained("videoloc/seamless-translation")
|
| 78 |
|
|
|
|
| 72 |
import numpy as np
|
| 73 |
import importlib.util
|
| 74 |
|
| 75 |
+
# Load model - architecture is included in the repository
|
| 76 |
model = AutoModel.from_pretrained("videoloc/seamless-translation")
|
| 77 |
config = AutoConfig.from_pretrained("videoloc/seamless-translation")
|
| 78 |
|
__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SeamlessTranslation model for HuggingFace Transformers
|
| 3 |
+
"""
|
| 4 |
+
from .modeling_seamless_translation import HFSeamlessTranslation, SeamlessTranslationConfig
|
| 5 |
+
|
| 6 |
+
__all__ = ["HFSeamlessTranslation", "SeamlessTranslationConfig"]
|
example_usage.py
CHANGED
|
@@ -8,6 +8,7 @@ import numpy as np
|
|
| 8 |
import importlib.util
|
| 9 |
|
| 10 |
def load_model_and_collator():
|
|
|
|
| 11 |
model = AutoModel.from_pretrained("videoloc/seamless-translation")
|
| 12 |
config = AutoConfig.from_pretrained("videoloc/seamless-translation")
|
| 13 |
|
|
@@ -31,7 +32,7 @@ def example_inference():
|
|
| 31 |
# Example data with translation awareness
|
| 32 |
data = [{
|
| 33 |
'raw_audio': np.random.randn(16000 * 3), # 3 seconds at 16kHz
|
| 34 |
-
'raw_text': "Example subtitle text for
|
| 35 |
'is_translation': 1, # 1 for translated content, 0 for original
|
| 36 |
}]
|
| 37 |
|
|
|
|
| 8 |
import importlib.util
|
| 9 |
|
| 10 |
def load_model_and_collator():
|
| 11 |
+
# Load model - architecture is included in the repository
|
| 12 |
model = AutoModel.from_pretrained("videoloc/seamless-translation")
|
| 13 |
config = AutoConfig.from_pretrained("videoloc/seamless-translation")
|
| 14 |
|
|
|
|
| 32 |
# Example data with translation awareness
|
| 33 |
data = [{
|
| 34 |
'raw_audio': np.random.randn(16000 * 3), # 3 seconds at 16kHz
|
| 35 |
+
'raw_text': "Example subtitle text for TTE prediction",
|
| 36 |
'is_translation': 1, # 1 for translated content, 0 for original
|
| 37 |
}]
|
| 38 |
|
modeling_seamless_translation.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 5 |
+
from transformers.modeling_outputs import SequenceClassifierOutput
|
| 6 |
+
from transformers import SeamlessM4TModel
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class SeamlessTranslationConfig(PretrainedConfig):
|
| 13 |
+
"""Configuration class for SeamlessTranslation model."""
|
| 14 |
+
|
| 15 |
+
model_type = "seamless_translation"
|
| 16 |
+
|
| 17 |
+
def __init__(
|
| 18 |
+
self,
|
| 19 |
+
seamless_model_name="facebook/hf-seamless-m4t-medium",
|
| 20 |
+
hidden_size=1024,
|
| 21 |
+
dropout_prob=0.1,
|
| 22 |
+
**kwargs
|
| 23 |
+
):
|
| 24 |
+
super().__init__(**kwargs)
|
| 25 |
+
self.seamless_model_name = seamless_model_name
|
| 26 |
+
self.hidden_size = hidden_size
|
| 27 |
+
self.dropout_prob = dropout_prob
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class HFSeamlessTranslation(PreTrainedModel):
|
| 31 |
+
"""SeamlessM4T model with translation features for HuggingFace Hub."""
|
| 32 |
+
|
| 33 |
+
config_class = SeamlessTranslationConfig
|
| 34 |
+
supports_gradient_checkpointing = True
|
| 35 |
+
|
| 36 |
+
def __init__(self, config):
|
| 37 |
+
super().__init__(config)
|
| 38 |
+
self.config = config
|
| 39 |
+
|
| 40 |
+
# Load the underlying SeamlessM4T model
|
| 41 |
+
self.seamless_model = SeamlessM4TModel.from_pretrained(config.seamless_model_name)
|
| 42 |
+
self.seamless_model_speech_encoder = self.seamless_model.speech_encoder
|
| 43 |
+
self.seamless_model_text_encoder = self.seamless_model.text_encoder
|
| 44 |
+
|
| 45 |
+
# Freeze pre-trained models
|
| 46 |
+
for param in self.seamless_model_speech_encoder.parameters():
|
| 47 |
+
param.requires_grad = False
|
| 48 |
+
for param in self.seamless_model_text_encoder.parameters():
|
| 49 |
+
param.requires_grad = False
|
| 50 |
+
|
| 51 |
+
# Projection layers
|
| 52 |
+
self.audio_proj = nn.Linear(
|
| 53 |
+
self.seamless_model_speech_encoder.config.hidden_size,
|
| 54 |
+
config.hidden_size
|
| 55 |
+
)
|
| 56 |
+
self.text_proj = nn.Linear(
|
| 57 |
+
self.seamless_model_text_encoder.config.hidden_size,
|
| 58 |
+
config.hidden_size
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Translation feature embedding
|
| 62 |
+
self.translation_proj = nn.Linear(1, 64)
|
| 63 |
+
|
| 64 |
+
# Classification head (2048 + 64 = 2112)
|
| 65 |
+
self.fc = nn.Sequential(
|
| 66 |
+
nn.Linear(2112, 1024),
|
| 67 |
+
nn.ReLU(),
|
| 68 |
+
nn.Dropout(config.dropout_prob),
|
| 69 |
+
nn.Linear(1024, 512),
|
| 70 |
+
nn.ReLU(),
|
| 71 |
+
nn.Dropout(config.dropout_prob),
|
| 72 |
+
nn.Linear(512, 256),
|
| 73 |
+
nn.ReLU(),
|
| 74 |
+
nn.Dropout(config.dropout_prob),
|
| 75 |
+
nn.Linear(256, 1)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
def forward(
|
| 79 |
+
self,
|
| 80 |
+
input_features,
|
| 81 |
+
input_ids,
|
| 82 |
+
text_attention_mask,
|
| 83 |
+
is_translation,
|
| 84 |
+
audio_attention_mask=None,
|
| 85 |
+
labels=None,
|
| 86 |
+
**kwargs # Accept additional features but ignore them
|
| 87 |
+
):
|
| 88 |
+
# Encode audio
|
| 89 |
+
audio_emb = self.seamless_model_speech_encoder(
|
| 90 |
+
input_features=input_features,
|
| 91 |
+
attention_mask=audio_attention_mask
|
| 92 |
+
).last_hidden_state.mean(dim=1)
|
| 93 |
+
audio_emb = self.audio_proj(audio_emb)
|
| 94 |
+
|
| 95 |
+
# Encode text
|
| 96 |
+
text_emb = self.seamless_model_text_encoder(
|
| 97 |
+
input_ids=input_ids,
|
| 98 |
+
attention_mask=text_attention_mask
|
| 99 |
+
).last_hidden_state.mean(dim=1)
|
| 100 |
+
text_emb = self.text_proj(text_emb)
|
| 101 |
+
|
| 102 |
+
# Process translation feature
|
| 103 |
+
translation_emb = self.translation_proj(is_translation.unsqueeze(-1))
|
| 104 |
+
|
| 105 |
+
# Combine features
|
| 106 |
+
combined = torch.cat([audio_emb, text_emb, translation_emb], dim=1) # (batch_size, 2112)
|
| 107 |
+
|
| 108 |
+
logits = self.fc(combined).squeeze(-1)
|
| 109 |
+
|
| 110 |
+
# Compute loss if labels are provided
|
| 111 |
+
loss = F.mse_loss(logits, labels) if labels is not None else None
|
| 112 |
+
|
| 113 |
+
return SequenceClassifierOutput(
|
| 114 |
+
loss=loss,
|
| 115 |
+
logits=logits,
|
| 116 |
+
hidden_states=None,
|
| 117 |
+
attentions=None
|
| 118 |
+
)
|