From fda6cb4068fcc90f8d4c0e59aeaceb8cf1fafae2 Mon Sep 17 00:00:00 2001 From: ned Date: Thu, 6 Jul 2023 12:04:57 +0200 Subject: [PATCH] added gtts, make remove plugin dependencies from requirements, updated readme --- README.md | 29 +++++++++-------- bot/plugin_manager.py | 4 ++- bot/plugins/gtts_text_to_speech.py | 44 ++++++++++++++++++++++++++ bot/plugins/youtube_audio_extractor.py | 3 +- bot/usage_tracker.py | 2 +- requirements.txt | 6 +--- 6 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 bot/plugins/gtts_text_to_speech.py diff --git a/README.md b/README.md index 0ddd65c..d13a52d 100644 --- a/README.md +++ b/README.md @@ -109,19 +109,22 @@ Check out the [official API reference](https://platform.openai.com/docs/api-refe | `SHOW_PLUGINS_USED` | Whether to show which plugins were used for a response | `false` | #### Available plugins -| Name | Description | Required environment variable(s) | -|---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------| -| `weather` | Daily weather and 7-day forecast for any location (powered by [Open-Meteo](https://open-meteo.com)) | - | -| `wolfram` | WolframAlpha queries (powered by [WolframAlpha](https://www.wolframalpha.com)) | `WOLFRAM_APP_ID` | -| `ddg_web_search` | Web search (powered by [DuckDuckGo](https://duckduckgo.com)) | - | -| `ddg_translate` | Translate text to any language (powered by [DuckDuckGo](https://duckduckgo.com)) | - | -| `ddg_image_search` | Search image or GIF (powered by [DuckDuckGo](https://duckduckgo.com)) | - | -| `crypto` | Live cryptocurrencies rate (powered by [CoinCap](https://coincap.io)) - by [@stumpyfr](https://github.com/stumpyfr) | - | -| `spotify` | Spotify top tracks/artists, currently playing song and content search (powered by [Spotify](https://spotify.com)). Requires one-time authorization. | `SPOTIFY_CLIENT_ID`, `SPOTIFY_CLIENT_SECRET`, `SPOTIFY_REDIRECT_URI` | -| `worldtimeapi` | Get latest world time (powered by [WorldTimeAPI](https://worldtimeapi.org/)) | `WORLDTIME_DEFAULT_TIMEZONE` | -| `dice` | Send a dice in the chat! | - | -| `youtube_audio_extractor` | Extract audio from YouTube videos | - | -| `deepl_translate` | Translate text to any language (powered by [DeepL](https://deepl.com)) - by [@LedyBacer](https://github.com/LedyBacer) | `DEEPL_API_KEY` | +| Name | Description | Required environment variable(s) | Dependency | +|---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|----------------------------------------| +| `weather` | Daily weather and 7-day forecast for any location (powered by [Open-Meteo](https://open-meteo.com)) | - | | +| `wolfram` | WolframAlpha queries (powered by [WolframAlpha](https://www.wolframalpha.com)) | `WOLFRAM_APP_ID` | `pip install wolframalpha~=5.0.0` | +| `ddg_web_search` | Web search (powered by [DuckDuckGo](https://duckduckgo.com)) | - | `pip install duckduckgo_search~=3.8.3` | +| `ddg_translate` | Translate text to any language (powered by [DuckDuckGo](https://duckduckgo.com)) | - | `pip install duckduckgo_search~=3.8.3` | +| `ddg_image_search` | Search image or GIF (powered by [DuckDuckGo](https://duckduckgo.com)) | - | `pip install duckduckgo_search~=3.8.3` | +| `crypto` | Live cryptocurrencies rate (powered by [CoinCap](https://coincap.io)) - by [@stumpyfr](https://github.com/stumpyfr) | - | | +| `spotify` | Spotify top tracks/artists, currently playing song and content search (powered by [Spotify](https://spotify.com)). Requires one-time authorization. | `SPOTIFY_CLIENT_ID`, `SPOTIFY_CLIENT_SECRET`, `SPOTIFY_REDIRECT_URI` | `pip install spotipy~=2.23.0` | +| `worldtimeapi` | Get latest world time (powered by [WorldTimeAPI](https://worldtimeapi.org/)) | `WORLDTIME_DEFAULT_TIMEZONE` | | +| `dice` | Send a dice in the chat! | - | | +| `youtube_audio_extractor` | Extract audio from YouTube videos | - | `pip install pytube~=15.0.0` | +| `deepl_translate` | Translate text to any language (powered by [DeepL](https://deepl.com)) - by [@LedyBacer](https://github.com/LedyBacer) | `DEEPL_API_KEY` | | +| `gtts_text_to_speech` | Text to speech (powered by Google Translate APIs) | - | `pip install gtts~=2.3.2` | + +**Note**: some plugins have additional dependencies that are not listed in the `requirements.txt` file. If you plan on using these plugins, you can install them manually using the command above (see the `Dependency` column). #### Environment variables | Variable | Description | Default value | diff --git a/bot/plugin_manager.py b/bot/plugin_manager.py index 2a4d334..6b5830b 100644 --- a/bot/plugin_manager.py +++ b/bot/plugin_manager.py @@ -1,5 +1,6 @@ import json +from bot.plugins.gtts_text_to_speech import GTTSTextToSpeech from plugins.dice import DicePlugin from plugins.youtube_audio_extractor import YouTubeAudioExtractorPlugin from plugins.ddg_image_search import DDGImageSearchPlugin @@ -31,7 +32,8 @@ class PluginManager: 'worldtimeapi': WorldTimeApiPlugin, 'youtube_audio_extractor': YouTubeAudioExtractorPlugin, 'dice': DicePlugin, - 'deepl_translate': DeeplTranslatePlugin + 'deepl_translate': DeeplTranslatePlugin, + 'gtts_text_to_speech': GTTSTextToSpeech, } self.plugins = [plugin_mapping[plugin]() for plugin in enabled_plugins if plugin in plugin_mapping] diff --git a/bot/plugins/gtts_text_to_speech.py b/bot/plugins/gtts_text_to_speech.py new file mode 100644 index 0000000..50f0120 --- /dev/null +++ b/bot/plugins/gtts_text_to_speech.py @@ -0,0 +1,44 @@ +import datetime +from typing import Dict + +from gtts import gTTS + +from .plugin import Plugin + + +class GTTSTextToSpeech(Plugin): + """ + A plugin to convert text to speech using Google Translate's Text to Speech API + """ + + def get_source_name(self) -> str: + return "gTTS" + + def get_spec(self) -> [Dict]: + return [{ + "name": "google_translate_text_to_speech", + "description": "Translate text to speech using Google Translate's Text to Speech API", + "parameters": { + "type": "object", + "properties": { + "text": {"type": "string", "description": "The text to translate to speech"}, + "lang": { + "type": "string", "description": "The language of the text to translate to speech." + "Infer this from the language of the text.", + }, + }, + "required": ["text", "lang"], + }, + }] + + async def execute(self, function_name, **kwargs) -> Dict: + tts = gTTS(kwargs['text'], lang=kwargs.get('lang', 'en')) + output = f'gtts_{datetime.datetime.now().timestamp()}.mp3' + tts.save(output) + return { + 'direct_result': { + 'kind': 'file', + 'format': 'path', + 'value': output + } + } diff --git a/bot/plugins/youtube_audio_extractor.py b/bot/plugins/youtube_audio_extractor.py index da552ce..5fc4d80 100644 --- a/bot/plugins/youtube_audio_extractor.py +++ b/bot/plugins/youtube_audio_extractor.py @@ -1,4 +1,5 @@ import logging +import re from typing import Dict from pytube import YouTube @@ -32,7 +33,7 @@ class YouTubeAudioExtractorPlugin(Plugin): try: video = YouTube(link) audio = video.streams.filter(only_audio=True, file_extension='mp4').first() - output = video.title + '.mp4' + output = re.sub(r'[^\w\-_\. ]', '_', video.title) + '.mp3' audio.download(filename=output) return { 'direct_result': { diff --git a/bot/usage_tracker.py b/bot/usage_tracker.py index c733f9e..c7bb7b3 100644 --- a/bot/usage_tracker.py +++ b/bot/usage_tracker.py @@ -74,7 +74,7 @@ class UsageTracker: :param tokens_price: price per 1000 tokens, defaults to 0.002 """ today = date.today() - token_cost = round(tokens * tokens_price / 1000, 6) + token_cost = round(float(tokens) * tokens_price / 1000, 6) self.add_current_costs(token_cost) # update usage_history diff --git a/requirements.txt b/requirements.txt index bb30eed..1c523d3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,8 +4,4 @@ tiktoken==0.4.0 openai==0.27.8 python-telegram-bot==20.3 requests~=2.31.0 -tenacity==8.2.2 -wolframalpha==5.0.0 -duckduckgo_search==3.8.3 -spotipy==2.23.0 -pytube==15.0.0 \ No newline at end of file +tenacity==8.2.2 \ No newline at end of file