Skip to content

Commit

Permalink
✨ Feature: Support voice input
Browse files Browse the repository at this point in the history
💻 Code: Upgrade ModelMerge version to 0.9.7
  • Loading branch information
yym68686 committed Jul 23, 2024
1 parent 9077be6 commit 94a7830
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ModelMerge
47 changes: 47 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,52 @@ async def handle_file(update, context):
message = await context.bot.send_message(chat_id=chatid, message_thread_id=message_thread_id, text=escape(strings['message_doc'][get_current_lang()]), parse_mode='MarkdownV2', disable_web_page_preview=True)
await delete_message(update, context, [message.message_id])

from pydub import AudioSegment
from io import BytesIO
async def transcribe_audio(file_id: str, bot) -> str:
file_unique_id = file_id
filename_mp3 = f'{file_unique_id}.mp3'

try:
file = await bot.get_file(file_id)
file_bytes = await file.download_as_bytearray()

audio = AudioSegment.from_file(BytesIO(file_bytes))
audio.export(filename_mp3, format="mp3")

with open(filename_mp3, 'rb') as audio_file:
transcript = config.whisperBot.generate(audio_file)

return transcript

except Exception as e:
logging.exception(e)
return f"Ошибка при обработке аудиофайла: {str(e)}"
finally:
import os
if os.path.exists(filename_mp3):
os.remove(filename_mp3)


@decorators.GroupAuthorization
@decorators.Authorization
@decorators.APICheck
async def handle_voice(update, context):
file_id = update.message.voice.file_id
bot = context.bot

transcript_text = await transcribe_audio(file_id, bot)

_, _, _, chatid, messageid, _, _, message_thread_id, convo_id, file_url, _ = await GetMesageInfo(update, context)
engine = get_ENGINE(convo_id)
robot, role = get_robot(convo_id)
if Users.get_config(convo_id, "TITLE"):
title = f"`🤖️ {engine}`\n\n"
if Users.get_config(convo_id, "REPLY") == False:
messageid = None
pass_history = Users.get_config(convo_id, "PASS_HISTORY")
await getChatGPT(update, context, title, robot, transcript_text, chatid, messageid, convo_id, message_thread_id, pass_history)

@decorators.GroupAuthorization
@decorators.Authorization
@decorators.APICheck
Expand Down Expand Up @@ -625,6 +671,7 @@ async def post_init(application: Application) -> None:
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, lambda update, context: command_bot(update, context, prompt=None, has_command=False), block = False))
application.add_handler(MessageHandler(filters.CAPTION & ((filters.PHOTO & ~filters.COMMAND) | (filters.Document.FileExtension("jpg") | filters.Document.FileExtension("jpeg") | filters.Document.FileExtension("png"))), lambda update, context: command_bot(update, context, prompt=None, has_command=False)))
application.add_handler(MessageHandler(~filters.CAPTION & ((filters.PHOTO & ~filters.COMMAND) | (filters.Document.PDF | filters.Document.TXT | filters.Document.DOC | filters.Document.FileExtension("jpg") | filters.Document.FileExtension("jpeg") | filters.Document.FileExtension("md") | filters.Document.FileExtension("py"))), handle_file))
application.add_handler(MessageHandler(filters.VOICE, handle_voice))
application.add_handler(MessageHandler(filters.COMMAND, unknown))
application.add_error_handler(error)

Expand Down
7 changes: 4 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utils.i18n import strings
from datetime import datetime
from ModelMerge.src.ModelMerge.utils import prompt
from ModelMerge.src.ModelMerge.models import chatgpt, claude, groq, claude3, gemini, PLUGINS
from ModelMerge.src.ModelMerge.models import chatgpt, claude, groq, claude3, gemini, PLUGINS, whisper
from ModelMerge.src.ModelMerge.models.base import BaseAPI

from telegram import InlineKeyboardButton
Expand Down Expand Up @@ -143,9 +143,9 @@ def get_ENGINE(user_id = None):
temperature = float(os.environ.get('temperature', '0.5'))
CLAUDE_API = os.environ.get('claude_api_key', None)

ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot = None, None, None, None, None, None
ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot, whisperBot = None, None, None, None, None, None, None
def update_ENGINE(data = None, chat_id=None):
global Users, ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot
global Users, ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot, whisperBot
if data:
Users.set_config(chat_id, "engine", data)
engine = Users.get_config(chat_id, "engine")
Expand All @@ -159,6 +159,7 @@ def update_ENGINE(data = None, chat_id=None):
else:
ChatGPTbot = chatgpt(api_key=f"{api_key}", api_url=api_url, engine=engine, system_prompt=systemprompt, temperature=temperature)
SummaryBot = chatgpt(api_key=f"{api_key}", api_url=api_url, engine="gpt-3.5-turbo", system_prompt=systemprompt, temperature=temperature)
whisperBot = whisper(api_key=f"{api_key}", api_url=api_url)
if CLAUDE_API and "claude-2.1" in engine:
claudeBot = claude(api_key=f"{CLAUDE_API}", engine=engine, system_prompt=claude_systemprompt, temperature=temperature)
if CLAUDE_API and "claude-3" in engine:
Expand Down

0 comments on commit 94a7830

Please sign in to comment.