From a9623e698560d03beedd5361f8fc167f0ba5d2a0 Mon Sep 17 00:00:00 2001 From: Anton Palgunov Date: Sat, 1 Apr 2023 21:43:44 +0100 Subject: [PATCH] upd --- .dockerignore | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 5 +++++ bot.py | 8 +++----- custom_openai.py | 24 ++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 custom_openai.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d5dc156 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,46 @@ +# .dockerignore + +# Игнорировать каталоги с кэшем и логами +__pycache__ +*.pyc +*.pyo +*.pyd +*.log + +# Игнорировать виртуальное окружение и зависимости +venv +*.egg-info/ +dist/ +build/ +.virtualenv/ +.pyenv/ + +# Игнорировать файлы и каталоги системы контроля версий +.git +.gitignore +.svn +.hg +.hgignore + +# Игнорировать файлы конфигурации IDE и редакторов +.vscode/ +.idea/ +*.swp +*.swo +*.swn +*.bak + +# Игнорировать файлы тестов и документации +tests/ +docs/ + +# Игнорировать файлы для развертывания и среды +.dockerignore +Dockerfile +*.yml +*.yaml +*.sh +*.md +*.txt +!requirements.txt +LICENSE \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 88cc8c3..1fff034 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,10 @@ FROM python:3.9-slim-buster +# Установить зависимости для FFmpeg +RUN apt-get update && \ + apt-get install -y --no-install-recommends ffmpeg && \ + rm -rf /var/lib/apt/lists/* + WORKDIR /app COPY requirements.txt requirements.txt diff --git a/bot.py b/bot.py index 44797a9..26d7ef1 100644 --- a/bot.py +++ b/bot.py @@ -11,6 +11,7 @@ import hashlib import threading from datetime import datetime, timedelta from transcription_dict import TranscriptionDict +from custom_openai import CustomAudio # Load the environment variables from the .env file load_dotenv() @@ -146,19 +147,16 @@ def handle_voice_message(message): audio_bytes = bytearray(mp3_data.read()) - transcript = openai.Audio.transcribe("whisper-1", file=audio_bytes, filename="voice_message.mp3") + transcript = CustomAudio.transcribe("whisper-1", file=audio_bytes, filename="voice_message.mp3") transcription = transcript['text'] - # Return the transcribed text back to the user - bot.reply_to(message, transcription) - transcription_hash = hashlib.md5(transcription.encode('utf-8')).hexdigest()[:8] transcription_dict.set(transcription_hash, transcription) markup = telebot.types.InlineKeyboardMarkup() markup.add(telebot.types.InlineKeyboardButton("Отправить текст", callback_data=f"voice_text:{transcription_hash}")) - bot.send_message(message.chat.id, f"Текст сообщения: {transcription}", reply_markup=markup) + bot.reply_to(message, transcription, reply_markup=markup) else: bot.reply_to(message, "Не удалось получить голосовое сообщение.") diff --git a/custom_openai.py b/custom_openai.py new file mode 100644 index 0000000..20eb64b --- /dev/null +++ b/custom_openai.py @@ -0,0 +1,24 @@ +from openai.api_resources.abstract import APIResource +from openai.api_resources.audio import Audio +from openai import util + +class CustomAudio(Audio): + @classmethod + def transcribe( + cls, + model, + file, + filename, + api_key=None, + api_base=None, + api_type=None, + api_version=None, + organization=None, + **params, + ): + requestor, files, data = cls._prepare_request(file, filename, model, **params) + url = cls._get_url("transcriptions") + response, _, api_key = requestor.request("post", url, files=files, params=data) + return util.convert_to_openai_object( + response, api_key, api_version, organization + ) \ No newline at end of file