Modernizing the transcribe to new version of Open AI
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ekaropolus 2025-07-07 03:39:13 -06:00
parent 96cb030e19
commit 7119d35f7f

View File

@ -2,7 +2,7 @@ import os
import json import json
import logging import logging
import openai from openai import OpenAI
from telegram import Update, Bot from telegram import Update, Bot
from django.http import JsonResponse from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
@ -19,9 +19,8 @@ from .handlers import (
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Configura tu API Key de OpenAI # Configura tu cliente de OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY") client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# ------------------------------- # -------------------------------
# 🛠 Modular local handlers inside views.py # 🛠 Modular local handlers inside views.py
@ -91,29 +90,25 @@ async def dispatch_private_commands(update, text):
# ------------------------------- # -------------------------------
# 🛠 Voice transcription helper # 🛠 Voice transcription helper (Whisper via OpenAI client)
# ------------------------------- # -------------------------------
async def transcribe_with_whisper(update, bot): async def transcribe_with_whisper(update, bot):
# 1. Descarga el archivo de voz desde Telegram # 1. Descarga el archivo de voz desde Telegram
tg_file = await bot.get_file(update.message.voice.file_id) tg_file = await bot.get_file(update.message.voice.file_id)
download_path = f"/tmp/{update.message.voice.file_id}.ogg" download_path = f"/tmp/{update.message.voice.file_id}.ogg"
# En python-telegram-bot v21: download_to_drive es coroutine
await tg_file.download_to_drive(download_path) await tg_file.download_to_drive(download_path)
# 2. Envía el audio a la API Whisper de OpenAI # 2. Envía el audio a la API de OpenAI para transcripción
with open(download_path, "rb") as audio: with open(download_path, "rb") as audio_file:
transcript = openai.Audio.transcribe( transcript = client.audio.transcriptions.create(
model="whisper-1", model="gpt-4o-transcribe", # o "whisper-1"
file=audio, file=audio_file,
response_format="text", response_format="text",
language="es" language="es"
) )
return transcript.strip() return transcript.text if hasattr(transcript, 'text') else transcript
# -------------------------------
# 🌐 Main webhook
# ------------------------------- # -------------------------------
@csrf_exempt @csrf_exempt