Files
PsAgentAI/venv/lib/python3.11/site-packages/speech_recognition/recognizers/whisper.py
T
marcocosta10000 25288d2c0f feat(voice): transcribe Telegram voice notes with SpeechRecognition
Add a voice message handler that downloads OGG audio, converts it to
WAV via pydub/ffmpeg, and transcribes it with Google Speech Recognition
(pt-BR) before feeding the text into the normal message pipeline.

Also extract shared text processing into process_text_message, guard
against updates without a text attribute, switch the default AI provider
to openrouter with deepseek-v4-flash, and document the ffmpeg system
requirement in DEPLOY.md.
2026-10-25 17:27:42 -03:00

43 lines
1.5 KiB
Python

from __future__ import annotations
import os
from io import BytesIO
from speech_recognition.audio import AudioData
from speech_recognition.exceptions import SetupError
def recognize_whisper_api(
recognizer,
audio_data: "AudioData",
*,
model: str = "whisper-1",
api_key: str | None = None,
):
"""
Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the OpenAI Whisper API.
This function requires an OpenAI account; visit https://platform.openai.com/signup, then generate API Key in `User settings <https://platform.openai.com/account/api-keys>`__.
Detail: https://platform.openai.com/docs/guides/speech-to-text
Raises a ``speech_recognition.exceptions.SetupError`` exception if there are any issues with the openai installation, or the environment variable is missing.
"""
if not isinstance(audio_data, AudioData):
raise ValueError("``audio_data`` must be an ``AudioData`` instance")
if api_key is None and os.environ.get("OPENAI_API_KEY") is None:
raise SetupError("Set environment variable ``OPENAI_API_KEY``")
try:
import openai
except ImportError:
raise SetupError(
"missing openai module: ensure that openai is set up correctly."
)
wav_data = BytesIO(audio_data.get_wav_data())
wav_data.name = "SpeechRecognition_audio.wav"
transcript = openai.Audio.transcribe(model, wav_data, api_key=api_key)
return transcript["text"]