diff --git a/telegrambot/audio/test b/telegrambot/audio/test new file mode 100644 index 00000000..e69de29b diff --git a/telegrambot/main.py b/telegrambot/main.py new file mode 100644 index 00000000..6f3591ad --- /dev/null +++ b/telegrambot/main.py @@ -0,0 +1,114 @@ +import os +from configparser import ConfigParser + +from telegram.ext import Updater, CommandHandler, MessageHandler, ConversationHandler, Filters +from textbase import bot, Message +from textbase.models import OpenAI +from typing import List +import whisper + + +class TextBase: + def __init__(self): + os.environ["PATH"] = os.path.join(os.path.dirname(__file__),"utils") + config = ConfigParser() + config.read(f'utils/config.ini') + self.token = config.get('TELEGRAM', 'auth_token') + self.OPENAIKEY = config.get('OPENAI', 'api_key') + self.PROMT = "" + self.SELECT_DATA = 1 + self.user_data = {} + self.model = whisper.load_model("medium") + self.updater = Updater(self.token, use_context=True) + self.dispatcher = self.updater.dispatcher + self.message_handler = None + + def run_bot(self): + print("Started bot...") + self.dispatcher.add_handler(CommandHandler('start', self.start)) + self.dispatcher.add_handler(CommandHandler('quit', self.quit)) + self.updater.start_polling() + self.updater.idle() + + + + def start(self, update, context): + update.message.reply_text("Welcome to textbase bot! Send me any message, and I'll respond.") + self.message_handler = MessageHandler(Filters.all, self.select_data) + self.dispatcher.add_handler(self.message_handler) + + + def quit(self, update, context): + update.message.reply_text("Goodbye!") + if self.message_handler: + self.dispatcher.remove_handler(self.message_handler) + self.message_handler = None + + + + def select_data(self, update, context): + user_id = update.message.chat_id + self.user_data[user_id] = {'text': None, 'audio': None} + if update.message.text: + update.message.reply_text("Text received. Your result is being Prepared") + self.user_data[user_id]['text'] = update.message.text + elif update.message.audio or update.message.voice: + update.message.reply_text("Audio received. Your result is being Prepared") + audio_file = update.message.audio or update.message.voice + file_id = audio_file.file_id + file_path = os.path.join(os.path.dirname(__file__),"audio", f"{user_id}_{file_id}.ogg") + audio_file.get_file().download(file_path) + self.user_data[user_id]['audio'] = file_path + self.generate_text_from_audio(update, context, user_id) + else: + update.message.reply_text("No valid data received. Please send either text or an OGG audio file.") + if self.user_data[user_id]['text']: + self.PROMT = self.user_data[user_id]['text'] + result = self.on_message([]) + update.message.reply_text(result) + + def generate_text_from_audio(self, update, context, user_id): + file_path = self.user_data[user_id]['audio'] + result = self.model.transcribe(file_path)["text"] + self.user_data[user_id]['text'] = result + update.message.reply_text(f""" +Transcribed Text: +{result} +""") + os.remove(file_path) + + + def on_message(self, message_history: List[Message], state: dict = None ): + # Generate GPT-3.5 Turbo response + OpenAI.api_key = self.OPENAIKEY + bot_response = OpenAI.generate( + system_prompt=self.PROMT, + message_history=message_history, # Assuming history is the list of user messages + model="gpt-3.5-turbo", + ) + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + response = response["data"]["messages"] + completeData = "" + for data in response: + completeData += data["value"] + return completeData + + +if __name__ == '__main__': + bot = TextBase() + bot.run_bot() \ No newline at end of file diff --git a/telegrambot/readme.md b/telegrambot/readme.md new file mode 100644 index 00000000..10d3e590 --- /dev/null +++ b/telegrambot/readme.md @@ -0,0 +1,30 @@ +# Telegram Bot Using OpenAi + +## Steps to use the Telegram Bot: + +- ### Prerequisites: + +``` +Git +Python 3.9+ +``` + +- ### Add the API_KEY of OpenAi and Telegram AUTH_TOKEN created by [Botfather](https://t.me/BotFather) in utils/config.ini + +- ### Run the following commands to start the Telegram Bot. + - #### Windows: + ``` + python -m venv env + env\Scripts\activate + pip install -r requirements.txt + python main.py + ``` + - #### MacOS/Linux: + ``` + python3 -m venv env + source env/bin/activate + pip3 install -r requirements.txt + python3 main.py + ``` + + Note: FFmpeg for Windows is already been provided in utils. You can install or add the ffmpeg executable for MacOS/Linux. diff --git a/telegrambot/requirements.txt b/telegrambot/requirements.txt new file mode 100644 index 00000000..0f295ff8 --- /dev/null +++ b/telegrambot/requirements.txt @@ -0,0 +1,63 @@ +aiohttp==3.8.5 +aiosignal==1.3.1 +annotated-types==0.5.0 +APScheduler==3.6.3 +async-timeout==4.0.3 +attrs==23.1.0 +blinker==1.6.2 +cachetools==4.2.2 +certifi==2023.7.22 +charset-normalizer==3.2.0 +click==8.1.7 +cloudevents==1.9.0 +colorama==0.4.6 +deprecation==2.1.0 +ffmpeg-python==0.2.0 +filelock==3.12.3 +flask==2.3.3 +frozenlist==1.4.0 +functions-framework==3.4.0 +future==0.18.3 +idna==3.4 +importlib-metadata==6.8.0 +itsdangerous==2.1.2 +Jinja2==3.1.2 +llvmlite==0.40.1 +MarkupSafe==2.1.3 +more-itertools==10.1.0 +mpmath==1.3.0 +multidict==6.0.4 +networkx==3.1 +numba==0.57.1 +numpy==1.24.4 +openai==0.27.10 +openai-whisper==20230314 +packaging==23.1 +pydantic==2.3.0 +pydantic-core==2.6.3 +python-dotenv==1.0.0 +python-telegram-bot==13.11 +pytz==2023.3 +regex==2023.8.8 +requests==2.31.0 +semantic-version==2.10.0 +setuptools-rust==1.7.0 +six==1.16.0 +sympy==1.12 +tabulate==0.9.0 +termcolor==2.3.0 +textbase-client==0.1.4 +tiktoken==0.3.1 +tomli==2.0.1 +torch==2.0.1 +tornado==6.3.3 +tqdm==4.66.1 +typing-extensions==4.7.1 +tzdata==2023.3 +tzlocal==5.0.1 +urllib3==2.0.4 +watchdog==3.0.0 +werkzeug==2.3.7 +yarl==1.9.2 +yaspin==3.0.0 +zipp==3.16.2 diff --git a/telegrambot/utils/config.ini b/telegrambot/utils/config.ini new file mode 100644 index 00000000..9dae6bd3 --- /dev/null +++ b/telegrambot/utils/config.ini @@ -0,0 +1,7 @@ +[TELEGRAM] +auth_token = +channel_id = +admin_ids = + +[OPENAI] +api_key = \ No newline at end of file diff --git a/telegrambot/utils/ffmpeg.exe b/telegrambot/utils/ffmpeg.exe new file mode 100644 index 00000000..234ae995 Binary files /dev/null and b/telegrambot/utils/ffmpeg.exe differ