Evan-Lin commited on
Commit
f3d0fb0
·
verified ·
1 Parent(s): be0dd6d

Add model card with Transformers and ms-swift inference examples

Browse files
Files changed (1) hide show
  1. README.md +198 -0
README.md ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: Qwen/Qwen2.5-Omni-7B
3
+ language:
4
+ - en
5
+ tags:
6
+ - audio
7
+ - audio-quality
8
+ - text-audio-alignment
9
+ - evaluation
10
+ ---
11
+
12
+ # CoTTAGE
13
+
14
+ CoTTAGE is an audio judge based on Qwen2.5-Omni-7B. It explains its judgment,
15
+ then predicts a mean opinion score (MOS) from 1.00 to 5.00.
16
+
17
+ It supports two tasks:
18
+
19
+ - **REL**: how well the audio matches a text description.
20
+ - **OVL**: overall audio quality, without a text description.
21
+
22
+ This repository contains the full model weights. No separate LoRA adapter is needed.
23
+
24
+ ## Usage with Transformers
25
+
26
+ Use Python 3.10 and a CUDA GPU. This example does not need ms-swift, vLLM,
27
+ or qwen-omni-utils.
28
+
29
+ ```bash
30
+ pip install "torch>=2.6" "transformers>=4.57,<5" accelerate librosa soundfile
31
+ ```
32
+
33
+ Replace `clip.wav` and `description` with your audio file and its text prompt.
34
+ The example scores the clip on REL and OVL separately. Audio is converted to
35
+ 16 kHz mono. Only text is generated; speech output is disabled.
36
+
37
+ ```python
38
+ import librosa
39
+ import torch
40
+ from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor
41
+
42
+ model_id = "AutoAudioReasoningMOS/CoTTAGE"
43
+ audio_path = "clip.wav"
44
+ description = "A dog barks twice, then a car drives past."
45
+
46
+ model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
47
+ model_id,
48
+ torch_dtype="auto",
49
+ device_map="auto",
50
+ enable_audio_output=False,
51
+ ).eval()
52
+ processor = Qwen2_5OmniProcessor.from_pretrained(model_id)
53
+
54
+ rel_prompt = (
55
+ "Rate the semantic alignment between this audio and the following text "
56
+ "description on a scale from 1.00 to 5.00, where 1.00 means no match and "
57
+ "5.00 means a perfect match. Think step by step, then give the final score. "
58
+ "Output the score as a number rounded to two decimal places. Put your "
59
+ "reasoning inside <think></think> and the final score inside <score></score>."
60
+ f"\n\nText: {description}\n"
61
+ )
62
+ ovl_prompt = (
63
+ "Rate the overall audio quality of this audio clip generated by a "
64
+ "text-to-audio model on a scale from 1.00 to 5.00, where 1.00 means very "
65
+ "poor quality and 5.00 means excellent quality. Think step by step, then "
66
+ "give the final score. Output the score as a number rounded to two decimal "
67
+ "places. Put your reasoning inside <think></think> and the final score "
68
+ "inside <score></score>.\n"
69
+ )
70
+
71
+ # The processor accepts a waveform directly; no temporary WAV is needed.
72
+ audio, _ = librosa.load(audio_path, sr=16000, mono=True)
73
+
74
+ for task, prompt in [("REL", rel_prompt), ("OVL", ovl_prompt)]:
75
+ messages = [{
76
+ "role": "user",
77
+ "content": [
78
+ {"type": "audio", "audio": audio_path},
79
+ {"type": "text", "text": prompt},
80
+ ],
81
+ }]
82
+ chat = processor.apply_chat_template(
83
+ messages, tokenize=False, add_generation_prompt=True,
84
+ )
85
+ inputs = processor(
86
+ text=chat,
87
+ audio=[audio],
88
+ sampling_rate=16000,
89
+ padding=True,
90
+ return_tensors="pt",
91
+ ).to(model.device).to(model.dtype)
92
+
93
+ with torch.inference_mode():
94
+ output_ids = model.generate(
95
+ **inputs,
96
+ return_audio=False,
97
+ thinker_do_sample=False,
98
+ thinker_max_new_tokens=1024,
99
+ )
100
+
101
+ # Decode only the generated answer, excluding the input prompt.
102
+ answer_ids = output_ids[:, inputs["input_ids"].shape[1]:]
103
+ text = processor.batch_decode(
104
+ answer_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False,
105
+ )[0]
106
+ print(f"{task}:\n{text}\n")
107
+ ```
108
+
109
+ The loading and generation APIs follow the
110
+ [Qwen2.5-Omni Transformers documentation](https://huggingface.co/docs/transformers/v4.57.1/en/model_doc/qwen2_5_omni).
111
+
112
+ ## Usage with ms-swift and vLLM
113
+
114
+ You can also use ms-swift's vLLM engine to speed up the inference process:
115
+
116
+ ```bash
117
+ pip install "ms-swift>=3.12" "vllm>=0.10.2" "transformers>=4.57" \
118
+ qwen-omni-utils librosa soundfile
119
+ ```
120
+
121
+ ```python
122
+ import os
123
+ import tempfile
124
+ from pathlib import Path
125
+
126
+ os.environ["ENABLE_AUDIO_OUTPUT"] = "0"
127
+
128
+ import librosa
129
+ import soundfile as sf
130
+ from swift.llm import InferRequest, RequestConfig, VllmEngine
131
+
132
+ model = "AutoAudioReasoningMOS/CoTTAGE"
133
+ audio_path = "clip.wav"
134
+ description = "A dog barks twice, then a car drives past."
135
+
136
+ rel_prompt = (
137
+ "Rate the semantic alignment between this audio and the following text "
138
+ "description on a scale from 1.00 to 5.00, where 1.00 means no match and "
139
+ "5.00 means a perfect match. Think step by step, then give the final score. "
140
+ "Output the score as a number rounded to two decimal places. Put your "
141
+ "reasoning inside <think></think> and the final score inside <score></score>."
142
+ f"\n\nText: {description}\n"
143
+ )
144
+ ovl_prompt = (
145
+ "Rate the overall audio quality of this audio clip generated by a "
146
+ "text-to-audio model on a scale from 1.00 to 5.00, where 1.00 means very "
147
+ "poor quality and 5.00 means excellent quality. Think step by step, then "
148
+ "give the final score. Output the score as a number rounded to two decimal "
149
+ "places. Put your reasoning inside <think></think> and the final score "
150
+ "inside <score></score>.\n"
151
+ )
152
+
153
+ engine = VllmEngine(model)
154
+ config = RequestConfig(max_tokens=1024, temperature=0.0)
155
+
156
+ with tempfile.TemporaryDirectory() as tmp:
157
+ wav, _ = librosa.load(audio_path, sr=16000, mono=True)
158
+ prepared_audio = str(Path(tmp) / "clip.wav")
159
+ sf.write(prepared_audio, wav, 16000)
160
+
161
+ for task, prompt in [("REL", rel_prompt), ("OVL", ovl_prompt)]:
162
+ request = InferRequest(messages=[{
163
+ "role": "user",
164
+ "content": [
165
+ {"type": "audio", "audio": prepared_audio},
166
+ {"type": "text", "text": prompt},
167
+ ],
168
+ }])
169
+ response = engine.infer([request], config)
170
+ text = response[0].choices[0].message.content
171
+ print(f"{task}:\n{text}\n")
172
+ ```
173
+
174
+ For REL, supply the text used to generate the audio. For OVL, only the audio
175
+ is used. To run just one task, keep only its entry in the loop.
176
+
177
+ ## Output
178
+
179
+ Each response contains an explanation in `<think>` tags and a score in `<score>` tags. For example:
180
+
181
+ ```text
182
+ <think>
183
+ The motor stays at a steady pitch and volume. It does not speed up or stop
184
+ as described in the text.
185
+ </think>
186
+ <score>1.80</score>
187
+ ```
188
+
189
+ Both examples store the latest response in `text`. Extract its score:
190
+
191
+ ```python
192
+ import re
193
+
194
+ match = re.search(r"<score>\s*([0-9]+(?:\.[0-9]+)?)\s*</score>", text)
195
+ score = float(match.group(1)) if match else None
196
+ ```
197
+
198
+ REL ranges from no match (1.00) to a perfect match (5.00). OVL ranges from very poor quality (1.00) to excellent quality (5.00). Run the tasks separately. A response gives a score for the requested task, not both.