Views
No views yet
diffusers library, ComfyUI, or any other model), although compressing models that are of architectures that are unfamiliar to me might be more difficult.transformers1import soundfile as sf
2from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor
3from qwen_omni_utils import process_mm_info
4
5# Highly recommended to enable flash_attention_2 for better acceleration and memory saving.
6model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
7 "Qwen/Qwen2.5-Omni-7B",
8 attn_implementation="flash_attention_2",
9 dtype="auto",
10 device_map="cpu"
11)
12
13DFloat11Model.from_pretrained("mingyi456/Qwen2.5-Omni-7B-DF11", device = "cpu", bfloat16_model = model)
14model.to("cuda")
15
16# IMPORTANT: If you want to disable the talker module, do it here, only after calling `model.to("cuda")`
17# model.disable_talker()
18
19processor = Qwen2_5OmniProcessor.from_pretrained("Qwen/Qwen2.5-Omni-7B")
20conversation = [
21 {
22 "role": "system",
23 "content": [
24 {"type": "text", "text": "You are Qwen, a virtual human developed by the Qwen Team, Alibaba Group, capable of perceiving auditory and visual inputs, as well as generating text and speech."}
25 ],
26 },
27 {
28 "role": "user",
29 "content": [
30 {"type": "video", "video": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-Omni/draw.mp4"},
31 ],
32 },
33]
34# set use audio in video
35USE_AUDIO_IN_VIDEO = True
36# Preparation for inference
37text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
38audios, images, videos = process_mm_info(conversation, use_audio_in_video=USE_AUDIO_IN_VIDEO)
39inputs = processor(text=text, audio=audios, images=images, videos=videos, return_tensors="pt", padding=True, use_audio_in_video=USE_AUDIO_IN_VIDEO)
40inputs = inputs.to(model.device).to(model.dtype)
41# Inference: Generation of the output text and audio
42text_ids, audio = model.generate(**inputs, use_audio_in_video=USE_AUDIO_IN_VIDEO)
43text = processor.batch_decode(text_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
44print(text)
45sf.write(
46 "output.wav",
47 audio.reshape(-1).detach().cpu().numpy(),
48 samplerate=24000,
49)