示例#1
0
def transcribe(file_name: str,
               message: Message,
               lang_code: str = 'ru-RU',
               alternatives: List[str] = ['en-US', 'uk-UA']) -> List[str]:
    media_info = MediaInfo.parse(file_name)
    if len(media_info.audio_tracks) != 1 or not hasattr(
            media_info.audio_tracks[0], 'sampling_rate'):
        os.remove(file_name)
        raise ValueError('Failed to detect sample rate')
    actual_duration = round(media_info.audio_tracks[0].duration / 1000)

    sample_rate = media_info.audio_tracks[0].sampling_rate
    encoding = RecognitionConfig.AudioEncoding.OGG_OPUS
    if sample_rate not in SUPPORTED_SAMPLE_RATES:
        message.reply_text(
            'Your voice message has a sample rate of {} Hz which is not in the list '
            'of supported sample rates ({}).\n\nI will try to resample it, '
            'but this may reduce recognition accuracy'.format(
                sample_rate, ', '.join(
                    str(int(rate / 1000)) + ' kHz'
                    for rate in SUPPORTED_SAMPLE_RATES)),
            quote=True)
        message.reply_chat_action(action=ChatAction.TYPING)
        encoding, file_name, sample_rate = resample(file_name)
    config = RecognitionConfig(
        encoding=encoding,
        sample_rate_hertz=sample_rate,
        enable_automatic_punctuation=True,
        language_code=lang_code,
        alternative_language_codes=alternatives,
    )

    try:
        response = upload_to_gs(file_name, config) \
            if actual_duration > UPLOAD_LIMIT \
            else regular_upload(file_name, config)
    except Exception as e:
        print(e)
        os.remove(file_name)
        return ['Failed']

    with conn.cursor() as cur:
        cur.execute(
            "insert into customer(user_id) values (%s) on conflict (user_id) do nothing;",
            (message.chat_id, ))
        cur.execute(
            "update customer set balance = balance - (%s) where user_id = (%s);",
            (actual_duration, message.chat_id))
        cur.execute(
            "insert into stat(user_id, message_timestamp, duration) values (%s, current_timestamp, %s);",
            (message.chat_id, actual_duration))
        conn.commit()

    os.remove(file_name)

    message_text = ''
    for result in response.results:
        message_text += result.alternatives[0].transcript + '\n'

    return split_long_message(message_text)
示例#2
0
def download_and_prep(file_name: str, message: Message) -> None:
    message.voice.get_file().download(file_name)
    message.reply_chat_action(action=ChatAction.TYPING)