Views
No views yet
vllm serve mlinmg/Qwen-2-Audio-Instruct-dynamic-fp8pip install git+https://github.com/huggingface/transformers, or you might encounter the following error:KeyError: 'qwen2-audio'Qwen2-Audio-7B-Instruct for the inference, supporting both voice chat and audio analysis modes. Note that we have used the ChatML format for dialog, in this demo we show how to leverage apply_chat_template for this purpose.1from io import BytesIO
2from urllib.request import urlopen
3import librosa
4from transformers import Qwen2AudioForConditionalGeneration, AutoProcessor
5
6processor = AutoProcessor.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct")
7model = Qwen2AudioForConditionalGeneration.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct", device_map="auto")
8
9conversation = [
10 {"role": "user", "content": [
11 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/guess_age_gender.wav"},
12 ]},
13 {"role": "assistant", "content": "Yes, the speaker is female and in her twenties."},
14 {"role": "user", "content": [
15 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/translate_to_chinese.wav"},
16 ]},
17]
18text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
19audios = []
20for message in conversation:
21 if isinstance(message["content"], list):
22 for ele in message["content"]:
23 if ele["type"] == "audio":
24 audios.append(librosa.load(
25 BytesIO(urlopen(ele['audio_url']).read()),
26 sr=processor.feature_extractor.sampling_rate)[0]
27 )
28
29inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True)
30inputs.input_ids = inputs.input_ids.to("cuda")
31
32generate_ids = model.generate(**inputs, max_length=256)
33generate_ids = generate_ids[:, inputs.input_ids.size(1):]
34
35response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]1from io import BytesIO
2from urllib.request import urlopen
3import librosa
4from transformers import Qwen2AudioForConditionalGeneration, AutoProcessor
5
6processor = AutoProcessor.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct")
7model = Qwen2AudioForConditionalGeneration.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct", device_map="auto")
8
9conversation = [
10 {'role': 'system', 'content': 'You are a helpful assistant.'},
11 {"role": "user", "content": [
12 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/glass-breaking-151256.mp3"},
13 {"type": "text", "text": "What's that sound?"},
14 ]},
15 {"role": "assistant", "content": "It is the sound of glass shattering."},
16 {"role": "user", "content": [
17 {"type": "text", "text": "What can you do when you hear that?"},
18 ]},
19 {"role": "assistant", "content": "Stay alert and cautious, and check if anyone is hurt or if there is any damage to property."},
20 {"role": "user", "content": [
21 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/1272-128104-0000.flac"},
22 {"type": "text", "text": "What does the person say?"},
23 ]},
24]
25text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
26audios = []
27for message in conversation:
28 if isinstance(message["content"], list):
29 for ele in message["content"]:
30 if ele["type"] == "audio":
31 audios.append(
32 librosa.load(
33 BytesIO(urlopen(ele['audio_url']).read()),
34 sr=processor.feature_extractor.sampling_rate)[0]
35 )
36
37inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True)
38inputs.input_ids = inputs.input_ids.to("cuda")
39
40generate_ids = model.generate(**inputs, max_length=256)
41generate_ids = generate_ids[:, inputs.input_ids.size(1):]
42
43response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]1from io import BytesIO
2from urllib.request import urlopen
3import librosa
4from transformers import Qwen2AudioForConditionalGeneration, AutoProcessor
5
6processor = AutoProcessor.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct")
7model = Qwen2AudioForConditionalGeneration.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct", device_map="auto")
8
9conversation1 = [
10 {"role": "user", "content": [
11 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/glass-breaking-151256.mp3"},
12 {"type": "text", "text": "What's that sound?"},
13 ]},
14 {"role": "assistant", "content": "It is the sound of glass shattering."},
15 {"role": "user", "content": [
16 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/f2641_0_throatclearing.wav"},
17 {"type": "text", "text": "What can you hear?"},
18 ]}
19]
20
21conversation2 = [
22 {"role": "user", "content": [
23 {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/1272-128104-0000.flac"},
24 {"type": "text", "text": "What does the person say?"},
25 ]},
26]
27
28conversations = [conversation1, conversation2]
29
30text = [processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False) for conversation in conversations]
31
32audios = []
33for conversation in conversations:
34 for message in conversation:
35 if isinstance(message["content"], list):
36 for ele in message["content"]:
37 if ele["type"] == "audio":
38 audios.append(
39 librosa.load(
40 BytesIO(urlopen(ele['audio_url']).read()),
41 sr=processor.feature_extractor.sampling_rate)[0]
42 )
43
44inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True)
45inputs['input_ids'] = inputs['input_ids'].to("cuda")
46inputs.input_ids = inputs.input_ids.to("cuda")
47
48generate_ids = model.generate(**inputs, max_length=256)
49generate_ids = generate_ids[:, inputs.input_ids.size(1):]
50
51response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)1@article{Qwen2-Audio,
2 title={Qwen2-Audio Technical Report},
3 author={Chu, Yunfei and Xu, Jin and Yang, Qian and Wei, Haojie and Wei, Xipin and Guo, Zhifang and Leng, Yichong and Lv, Yuanjun and He, Jinzheng and Lin, Junyang and Zhou, Chang and Zhou, Jingren},
4 journal={arXiv preprint arXiv:2407.10759},
5 year={2024}
6}1@article{Qwen-Audio,
2 title={Qwen-Audio: Advancing Universal Audio Understanding via Unified Large-Scale Audio-Language Models},
3 author={Chu, Yunfei and Xu, Jin and Zhou, Xiaohuan and Yang, Qian and Zhang, Shiliang and Yan, Zhijie and Zhou, Chang and Zhou, Jingren},
4 journal={arXiv preprint arXiv:2311.07919},
5 year={2023}
6}