убрал транскрипты в отдельный файл. Сделал авточистку транскриптов, чтобы не засорять память.
This commit is contained in:
parent
7b99cda947
commit
f4e3fae4fb
43
bot.py
43
bot.py
@ -7,6 +7,10 @@ import requests
|
||||
import openai
|
||||
from dotenv import load_dotenv
|
||||
from pydub import AudioSegment
|
||||
import hashlib
|
||||
import threading
|
||||
from datetime import datetime, timedelta
|
||||
from transcription_dict import TranscriptionDict
|
||||
|
||||
# Load the environment variables from the .env file
|
||||
load_dotenv()
|
||||
@ -17,6 +21,7 @@ openai.api_key = os.environ.get("OPENAI_API_KEY")
|
||||
# Load the Telegram bot token from the environment variable
|
||||
bot = telebot.TeleBot(os.environ.get("TELEGRAM_BOT_TOKEN"))
|
||||
|
||||
transcription_dict = TranscriptionDict()
|
||||
|
||||
def check_user(func):
|
||||
def check_user_int(message):
|
||||
@ -147,9 +152,46 @@ def handle_voice_message(message):
|
||||
|
||||
# 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)
|
||||
else:
|
||||
bot.reply_to(message, "Не удалось получить голосовое сообщение.")
|
||||
|
||||
# Define a callback function to handle the voice text button click
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith("voice_text:"))
|
||||
def send_voice_text(call):
|
||||
# Extract the transcribed text from the callback data
|
||||
transcription_hash = call.data.split(':')[1]
|
||||
voice_text = transcription_dict.get(transcription_hash)
|
||||
|
||||
if voice_text:
|
||||
# Generate a response using the ChatGPT model
|
||||
(response_text, reason, cost) = generate_chat_response(voice_text)
|
||||
|
||||
print(response_text)
|
||||
|
||||
# Send the response back to the user with markdown formatting
|
||||
try:
|
||||
ans = bot.send_message(chat_id=call.message.chat.id, text=response_text , parse_mode='Markdown')
|
||||
bot.reply_to(ans, f"Finish reason: {reason} \nCost: {cost}")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
bot.send_message(chat_id=call.message.chat.id, text="Что-то пошло не так c ответом. Пробую другой метод...")
|
||||
|
||||
try:
|
||||
bot.send_message(chat_id=call.message.chat.id, text=response_text)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
bot.send_message(chat_id=call.message.chat.id, text="Что-то пошло не так c ответом")
|
||||
|
||||
else:
|
||||
bot.send_message(chat_id=call.message.chat.id, text="Не удалось получить текст голосового сообщения.")
|
||||
|
||||
# Define a handler function for incoming messages
|
||||
@bot.message_handler(func=lambda message: True)
|
||||
@check_user
|
||||
@ -179,5 +221,6 @@ def handle_message(message):
|
||||
bot.reply_to(message, "Что-то пошло не так c ответом...")
|
||||
|
||||
|
||||
|
||||
# Start the bot and listen for incoming messages
|
||||
bot.polling()
|
||||
|
||||
38
transcription_dict.py
Normal file
38
transcription_dict.py
Normal file
@ -0,0 +1,38 @@
|
||||
import time
|
||||
import threading
|
||||
|
||||
class TranscriptionDict:
|
||||
def __init__(self, max_size=1000):
|
||||
self.max_size = max_size
|
||||
self._dict = {}
|
||||
|
||||
# Start a thread to clean expired items every 3 minutes
|
||||
self._clean_thread = threading.Thread(target=self._clean_expired_items_periodically)
|
||||
self._clean_thread.daemon = True
|
||||
self._clean_thread.start()
|
||||
|
||||
def __len__(self):
|
||||
return len(self._dict)
|
||||
|
||||
def get(self, key):
|
||||
return self._dict[key]
|
||||
|
||||
def set(self, key, value):
|
||||
self._dict[key] = value
|
||||
if len(self._dict) > self.max_size:
|
||||
self._clean_expired_items()
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self._dict
|
||||
|
||||
def _clean_expired_items(self):
|
||||
now = time.time()
|
||||
expired_items = [k for k, v in self._dict.items() if now - v[1] >= 180] # 180 seconds = 3 minutes
|
||||
for key in expired_items:
|
||||
del self._dict[key]
|
||||
|
||||
def _clean_expired_items_periodically(self):
|
||||
while True:
|
||||
time.sleep(180) # Check for expired items every 3 minutes
|
||||
self._clean_expired_items()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user