Skip to content

Commit 5962341

Browse files
committed
feat: Vllm whisper model
1 parent 27aeba4 commit 5962341

File tree

7 files changed

+158
-6
lines changed

7 files changed

+158
-6
lines changed

apps/locales/en_US/LC_MESSAGES/django.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8663,4 +8663,7 @@ msgid "resource authorization"
86638663
msgstr ""
86648664

86658665
msgid "The Qwen Audio based end-to-end speech recognition model supports audio recognition within 3 minutes. At present, it mainly supports Chinese and English recognition."
8666+
msgstr ""
8667+
8668+
msgid "If not passed, the default value is 'zh'"
86668669
msgstr ""

apps/locales/zh_CN/LC_MESSAGES/django.po

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8789,4 +8789,7 @@ msgid "resource authorization"
87898789
msgstr "资源授权"
87908790

87918791
msgid "The Qwen Audio based end-to-end speech recognition model supports audio recognition within 3 minutes. At present, it mainly supports Chinese and English recognition."
8792-
msgstr "基于Qwen-Audio的端到端语音识别大模型,支持3分钟以内的音频识别,目前主要支持中英文识别。"
8792+
msgstr "基于Qwen-Audio的端到端语音识别大模型,支持3分钟以内的音频识别,目前主要支持中英文识别。"
8793+
8794+
msgid "If not passed, the default value is 'zh'"
8795+
msgstr "如果未传递,则默认值为'zh'"

apps/locales/zh_Hant/LC_MESSAGES/django.po

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8789,4 +8789,7 @@ msgid "resource authorization"
87898789
msgstr "資源授權"
87908790

87918791
msgid "The Qwen Audio based end-to-end speech recognition model supports audio recognition within 3 minutes. At present, it mainly supports Chinese and English recognition."
8792-
msgstr "基於Qwen-Audio的端到端語音辨識大模型,支持3分鐘以內的音訊識別,現時主要支持中英文識別。"
8792+
msgstr "基於Qwen-Audio的端到端語音辨識大模型,支持3分鐘以內的音訊識別,現時主要支持中英文識別。"
8793+
8794+
msgid "If not passed, the default value is 'zh'"
8795+
msgstr "如果未傳遞,則預設值為'zh'"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# coding=utf-8
2+
import traceback
3+
from typing import Dict
4+
5+
from django.utils.translation import gettext_lazy as _, gettext
6+
from langchain_core.messages import HumanMessage
7+
8+
from common import forms
9+
from common.exception.app_exception import AppApiException
10+
from common.forms import BaseForm, TooltipLabel
11+
from models_provider.base_model_provider import BaseModelCredential, ValidCode
12+
13+
14+
class VLLMWhisperModelParams(BaseForm):
15+
Language = forms.TextInputField(
16+
TooltipLabel(_('Language'),
17+
_("If not passed, the default value is 'zh'")),
18+
required=True,
19+
default_value='zh',
20+
)
21+
22+
23+
class VLLMWhisperModelCredential(BaseForm, BaseModelCredential):
24+
api_url = forms.TextInputField('API URL', required=True)
25+
api_key = forms.PasswordInputField('API Key', required=True)
26+
27+
def is_valid(self,
28+
model_type: str,
29+
model_name,
30+
model_credential: Dict[str, object],
31+
model_params,
32+
provider,
33+
raise_exception=False):
34+
35+
model_type_list = provider.get_model_type_list()
36+
37+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
38+
raise AppApiException(ValidCode.valid_error.value,
39+
gettext('{model_type} Model type is not supported').format(model_type=model_type))
40+
try:
41+
model_list = provider.get_base_model_list(model_credential.get('api_url'), model_credential.get('api_key'))
42+
except Exception as e:
43+
raise AppApiException(ValidCode.valid_error.value, gettext('API domain name is invalid'))
44+
exist = provider.get_model_info_by_name(model_list, model_name)
45+
if len(exist) == 0:
46+
raise AppApiException(ValidCode.valid_error.value,
47+
gettext('The model does not exist, please download the model first'))
48+
model = provider.get_model(model_type, model_name, model_credential, **model_params)
49+
return True
50+
51+
def encryption_dict(self, model_info: Dict[str, object]):
52+
return {**model_info, 'api_key': super().encryption(model_info.get('api_key', ''))}
53+
54+
def build_model(self, model_info: Dict[str, object]):
55+
for key in ['api_key', 'model']:
56+
if key not in model_info:
57+
raise AppApiException(500, gettext('{key} is required').format(key=key))
58+
self.api_key = model_info.get('api_key')
59+
return self
60+
61+
def get_model_params_setting_form(self, model_name):
62+
return VLLMWhisperModelParams()
Binary file not shown.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import base64
2+
import os
3+
import traceback
4+
from typing import Dict
5+
6+
from openai import OpenAI
7+
8+
from common.utils.logger import maxkb_logger
9+
from models_provider.base_model_provider import MaxKBBaseModel
10+
from models_provider.impl.base_stt import BaseSpeechToText
11+
12+
13+
14+
class VllmWhisperSpeechToText(MaxKBBaseModel, BaseSpeechToText):
15+
api_key: str
16+
api_url: str
17+
model: str
18+
params: dict
19+
20+
def __init__(self, **kwargs):
21+
super().__init__(**kwargs)
22+
self.api_key = kwargs.get('api_key')
23+
self.model = kwargs.get('model')
24+
self.params = kwargs.get('params')
25+
self.api_url = kwargs.get('api_url')
26+
27+
@staticmethod
28+
def is_cache_model():
29+
return False
30+
31+
@staticmethod
32+
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
33+
return VllmWhisperSpeechToText(
34+
model=model_name,
35+
api_key=model_credential.get('api_key'),
36+
api_url=model_credential.get('api_url'),
37+
params=model_kwargs,
38+
**model_kwargs
39+
)
40+
41+
def check_auth(self):
42+
cwd = os.path.dirname(os.path.abspath(__file__))
43+
with open(f'{cwd}/iat_mp3_16k.mp3', 'rb') as audio_file:
44+
self.speech_to_text(audio_file)
45+
46+
def speech_to_text(self, audio_file):
47+
base_url = f"{self.api_url}/v1"
48+
try:
49+
client = OpenAI(
50+
api_key=self.api_key,
51+
base_url=base_url
52+
)
53+
54+
result = client.audio.transcriptions.create(
55+
file=audio_file,
56+
model=self.model,
57+
language=self.params.get('Language'),
58+
response_format="json"
59+
)
60+
61+
return result.text
62+
63+
except Exception as err:
64+
maxkb_logger.error(f":Error: {str(err)}: {traceback.format_exc()}")

apps/models_provider/impl/vllm_model_provider/vllm_model_provider.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@
1010
from models_provider.impl.vllm_model_provider.credential.embedding import VllmEmbeddingCredential
1111
from models_provider.impl.vllm_model_provider.credential.image import VllmImageModelCredential
1212
from models_provider.impl.vllm_model_provider.credential.llm import VLLMModelCredential
13+
from models_provider.impl.vllm_model_provider.credential.whisper_stt import VLLMWhisperModelCredential
1314
from models_provider.impl.vllm_model_provider.model.embedding import VllmEmbeddingModel
1415
from models_provider.impl.vllm_model_provider.model.image import VllmImage
1516
from models_provider.impl.vllm_model_provider.model.llm import VllmChatModel
1617
from maxkb.conf import PROJECT_DIR
1718
from django.utils.translation import gettext as _
1819

20+
from models_provider.impl.vllm_model_provider.model.whisper_sst import VllmWhisperSpeechToText
21+
1922
v_llm_model_credential = VLLMModelCredential()
2023
image_model_credential = VllmImageModelCredential()
2124
embedding_model_credential = VllmEmbeddingCredential()
25+
whisper_model_credential = VLLMWhisperModelCredential()
2226

2327
model_info_list = [
24-
ModelInfo('facebook/opt-125m', _('Facebook’s 125M parameter model'), ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel),
25-
ModelInfo('BAAI/Aquila-7B', _('BAAI’s 7B parameter model'), ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel),
26-
ModelInfo('BAAI/AquilaChat-7B', _('BAAI’s 13B parameter mode'), ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel),
28+
ModelInfo('facebook/opt-125m', _('Facebook’s 125M parameter model'), ModelTypeConst.LLM, v_llm_model_credential,
29+
VllmChatModel),
30+
ModelInfo('BAAI/Aquila-7B', _('BAAI’s 7B parameter model'), ModelTypeConst.LLM, v_llm_model_credential,
31+
VllmChatModel),
32+
ModelInfo('BAAI/AquilaChat-7B', _('BAAI’s 13B parameter mode'), ModelTypeConst.LLM, v_llm_model_credential,
33+
VllmChatModel),
2734

2835
]
2936

@@ -32,7 +39,15 @@
3239
]
3340

3441
embedding_model_info_list = [
35-
ModelInfo('HIT-TMG/KaLM-embedding-multilingual-mini-instruct-v1.5', '', ModelTypeConst.EMBEDDING, embedding_model_credential, VllmEmbeddingModel),
42+
ModelInfo('HIT-TMG/KaLM-embedding-multilingual-mini-instruct-v1.5', '', ModelTypeConst.EMBEDDING,
43+
embedding_model_credential, VllmEmbeddingModel),
44+
]
45+
46+
whisper_model_info_list = [
47+
ModelInfo('whisper-tiny', '', ModelTypeConst.STT, whisper_model_credential, VllmWhisperSpeechToText),
48+
ModelInfo('whisper-large-v3-turbo', '', ModelTypeConst.STT, whisper_model_credential, VllmWhisperSpeechToText),
49+
ModelInfo('whisper-small', '', ModelTypeConst.STT, whisper_model_credential, VllmWhisperSpeechToText),
50+
ModelInfo('whisper-large-v3', '', ModelTypeConst.STT, whisper_model_credential, VllmWhisperSpeechToText),
3651
]
3752

3853
model_info_manage = (
@@ -45,6 +60,8 @@
4560
.append_default_model_info(image_model_info_list[0])
4661
.append_model_info_list(embedding_model_info_list)
4762
.append_default_model_info(embedding_model_info_list[0])
63+
.append_model_info_list(whisper_model_info_list)
64+
.append_default_model_info(whisper_model_info_list[0])
4865
.build()
4966
)
5067

0 commit comments

Comments
 (0)