Phospheneser commited on
Commit
484c8fb
·
verified ·
1 Parent(s): 8fa91d0

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +116 -0
README.md ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model:
3
+ - Qwen/Qwen3-8B
4
+ pipeline_tag: feature-extraction
5
+ ---
6
+
7
+ # MOSS-Speech ☄️
8
+
9
+ ## Overview
10
+
11
+ MOSS-Speech is an open-source bilingual native speech-to-speech model Without text guidance that supports both Chinese and English.
12
+ Our approach combines a **modality-based layer-splitting architecture** with a **frozen pre-training strategy**, leveraging pretrained text LLMs while extending native speech capabilities. Experiments show state-of-the-art results in spoken question answering and competitive speech-to-speech performance compared to text-guided systems.
13
+
14
+ ## Highlights
15
+
16
+ - **True Speech-to-Speech Modeling**: No text guidance required.
17
+ - **Layer-Splitting Architecture**: Integrates modality-specific layers on top of pretrained text LLM backbones.
18
+ - **Frozen Pre-Training Strategy**: Preserves LLM reasoning while enhancing speech understanding and generation.
19
+ - **State-of-the-Art Performance**: Excels in spoken question answering and speech-to-speech tasks.
20
+ - **Expressive & Efficient**: Maintains paralinguistic cues often lost in cascaded pipelines, such as tone, emotion, and prosody.
21
+
22
+
23
+ ```python
24
+ """MossSpeech inference demo aligned with Hugging Face Transformers guidelines."""
25
+ import os
26
+ from dataclasses import astuple
27
+
28
+ import torch
29
+ import torchaudio
30
+
31
+ from transformers import (
32
+ AutoModel,
33
+ AutoProcessor,
34
+ GenerationConfig,
35
+ StoppingCriteria,
36
+ StoppingCriteriaList,
37
+ )
38
+
39
+
40
+ prompt = "Hello!"
41
+ prompt_audio = "<your path to prompt>
42
+ model_path = "fnlp/MOSS-Speech"
43
+ codec_path = "fnlp/MOSS-Speech-Codec"
44
+ output_path = "outputs"
45
+ output_modality = "audio" # or text
46
+
47
+ generation_config = GenerationConfig(
48
+ temperature=0.7,
49
+ top_p=0.95,
50
+ top_k=20,
51
+ repetition_penalty=1.0,
52
+ max_new_tokens=1000,
53
+ min_new_tokens=10,
54
+ do_sample=True,
55
+ use_cache=True,
56
+ )
57
+
58
+
59
+ class StopOnToken(StoppingCriteria):
60
+ """Stop generation once the final token equals the provided stop ID."""
61
+
62
+ def __init__(self, stop_id: int) -> None:
63
+ super().__init__()
64
+ self.stop_id = stop_id
65
+
66
+ def __call__(self, input_ids: torch.LongTensor, scores) -> bool: # type: ignore[override]
67
+ return input_ids[0, -1].item() == self.stop_id
68
+
69
+
70
+ def prepare_stopping_criteria(processor):
71
+ tokenizer = processor.tokenizer
72
+ stop_tokens = [
73
+ tokenizer.pad_token_id,
74
+ tokenizer.convert_tokens_to_ids("<|im_end|>"),
75
+ ]
76
+ return StoppingCriteriaList([StopOnToken(token_id) for token_id in stop_tokens])
77
+
78
+
79
+ messages = [
80
+ [
81
+ {
82
+ "role": "system",
83
+ "content": "You are a helpful voice assistant. Answer the user's questions with spoken responses."},
84
+ # "content": "You are a helpful assistant. Answer the user's questions with text."}, # if output_modality = "text"
85
+ {
86
+ "role": "user",
87
+ "content": prompt
88
+ }
89
+ ]
90
+ ]
91
+
92
+
93
+ processor = AutoProcessor.from_pretrained(model_path, codec_path=codec_path, device="cuda", trust_remote_code=True)
94
+ stopping_criteria = prepare_stopping_criteria(processor)
95
+ encoded_inputs = processor(messages, output_modality)
96
+
97
+ model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map="cuda").eval()
98
+
99
+ with torch.inference_mode():
100
+ token_ids = model.generate(
101
+ input_ids=encoded_inputs["input_ids"].to("cuda"),
102
+ attention_mask=encoded_inputs["attention_mask"].to("cuda"),
103
+ generation_config=generation_config,
104
+ stopping_criteria=stopping_criteria,
105
+ )
106
+
107
+ results = processor.decode(token_ids, output_modality, decoder_audio_prompt_path=prompt_audio)
108
+
109
+ os.makedirs(output_path, exist_ok=True)
110
+ for index, (result, modality) in enumerate(zip(results, output_modality)):
111
+ audio, text, sample_rate = astuple(result)
112
+ if modality == "audio":
113
+ torchaudio.save(f"{output_path}/audio_{index}.wav", audio, sample_rate)
114
+ else:
115
+ print(text)
116
+ ```