Views
No views yet


vllm (recommended): See hereTransformers 🤗: See heretemperature=0.2 and top_p=0.95 for chat completion (e.g. Audio Understanding) and temperature=0.0 for transcriptionuv:uv pip install -U "vllm[audio]" --systemmistral_common >= 1.8.1.python -c "import mistral_common; print(mistral_common.__version__)"git clone https://github.com/vllm-project/vllm && cd vllmpython examples/offline_inference/audio_language.py --num-audios 2 --model-type voxtralvllm serve mistralai/Voxtral-Mini-3B-2507 --tokenizer_mode mistral --config_format mistral --load_format mistralmistral-common with audio installed:pip install --upgrade mistral_common\[audio\]1from mistral_common.protocol.instruct.messages import TextChunk, AudioChunk, UserMessage, AssistantMessage, RawAudio
2from mistral_common.audio import Audio
3from huggingface_hub import hf_hub_download
4
5from openai import OpenAI
6
7# Modify OpenAI's API key and API base to use vLLM's API server.
8openai_api_key = "EMPTY"
9openai_api_base = "http://<your-server-host>:8000/v1"
10
11client = OpenAI(
12 api_key=openai_api_key,
13 base_url=openai_api_base,
14)
15
16models = client.models.list()
17model = models.data[0].id
18
19obama_file = hf_hub_download("patrickvonplaten/audio_samples", "obama.mp3", repo_type="dataset")
20bcn_file = hf_hub_download("patrickvonplaten/audio_samples", "bcn_weather.mp3", repo_type="dataset")
21
22def file_to_chunk(file: str) -> AudioChunk:
23 audio = Audio.from_file(file, strict=False)
24 return AudioChunk.from_audio(audio)
25
26text_chunk = TextChunk(text="Which speaker is more inspiring? Why? How are they different from each other?")
27user_msg = UserMessage(content=[file_to_chunk(obama_file), file_to_chunk(bcn_file), text_chunk]).to_openai()
28
29print(30 * "=" + "USER 1" + 30 * "=")
30print(text_chunk.text)
31print("\n\n")
32
33response = client.chat.completions.create(
34 model=model,
35 messages=[user_msg],
36 temperature=0.2,
37 top_p=0.95,
38)
39content = response.choices[0].message.content
40
41print(30 * "=" + "BOT 1" + 30 * "=")
42print(content)
43print("\n\n")
44# The speaker who is more inspiring is the one who delivered the farewell address, as they express
45# gratitude, optimism, and a strong commitment to the nation and its citizens. They emphasize the importance of
46# self-government and active citizenship, encouraging everyone to participate in the democratic process. In contrast,
47# the other speaker provides a factual update on the weather in Barcelona, which is less inspiring as it
48# lacks the emotional and motivational content of the farewell address.
49
50# **Differences:**
51# - The farewell address speaker focuses on the values and responsibilities of citizenship, encouraging active participation in democracy.
52# - The weather update speaker provides factual information about the temperature in Barcelona, without any emotional or motivational content.
53
54
55messages = [
56 user_msg,
57 AssistantMessage(content=content).to_openai(),
58 UserMessage(content="Ok, now please summarize the content of the first audio.").to_openai()
59]
60print(30 * "=" + "USER 2" + 30 * "=")
61print(messages[-1]["content"])
62print("\n\n")
63
64response = client.chat.completions.create(
65 model=model,
66 messages=messages,
67 temperature=0.2,
68 top_p=0.95,
69)
70content = response.choices[0].message.content
71print(30 * "=" + "BOT 2" + 30 * "=")
72print(content)mistral-common with audio installed:pip install --upgrade mistral_common\[audio\]1from mistral_common.protocol.transcription.request import TranscriptionRequest
2from mistral_common.protocol.instruct.messages import RawAudio
3from mistral_common.audio import Audio
4from huggingface_hub import hf_hub_download
5
6from openai import OpenAI
7
8# Modify OpenAI's API key and API base to use vLLM's API server.
9openai_api_key = "EMPTY"
10openai_api_base = "http://<your-server-host>:8000/v1"
11
12client = OpenAI(
13 api_key=openai_api_key,
14 base_url=openai_api_base,
15)
16
17models = client.models.list()
18model = models.data[0].id
19
20obama_file = hf_hub_download("patrickvonplaten/audio_samples", "obama.mp3", repo_type="dataset")
21audio = Audio.from_file(obama_file, strict=False)
22
23audio = RawAudio.from_audio(audio)
24req = TranscriptionRequest(model=model, audio=audio, language="en", temperature=0.0).to_openai(exclude=("top_p", "seed"))
25
26response = client.audio.transcriptions.create(**req)
27print(response)transformers >= 4.54.0 and above, you can run Voxtral natively!pip install -U transformersmistral-common >= 1.8.1 installed with audio dependencies:pip install --upgrade "mistral-common[audio]"1from transformers import VoxtralForConditionalGeneration, AutoProcessor
2import torch
3
4device = "cuda"
5repo_id = "mistralai/Voxtral-Mini-3B-2507"
6
7processor = AutoProcessor.from_pretrained(repo_id)
8model = VoxtralForConditionalGeneration.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map=device)
9
10conversation = [
11 {
12 "role": "user",
13 "content": [
14 {
15 "type": "audio",
16 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/mary_had_lamb.mp3",
17 },
18 {
19 "type": "audio",
20 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/winning_call.mp3",
21 },
22 {"type": "text", "text": "What sport and what nursery rhyme are referenced?"},
23 ],
24 }
25]
26
27inputs = processor.apply_chat_template(conversation)
28inputs = inputs.to(device, dtype=torch.bfloat16)
29
30outputs = model.generate(**inputs, max_new_tokens=500)
31decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
32
33print("\nGenerated response:")
34print("=" * 80)
35print(decoded_outputs[0])
36print("=" * 80)1from transformers import VoxtralForConditionalGeneration, AutoProcessor
2import torch
3
4device = "cuda"
5repo_id = "mistralai/Voxtral-Mini-3B-2507"
6
7processor = AutoProcessor.from_pretrained(repo_id)
8model = VoxtralForConditionalGeneration.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map=device)
9
10conversation = [
11 {
12 "role": "user",
13 "content": [
14 {
15 "type": "audio",
16 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama.mp3",
17 },
18 {
19 "type": "audio",
20 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3",
21 },
22 {"type": "text", "text": "Describe briefly what you can hear."},
23 ],
24 },
25 {
26 "role": "assistant",
27 "content": "The audio begins with the speaker delivering a farewell address in Chicago, reflecting on his eight years as president and expressing gratitude to the American people. The audio then transitions to a weather report, stating that it was 35 degrees in Barcelona the previous day, but the temperature would drop to minus 20 degrees the following day.",
28 },
29 {
30 "role": "user",
31 "content": [
32 {
33 "type": "audio",
34 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/winning_call.mp3",
35 },
36 {"type": "text", "text": "Ok, now compare this new audio with the previous one."},
37 ],
38 },
39]
40
41inputs = processor.apply_chat_template(conversation)
42inputs = inputs.to(device, dtype=torch.bfloat16)
43
44outputs = model.generate(**inputs, max_new_tokens=500)
45decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
46
47print("\nGenerated response:")
48print("=" * 80)
49print(decoded_outputs[0])
50print("=" * 80)1from transformers import VoxtralForConditionalGeneration, AutoProcessor
2import torch
3
4device = "cuda"
5repo_id = "mistralai/Voxtral-Mini-3B-2507"
6
7processor = AutoProcessor.from_pretrained(repo_id)
8model = VoxtralForConditionalGeneration.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map=device)
9
10conversation = [
11 {
12 "role": "user",
13 "content": [
14 {
15 "type": "text",
16 "text": "Why should AI models be open-sourced?",
17 },
18 ],
19 }
20]
21
22inputs = processor.apply_chat_template(conversation)
23inputs = inputs.to(device, dtype=torch.bfloat16)
24
25outputs = model.generate(**inputs, max_new_tokens=500)
26decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
27
28print("\nGenerated response:")
29print("=" * 80)
30print(decoded_outputs[0])
31print("=" * 80)1from transformers import VoxtralForConditionalGeneration, AutoProcessor
2import torch
3
4device = "cuda"
5repo_id = "mistralai/Voxtral-Mini-3B-2507"
6
7processor = AutoProcessor.from_pretrained(repo_id)
8model = VoxtralForConditionalGeneration.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map=device)
9
10conversation = [
11 {
12 "role": "user",
13 "content": [
14 {
15 "type": "audio",
16 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/winning_call.mp3",
17 },
18 ],
19 }
20]
21
22inputs = processor.apply_chat_template(conversation)
23inputs = inputs.to(device, dtype=torch.bfloat16)
24
25outputs = model.generate(**inputs, max_new_tokens=500)
26decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
27
28print("\nGenerated response:")
29print("=" * 80)
30print(decoded_outputs[0])
31print("=" * 80)1from transformers import VoxtralForConditionalGeneration, AutoProcessor
2import torch
3
4device = "cuda"
5repo_id = "mistralai/Voxtral-Mini-3B-2507"
6
7processor = AutoProcessor.from_pretrained(repo_id)
8model = VoxtralForConditionalGeneration.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map=device)
9
10conversations = [
11 [
12 {
13 "role": "user",
14 "content": [
15 {
16 "type": "audio",
17 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama.mp3",
18 },
19 {
20 "type": "audio",
21 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3",
22 },
23 {
24 "type": "text",
25 "text": "Who's speaking in the speach and what city's weather is being discussed?",
26 },
27 ],
28 }
29 ],
30 [
31 {
32 "role": "user",
33 "content": [
34 {
35 "type": "audio",
36 "path": "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/winning_call.mp3",
37 },
38 {"type": "text", "text": "What can you tell me about this audio?"},
39 ],
40 }
41 ],
42]
43
44inputs = processor.apply_chat_template(conversations)
45inputs = inputs.to(device, dtype=torch.bfloat16)
46
47outputs = model.generate(**inputs, max_new_tokens=500)
48decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
49
50print("\nGenerated responses:")
51print("=" * 80)
52for decoded_output in decoded_outputs:
53 print(decoded_output)
54 print("=" * 80)1from transformers import VoxtralForConditionalGeneration, AutoProcessor
2import torch
3
4device = "cuda"
5repo_id = "mistralai/Voxtral-Mini-3B-2507"
6
7processor = AutoProcessor.from_pretrained(repo_id)
8model = VoxtralForConditionalGeneration.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map=device)
9
10inputs = processor.apply_transcription_request(language="en", audio="https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama.mp3", model_id=repo_id)
11inputs = inputs.to(device, dtype=torch.bfloat16)
12
13outputs = model.generate(**inputs, max_new_tokens=500)
14decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
15
16print("\nGenerated responses:")
17print("=" * 80)
18for decoded_output in decoded_outputs:
19 print(decoded_output)
20 print("=" * 80)