Upload modeling_yor_audio.py with huggingface_hub
Browse files- modeling_yor_audio.py +53 -3
modeling_yor_audio.py
CHANGED
|
@@ -1,13 +1,63 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
-
from transformers import AutoModelForCausalLM, PreTrainedModel
|
| 4 |
from transformers.models.qwen2_audio.modeling_qwen2_audio import Qwen2AudioEncoder
|
| 5 |
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 6 |
-
|
| 7 |
-
from configuration_yor_audio import YorAudioConfig
|
| 8 |
|
| 9 |
AUDIO_TOKENS_PER_SEGMENT = 750
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class YorAudioMultiModalProjector(nn.Module):
|
| 13 |
def __init__(self, config: YorAudioConfig):
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, PreTrainedModel
|
| 4 |
from transformers.models.qwen2_audio.modeling_qwen2_audio import Qwen2AudioEncoder
|
| 5 |
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 6 |
+
from transformers import Qwen2AudioConfig, Qwen2AudioEncoderConfig
|
|
|
|
| 7 |
|
| 8 |
AUDIO_TOKENS_PER_SEGMENT = 750
|
| 9 |
|
| 10 |
+
AUDIO_SPECIAL_TOKENS = ["<|audio_bos|>", "<|audio_eos|>", "<|AUDIO|>"]
|
| 11 |
+
|
| 12 |
+
WHISPER_LARGE_V3_AUDIO_CONFIG = {
|
| 13 |
+
"num_mel_bins": 128,
|
| 14 |
+
"encoder_layers": 32,
|
| 15 |
+
"encoder_attention_heads": 20,
|
| 16 |
+
"encoder_ffn_dim": 5120,
|
| 17 |
+
"d_model": 1280,
|
| 18 |
+
"dropout": 0.0,
|
| 19 |
+
"attention_dropout": 0.0,
|
| 20 |
+
"activation_function": "gelu",
|
| 21 |
+
"max_source_positions": 1500,
|
| 22 |
+
"initializer_range": 0.02,
|
| 23 |
+
"scale_embedding": False,
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class YorAudioConfig(Qwen2AudioConfig):
|
| 28 |
+
model_type = "yor_audio"
|
| 29 |
+
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
audio_config=None,
|
| 33 |
+
text_config=None,
|
| 34 |
+
audio_token_index: int = None,
|
| 35 |
+
afriqueqwen_path: str = "McGill-NLP/AfriqueQwen3.5-9B-50Langs-Instruct-v1",
|
| 36 |
+
**kwargs,
|
| 37 |
+
):
|
| 38 |
+
if audio_config is None:
|
| 39 |
+
audio_config = Qwen2AudioEncoderConfig(**WHISPER_LARGE_V3_AUDIO_CONFIG)
|
| 40 |
+
elif isinstance(audio_config, dict):
|
| 41 |
+
audio_config = Qwen2AudioEncoderConfig(**audio_config)
|
| 42 |
+
|
| 43 |
+
if text_config is None:
|
| 44 |
+
from transformers import Qwen2Config
|
| 45 |
+
text_config = Qwen2Config()
|
| 46 |
+
elif isinstance(text_config, dict):
|
| 47 |
+
text_config = AutoConfig.for_model(**text_config)
|
| 48 |
+
|
| 49 |
+
if audio_token_index is None:
|
| 50 |
+
audio_token_index = text_config.vocab_size
|
| 51 |
+
|
| 52 |
+
self.afriqueqwen_path = afriqueqwen_path
|
| 53 |
+
|
| 54 |
+
super().__init__(
|
| 55 |
+
audio_config=audio_config,
|
| 56 |
+
text_config=text_config,
|
| 57 |
+
audio_token_index=audio_token_index,
|
| 58 |
+
**kwargs,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
|
| 62 |
class YorAudioMultiModalProjector(nn.Module):
|
| 63 |
def __init__(self, config: YorAudioConfig):
|