Skip to content
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

Merged
merged 1 commit into from
Dec 23, 2024
Merged

fix: fix bailian stt #1889

merged 1 commit into from
Dec 23, 2024

Conversation

shaohuzhang1
Copy link
Contributor

fix: fix bailian stt

Copy link

f2c-ci-robot bot commented Dec 23, 2024

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.

Copy link

f2c-ci-robot bot commented Dec 23, 2024

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

audio = audio.set_frame_rate(16000)

# 将转换后的音频文件保存到临时文件中
audio.export(temp_file_path, format='mp3')
# 识别临时文件
result = recognition.call(temp_file_path)
text = ''
Copy link
Contributor Author

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:

  1. Sample Rate Settwing: The sample_rate is being set explicitly within the function, but it's already defined when creating the Recognition object. You can either remove this line or ensure that it matches the default value used by DashScope.

  2. 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.

  3. 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.

@wxg0103 wxg0103 merged commit 53f4e11 into main Dec 23, 2024
4 checks passed
@wxg0103 wxg0103 deleted the pr@main@fix_bailian branch December 23, 2024 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants