Views
No views yet
Qwen3-ASR is now supported in llama-cpp-python. This project provides a test GGUF file.llama-cpp-python: https://github.com/JamePeng/llama-cpp-python1from llama_cpp import Llama
2from llama_cpp.llama_chat_format import Qwen3ASRChatHandler
3import base64
4import os
5
6# Model and multimodal projection paths
7MODEL_PATH = r"./Qwen3-ASR-1.7B-BF16.gguf"
8# BF16 mmproj is required for audio. Other quantizations are known to have degraded performance.
9MMPROJ_PATH = r"./mmproj-Qwen3-ASR-1.7b-BF16.gguf"
10
11# Initialize the Llama model with multimodal (audio) support
12llm = Llama(
13 model_path=MODEL_PATH,
14 chat_handler=Qwen3ASRChatHandler(
15 clip_model_path=MMPROJ_PATH,
16 verbose=False,
17 ),
18 n_gpu_layers=-1,
19 n_ctx=10240,
20 verbose=False,
21 verbosity=0
22)
23
24# 1. MIME dictionary, audio format support
25_MEDIA_MIME_TYPES = {
26 # ------ Audio Format ------
27 '.wav': ('audio', 'wav'), # OpenAI standard usually uses raw format names for audio
28 '.mp3': ('audio', 'mp3'),
29 # '.flac': ('audio', 'flac'),
30}
31
32def build_media_payload(file_path: str) -> dict:
33 """
34 Read local media files (audio) and convert them into an LLM-approved input structure.
35 """
36 if not os.path.isfile(file_path):
37 raise FileNotFoundError(f"Media file not found: {file_path}")
38
39 extension = os.path.splitext(file_path)[1].lower()
40 media_category, mime_or_format = _MEDIA_MIME_TYPES.get(extension, ('unknown', 'application/octet-stream'))
41
42 if media_category == 'unknown':
43 print(f"Warning: Unknown extension '{extension}'. It might not be processed correctly.")
44
45 # Reading the Base64 encoding of a file
46 with open(file_path, "rb") as f:
47 encoded_data = base64.b64encode(f.read()).decode("utf-8")
48
49 # 2. Return audio dictionary structures based on the media type.
50 if media_category == 'audio':
51 # Audio format: input_audio (OpenAI compatibility mode)
52 return {
53 "type": "input_audio",
54 "input_audio": {
55 "data": encoded_data,
56 "format": mime_or_format
57 }
58 }
59 else:
60 # Fallback
61 return {"type": "text", "text": f"[Attached unsupported file: {file_path}]"}
62
63
64# ========================
65# Main inference section
66# ========================
67
68# 3. Audio file path
69media_paths = [
70 r"./audio/test.wav", # audio
71]
72
73# 4. build user_content list
74user_content = []
75
76for path in media_paths:
77 payload = build_media_payload(path)
78 user_content.append(payload)
79
80# 5. eval
81response = llm.create_chat_completion(
82 messages=[
83 {"role": "system", "content":
84 """
85 You are an advanced multilingual Speech-to-Text model. Accurately transcribe the audio into text in its original spoken language.
86 You should ignore background noise, filler words, and stutters where possible, and format the final output with correct grammar and capitalization.
87 """
88 },
89 {"role": "user", "content": user_content}
90 ],
91 temperature=1.0,
92 top_p=0.95,
93 top_k=64,
94 max_tokens=10240,
95)
96
97print(f"Transcribe: {response["choices"][0]["message"]["content"]}")