added gtts, make remove plugin dependencies from requirements, updated readme

This commit is contained in:
ned
2023-07-06 12:04:57 +02:00
parent 41b92ff910
commit fda6cb4068
6 changed files with 67 additions and 21 deletions

View File

@@ -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 |

View File

@@ -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]

View File

@@ -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
}
}

View File

@@ -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': {

View File

@@ -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

View File

@@ -5,7 +5,3 @@ 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