-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Text2Speech Plugin #4622 #4623
Closed
Closed
Text2Speech Plugin #4622 #4623
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Text2Speech | ||
====== | ||
|
||
Version 0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
|
||
/** | ||
* Description of Text2SpeechPlugin. | ||
* | ||
* @author Francis Gonzales <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
|
||
/** | ||
* Install the Text2Speech Plugin. | ||
* | ||
* @package chamilo.plugin.text2speech | ||
*/ | ||
require_once __DIR__.'/../../main/inc/global.inc.php'; | ||
require_once __DIR__.'/Text2SpeechPlugin.php'; | ||
|
||
if (!api_is_platform_admin()) { | ||
exit('You must have admin permissions to install plugins'); | ||
} | ||
|
||
Text2SpeechPlugin::create()->install(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
|
||
$strings['plugin_title'] = 'Text to Speech (Text2Speech)'; | ||
$strings['plugin_comment'] = 'Plugin to convert text to speech using a 3rd-party service'; | ||
$strings['tool_enable'] = 'Enable plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
|
||
$strings['plugin_title'] = 'Texte pour parler (Text2Speech)'; | ||
$strings['plugin_comment'] = "Ce plugin permet de convertir du texte en parole à l'aide d'un service tiers"; | ||
$strings['tool_enable'] = 'Activer le plug-in'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
$strings['plugin_title'] = 'Texto a Voz (Text2Speech)'; | ||
$strings['plugin_comment'] = 'Este plugin es para convertir texto a voz usando un servicio de terceros'; | ||
$strings['tool_enable'] = 'Enable plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
|
||
require_once __DIR__.'/Text2SpeechPlugin.php'; | ||
|
||
$plugin_info = Text2SpeechPlugin::create()->get_info(); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
interface IProvider | ||
{ | ||
public function __construct(string $url, string $key, string $filePath); | ||
|
||
public function convert(string $text): string; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/../IProvider.php'; | ||
|
||
class MozillaTTS implements IProvider | ||
{ | ||
private $url; | ||
private $apiKey; | ||
private $filePath; | ||
|
||
public function __construct(string $url, string $apiKey, string $filePath) | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
/* For license terms, see /license.txt */ | ||
|
||
/** | ||
* Uninstall the Text2Speech Plugin. | ||
* | ||
* @package chamilo.plugin.text2speech | ||
*/ | ||
require_once __DIR__.'/../../main/inc/global.inc.php'; | ||
require_once __DIR__.'/Text2SpeechPlugin.php'; | ||
|
||
if (!api_is_platform_admin()) { | ||
exit('You must have admin permissions to install plugins'); | ||
} | ||
|
||
Text2SpeechPlugin::create()->uninstall(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
El api_key es necesario? Cómo lo genero? En mi TTS que tengo no pide api_key y me genera los audios por el api solo con el texto
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creo que no es necesario y que lo pone solo para abrirlo a la posibilidad de tener uno a futuro (para este u otro servicio)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exacto en este momento no es necesario pero lo dejo abierto por si se necesita en algún otro servicio.