mirror of
https://github.com/marcogll/talia_bot.git
synced 2026-01-13 21:35:19 +00:00
- Add file upload security validation to prevent processing of potentially harmful files. - Update python-telegram-bot to a pinned version and upgrade other dependencies. - Implement Whisper transcription agent for voice message processing. - Restore Google Calendar functionality with provided credentials.
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
# bot/modules/transcription.py
|
|
# This module handles audio transcription using the Whisper API.
|
|
|
|
import logging
|
|
import os
|
|
from openai import OpenAI
|
|
from bot.config import OPENAI_API_KEY
|
|
|
|
# Set up logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Initialize the OpenAI client
|
|
if not OPENAI_API_KEY:
|
|
logger.error("OPENAI_API_KEY is not configured in environment variables.")
|
|
client = None
|
|
else:
|
|
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
def transcribe_audio(file_path: str) -> str:
|
|
"""
|
|
Transcribes an audio file using the Whisper API.
|
|
|
|
Args:
|
|
file_path: The path to the audio file to transcribe.
|
|
|
|
Returns:
|
|
The transcribed text, or an error message if transcription fails.
|
|
"""
|
|
if not client:
|
|
return "Error: OpenAI API key is not configured."
|
|
|
|
if not os.path.exists(file_path):
|
|
logger.error(f"Audio file not found at: {file_path}")
|
|
return "Error: Audio file not found."
|
|
|
|
try:
|
|
logger.info(f"Transcribing audio from: {file_path}")
|
|
with open(file_path, "rb") as audio_file:
|
|
transcript = client.audio.transcriptions.create(
|
|
model="whisper-1",
|
|
file=audio_file
|
|
)
|
|
logger.info("Transcription successful.")
|
|
return transcript.text
|
|
except Exception as e:
|
|
logger.error(f"Error during audio transcription: {e}")
|
|
return "Error: Could not transcribe audio."
|