diff --git a/plugin/ai_helper/tool/learnpath.php b/plugin/ai_helper/tool/learnpath.php index 765bcb9f691..9bd58485f69 100644 --- a/plugin/ai_helper/tool/learnpath.php +++ b/plugin/ai_helper/tool/learnpath.php @@ -9,9 +9,13 @@ require_once __DIR__.'/../../../main/inc/global.inc.php'; require_once __DIR__.'/../AiHelperPlugin.php'; +require_once __DIR__.'/../../text2speech/Text2SpeechPlugin.php'; require_once api_get_path(SYS_CODE_PATH).'exercise/export/aiken/aiken_classes.php'; require_once api_get_path(SYS_CODE_PATH).'exercise/export/aiken/aiken_import.inc.php'; +$text2speechPlugin = Text2SpeechPlugin::create(); +$isTextToSpeechEnabled = $text2speechPlugin->get('tool_enable') && $text2speechPlugin->get('tool_lp_enable'); + $plugin = AiHelperPlugin::create(); $apiList = $plugin->getApiList(); @@ -35,6 +39,7 @@ $messageGetItems = 'Generate the table of contents of a course in "%s" in %d or less chapters on the topic of "%s" in a list separated with comma, without chapter number. Do not include a conclusion chapter.'; $prompt = sprintf($messageGetItems, $courseLanguage, $chaptersCount, $topic); + $resultText = $plugin->openAiGetCompletionText($prompt, 'learnpath'); if (isset($resultText['error']) && true === $resultText['error']) { @@ -65,10 +70,19 @@ $promptItem = sprintf($messageGetItemContent, $topic, $courseLanguage, $wordsCount, $title); $resultContentText = $plugin->openAiGetCompletionText($promptItem, 'learnpath'); $lpItemContent = (!empty($resultContentText) ? trim($resultContentText) : ''); + $audioControl = ''; + if ($isTextToSpeechEnabled && !empty($lpItemContent)) { + $filePath = $text2speechPlugin->convert(strip_tags($lpItemContent)); + $audioControl = ''; + } + if (false !== stripos($lpItemContent, '')) { - $lpItemContent = preg_replace("||i", "\r\n$style\r\n\\0", $lpItemContent); + $lpItemContent = preg_replace("||i", "\r\n$style\r\n\\0", $audioControl.$lpItemContent); } else { - $lpItemContent = ''.trim($title).''.$style.''.$lpItemContent.''; + $lpItemContent = ''.trim($title).''.$style.''.$audioControl.$lpItemContent.''; } $lpItems[$position]['content'] = $lpItemContent; $position++; diff --git a/plugin/text2speech/.DS_Store b/plugin/text2speech/.DS_Store new file mode 100644 index 00000000000..f80090f6222 Binary files /dev/null and b/plugin/text2speech/.DS_Store differ diff --git a/plugin/text2speech/README.md b/plugin/text2speech/README.md new file mode 100755 index 00000000000..daafad1c9de --- /dev/null +++ b/plugin/text2speech/README.md @@ -0,0 +1,4 @@ +Text2Speech +====== + +Version 0.1 diff --git a/plugin/text2speech/Text2SpeechPlugin.php b/plugin/text2speech/Text2SpeechPlugin.php new file mode 100644 index 00000000000..a44f5a55bcb --- /dev/null +++ b/plugin/text2speech/Text2SpeechPlugin.php @@ -0,0 +1,108 @@ + + */ +class Text2SpeechPlugin extends Plugin +{ + public const MOZILLATTS_API = 'mozillatts'; + public const PATH_TO_SAVE_FILES = __DIR__.'/../../app/upload/plugins/text2speech/'; + + protected function __construct() + { + $version = '0.1'; + $author = 'Francis Gonzales'; + + $message = 'Description'; + + $settings = [ + $message => 'html', + 'tool_enable' => 'boolean', + 'api_name' => [ + 'type' => 'select', + 'options' => $this->getApiList(), + ], + 'api_key' => 'text', + 'url' => 'text', + 'tool_lp_enable' => 'boolean', + ]; + + parent::__construct($version, $author, $settings); + } + + /** + * Get the list of apis availables. + * + * @return array + */ + public function getApiList() + { + return [ + self::MOZILLATTS_API => 'MozillaTTS', + ]; + } + + /** + * Get the completion text from openai. + * + * @return string + */ + public function convert(string $text) + { + $path = '/app/upload/plugins/text2speech/'; + switch ($this->get('api_name')) { + case self::MOZILLATTS_API: + require_once __DIR__.'/src/mozillatts/MozillaTTS.php'; + + $mozillaTTS = new MozillaTTS($this->get('url'), $this->get('api_key'), self::PATH_TO_SAVE_FILES); + $path .= $mozillaTTS->convert($text); + break; + } + + return $path; + } + + /** + * Get the plugin directory name. + */ + public function get_name(): string + { + return 'text2speech'; + } + + /** + * Get the class instance. + * + * @staticvar Text2SpeechPlugin $result + */ + public static function create(): Text2SpeechPlugin + { + static $result = null; + + return $result ?: $result = new self(); + } + + /** + * Install the plugin. create folder to save files. + */ + public function install() + { + if (!file_exists(self::PATH_TO_SAVE_FILES)) { + mkdir(self::PATH_TO_SAVE_FILES); + } + } + + /** + * Unistall plugin. Clear the folder. + */ + public function uninstall() + { + if (file_exists(self::PATH_TO_SAVE_FILES)) { + array_map('unlink', glob(self::PATH_TO_SAVE_FILES.'/*.*')); + rmdir(self::PATH_TO_SAVE_FILES); + } + } +} diff --git a/plugin/text2speech/install.php b/plugin/text2speech/install.php new file mode 100755 index 00000000000..408e9f86eb2 --- /dev/null +++ b/plugin/text2speech/install.php @@ -0,0 +1,16 @@ +install(); diff --git a/plugin/text2speech/lang/english.php b/plugin/text2speech/lang/english.php new file mode 100755 index 00000000000..0e726ced1e9 --- /dev/null +++ b/plugin/text2speech/lang/english.php @@ -0,0 +1,6 @@ +get_info(); diff --git a/plugin/text2speech/src/IProvider.php b/plugin/text2speech/src/IProvider.php new file mode 100644 index 00000000000..7312fbbe288 --- /dev/null +++ b/plugin/text2speech/src/IProvider.php @@ -0,0 +1,8 @@ +url = $url; + $this->apiKey = $apiKey; + $this->filePath = $filePath; + } + + public function convert(string $text): string + { + return $this->request($text); + } + + private function request(string $data): string + { + $filename = uniqid().'.wav'; + $filePath = $this->filePath.$filename; + $resource = fopen($filePath, 'w'); + + $client = new GuzzleHttp\Client(); + $client->get($this->url.'?api_key='.urlencode($this->apiKey). + '&text='.str_replace('%0A', '+', urlencode($data)), [ + 'headers' => [ + 'Cache-Control' => 'no-cache', + 'Content-Type' => 'audio/wav', + ], + 'sink' => $resource, + ]); + + return $filename; + } +} diff --git a/plugin/text2speech/uninstall.php b/plugin/text2speech/uninstall.php new file mode 100755 index 00000000000..df666a1d5dc --- /dev/null +++ b/plugin/text2speech/uninstall.php @@ -0,0 +1,16 @@ +uninstall();