-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: fix bailian stt #1889
fix: fix bailian stt #1889
Conversation
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
audio = audio.set_frame_rate(16000) | ||
|
||
# 将转换后的音频文件保存到临时文件中 | ||
audio.export(temp_file_path, format='mp3') | ||
# 识别临时文件 | ||
result = recognition.call(temp_file_path) | ||
text = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of issues and suggestions to address:
-
Sample Rate Settwing: The
sample_rate
is being set explicitly within the function, but it's already defined when creating theRecognition
object. You can either remove this line or ensure that it matches the default value used by DashScope. -
Channel Conversion: Setting channels and frame rate programmatically can sometimes lead to unexpected behavior. It's better to use predefined methods provided by PyDub or DashScope itself where applicable, especially if you need consistent handling across platforms.
-
Callback Handling: Since the callback parameter is not utilized in your code snippet, consider whether this could be removed for simplicity.
Here's an updated version with these adjustments:
import dashscope
from dashscope.audio.asr import (Recognition)
from setting.models_provider.base_model_provider import MaxKBBaseModel
from setting.models_provider.impl.base_stt import BaseSpeechToText
class YourSTTClass(BaseSpeechToText):
def __init__(self, api_key, model):
super().__init__()
dashboard_scope.api_key = self.api_key
self.model = model
@staticmethod
async def convert_audio_format(file_path: str) -> None:
"""Converts the audio file format to MP3."""
try:
audio_segment = AudioSegment.from_file(file_path)
# Ensure single channel and 16kHz sampling rate
audio_segment = audio_segment.set_channels(1).set_frame_rate(16000)
audio_segment.export(file_path, newformat="mp3")
except Exception as e:
print(f"Error converting audio: {e}")
async def recognize(self, audio_data: bytes | str) -> list[dict]:
"""Recognizes speech from binary data."""
recognition = Recognition(
model=self.model,
format='mp3'
)
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(audio_data)
temp_file.close()
temp_file_path = temp_file.name
await self.convert_audio_format(temp_file_path)
# Call the API for recognition
result = await recognition.call_async(temp_file_path)
return [line.data['result'] for line in result]
Key Changes:
- Removed the redundant
sample_rate
assignment. - Added a method
convert_audio_format
using PyDub for cleaning up audio samples. - Used asynchronous calls where appropriate, such as
recognition.call_async()
which is more suitable for real-time applications.
These changes should improve robustness and consistency in audio processing.
fix: fix bailian stt