Spaces:
Sleeping
Sleeping
Download src/modeling/loading.py from KeisukeMiyamoto/lambda-audio-v6: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/KeisukeMiyamoto/lambda-audio-v6/resolve/main/src/modeling/loading.py
- Command line
-
hf download hf://spaces/KeisukeMiyamoto/lambda-audio-v6/src/modeling/loading.py
-
curl -L -o loading.py https://huggingface.co/spaces/KeisukeMiyamoto/lambda-audio-v6/resolve/main/src/modeling/loading.py
1.47 kB
| from typing import cast | |
| import torch | |
| from pydantic import BaseModel, ConfigDict | |
| from transformers import ( | |
| AutoFeatureExtractor, | |
| AutoTokenizer, | |
| PreTrainedTokenizerBase, | |
| SequenceFeatureExtractor, | |
| ) | |
| from src.modeling.config import ModelConfig | |
| from src.modeling.lambda_audio import LambdaAudioModel | |
| from src.modeling.lfm2_decoder import AudioLfm2ForCausalLM | |
| from src.modeling.zipformer import ZipformerModel | |
| class ModelComponents(BaseModel): | |
| model_config = ConfigDict(arbitrary_types_allowed=True, frozen=True) | |
| model: LambdaAudioModel | |
| feature_extractor: SequenceFeatureExtractor | |
| tokenizer: PreTrainedTokenizerBase | |
| def load_model_components( | |
| config: ModelConfig, | |
| token: str, | |
| ) -> ModelComponents: | |
| feature_extractor = cast( | |
| SequenceFeatureExtractor, | |
| AutoFeatureExtractor.from_pretrained(config.encoder_model_id, token=token), | |
| ) | |
| tokenizer = cast( | |
| PreTrainedTokenizerBase, | |
| AutoTokenizer.from_pretrained(config.decoder_model_id, token=token), | |
| ) | |
| encoder = ZipformerModel.from_pretrained(config.encoder_model_id, token=token) | |
| decoder = AudioLfm2ForCausalLM.from_pretrained( | |
| config.decoder_model_id, | |
| attn_implementation="sdpa", | |
| dtype=torch.float32, | |
| token=token, | |
| ) | |
| model = LambdaAudioModel(encoder, decoder) | |
| return ModelComponents( | |
| model=model, | |
| feature_extractor=feature_extractor, | |
| tokenizer=tokenizer, | |
| ) | |