Moved to speech and transcribe repos

This commit is contained in:
FusionPBX
2024-04-15 17:12:18 -06:00
committed by GitHub
parent 75439fb4e9
commit c806aae310
6 changed files with 0 additions and 796 deletions

View File

@@ -1,64 +0,0 @@
<?php
//application details
$apps[$x]['name'] = 'AI';
$apps[$x]['uuid'] = 'be9e4d36-bea6-4dd2-b08e-b5f10a0ab8df';
$apps[$x]['category'] = 'AI';
$apps[$x]['subcategory'] = '';
$apps[$x]['version'] = '1.0';
$apps[$x]['license'] = 'Mozilla Public License 1.1';
$apps[$x]['url'] = 'http://www.fusionpbx.com';
$apps[$x]['description']['en-us'] = 'Artificial Intelligence';
//default settings
$y=0;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "c4751875-011d-4181-ba9a-8978ffd7497d";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "ai";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "speech_enabled";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "boolean";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "true";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Text to Speech";
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "e7a77c36-92d1-4fb6-9db3-4f62866dbaf2";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "ai";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "speech_engine";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "openai";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Text to Speech";
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "eced068b-db30-4257-aa7c-6e2659271e4b";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "ai";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "speech_key";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Text to Speech";
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "bc054920-5877-4695-9885-9c9009a7713c";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "ai";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "transcribe_enabled";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "boolean";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "true";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Speech to Text";
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "72b9feeb-b21c-4dad-ad26-dde86955d87b";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "ai";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "transcribe_engine";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "openai";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Speech to Text";
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "7883f9fc-9259-4f9b-a73d-532f44db2a28";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "ai";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "transcribe_key";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Speech to Text";
$y++;
?>

View File

@@ -1,189 +0,0 @@
<?php
/**
* audio class
*
* @method null download
*/
if (!class_exists('ai')) {
class ai {
/**
* declare private variables
*/
private $transcribe_key;
private $speech_key;
/** @var string $engine */
private $transcribe_engine;
private $speech_engine;
/** @var template_engine $object */
private $transcribe_object;
private $speech_object;
private $settings;
public $audio_path;
public $audio_filename;
public $audio_format;
public $audio_model;
public $audio_voice;
public $audio_language;
public $audio_message;
/**
* called when the object is created
*/
public function __construct(settings $settings = null) {
//make the setting object
if ($settings === null) {
$settings = new settings();
}
//add the settings object to the class
$this->settings = $settings;
//build the setting object and get the recording path
$this->transcribe_key = $settings->get('ai', 'transcribe_key');
$this->transcribe_engine = $settings->get('ai', 'transcribe_engine');
$this->speech_key = $settings->get('ai', 'speech_key');
$this->speech_engine = $settings->get('ai', 'speech_engine');
}
/**
* get_voices - get the list voices
*/
public function get_voices() : array {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the voices array
return $object->get_voices();
}
/**
* get_voices - get the list voices
*/
public function get_models() : array {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the voices array
return $object->get_models();
}
/**
* is_translate_enabled - get whether the engine can do translations
*/
public function is_translate_enabled() : bool {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the translate_enabled
return $object->is_translate_enabled();
}
/**
* is_language_enabled - get whether the engine allows to set the language
*/
public function is_language_enabled() : bool {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the language_enabled
return $object->is_language_enabled();
}
/**
* get_languages - get the list languages
*/
public function get_languages() : array {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the languages array
return $object->get_languages();
}
/**
* speech - text to speech
*/
public function speech() {
if (!empty($this->speech_engine)) {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//ensure the class has implemented the audio_interface interface
if ($object instanceof ai_speech) {
$object->set_path($this->audio_path);
$object->set_filename($this->audio_filename);
$object->set_format($this->audio_format);
$object->set_voice($this->audio_voice);
//$object->set_model($this->audio_model);
//$object->set_language($this->audio_language);
//$object->set_translate($this->audio_translate);
$object->set_message($this->audio_message);
$object->speech();
}
else {
return false;
}
}
}
/**
* transcribe - speech to text
*/
public function transcribe() : string {
if (!empty($this->transcribe_engine)) {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->transcribe_engine;
//create the object
$object = new $classname($this->settings);
//ensure the class has implemented the audio_interface interface
if ($object instanceof ai_transcribe) {
if ($object->is_language_enabled() && !empty($this->audio_language)) {
$object->set_language($this->audio_language);
}
$object->set_path($this->audio_path);
$object->set_filename($this->audio_filename);
return $object->transcribe();
}
else {
return '';
}
}
}
}
}
?>

View File

@@ -1,238 +0,0 @@
<?php
/**
* ai_elevenlabs class
*
*/
class ai_elevenlabs implements ai_speech {
private $voice;
private $path;
private $message;
private $format;
private $filename;
private $languages;
private $transcribe_key;
private $speech_key;
private $model;
public function __construct($settings) {
$this->voice = "";
$this->path = "";
$this->message = "";
$this->format = "";
$this->filename = "";
//build the setting object and get the recording path
$this->transcribe_key = $settings->get('ai', 'transcribe_key');
$this->speech_key = $settings->get('ai', 'speech_key');
}
public function set_filename(string $audio_filename) {
$this->filename = $audio_filename;
}
public function set_format(string $audio_format) {
$this->format = $audio_format;
}
public function set_message(string $audio_message) {
$this->message = $audio_message;
}
public function set_path(string $audio_path) {
$this->path = $audio_path;
}
public function set_voice(string $audio_voice) {
$this->voice = $audio_voice;
}
public function speech(): bool {
//get the model automatically
$model_id = $this->get_model();
// set the request URL
$url = 'https://api.elevenlabs.io/v1/text-to-speech/' . $this->voice;
// set the request headers
$headers[] = 'Content-Type: application/json';
$headers[] = 'xi-api-key: '.$this->speech_key;
// set the http data
$data['model_id'] = $model_id;
$data['text'] = $this->message;
//$data['pronunciation_dictionary_locators'][0]['pronunciation_dictionary_id'];
//$data['pronunciation_dictionary_locators'][0]['version_id'];
$data['voice_settings']['similarity_boost'] = 1;
$data['voice_settings']['stability'] = 1;
$data['voice_settings']['style'] = 0;
$data['voice_settings']['use_speaker_boost'] = 'true';
// initialize curl handle
$ch = curl_init($url);
// set the curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// run the curl request and get the response
$response = curl_exec($ch);
// get the errors
$error = curl_error($ch);
// get the http code
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// close the handle
curl_close($ch);
// show the result when there is an error
if ($http_code != 200) {
echo "error ".$error."\n";
echo "http_code ".$http_code."\n";
if (strlen($response) < 500) {
view_array(json_decode($response, true));
}
exit;
}
// save the audio file
if ($http_code == 200) {
file_put_contents($this->path.'/'.$this->filename, $response);
return true;
}
return false;
//$curl = new curl('https://api.elevenlabs.io/v1/text-to-speech/' . $this->voice);
//$response = $curl->set_headers($headers)->post(json_encode($data));
//$error = $curl->get_error();
//$http_code = $curl->get_http_code();
//if ($curl->get_http_code() == 200) {
//save the audio
//if ($http_code == 200) {
// file_put_contents($this->path . '/' . $this->filename, $response);
// return true;
//}
//return false;
}
public function is_language_enabled(): bool {
return false;
}
public function is_model_enabled(): bool {
return false;
}
public function get_languages(): array {
return ['en' => 'English'];
}
public function get_voices(): array {
$return_value = [];
$url = 'https://api.elevenlabs.io/v1/voices';
$headers = [
'Content-Type: application/json',
"xi-api-key: $this->speech_key",
];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if (!empty($response)) {
$json_array = json_decode($response, true);
foreach($json_array['voices'] as $row) {
$voice_id = $row['voice_id'];
$name = $row['name'];
$gender = $row['labels']['gender'] ?? '';
$accent = $row['labels']['accent'] ?? '';
$use_case = $row['labels']['use case'] ?? '';
$recommended_model = $row['high_quality_base_model_ids'][0] ?? '';
$return_value[$voice_id] = "$name ($gender, $accent";
if (!empty($use_case)) {
$return_value[$voice_id] .= ", " . $use_case;
}
$return_value[$voice_id] .= ")";
if (!empty($recommended_model)) {
$return_value[$voice_id] .= " - $recommended_model";
}
}
}
return $return_value;
}
public function set_language(string $audio_language) {
$this->languages = $audio_language;
}
public function set_model(string $model): void {
if (array_key_exists($model, $this->get_models())) {
$this->model = $model;
}
}
public function get_model() {
//if the voice is not set return the default model
if (empty($this->voice)) {
return 'eleven_monolingual_v1';
}
//get the voices and automatically find the model
$url = 'https://api.elevenlabs.io/v1/voices';
$headers = [
'Content-Type: application/json',
"xi-api-key: $this->speech_key",
];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if (!empty($response)) {
$json_array = json_decode($response, true);
foreach($json_array['voices'] as $row) {
if ($this->voice == $row['voice_id'] && !empty($row['high_quality_base_model_ids'][0])) {
return $row['high_quality_base_model_ids'][0];
}
}
return 'eleven_monolingual_v1';
}
}
public function get_models(): array {
return [
'eleven_monolingual_v1' => 'Default',
'eleven_turbo_v1' => 'Eleven Turbo v1',
'eleven_turbo_v2' => 'Eleven Turbo v2',
'eleven_multilingual_v1' => 'Eleven Multilingual v1',
'eleven_multilingual_v2' => 'Eleven Multilingual v2',
];
}
}
?>

View File

@@ -1,270 +0,0 @@
<?php
/**
* ai class
*
* @method null download
*/
if (!class_exists('ai_openai')) {
class ai_openai implements ai_speech, ai_transcribe {
/**
* declare private variables
*/
private $transcribe_key;
private $speech_key;
private $path;
private $filename;
private $format;
private $voice;
private $message;
private $model;
/**
* called when the object is created
*/
public function __construct($settings) {
//build the setting object and get the recording path
$this->transcribe_key = $settings->get('ai', 'transcribe_key');
$this->speech_key = $settings->get('ai', 'speech_key');
}
public function set_path(string $audio_path) {
$this->path = $audio_path;
}
public function set_filename(string $audio_filename) {
$this->filename = $audio_filename;
}
public function set_format(string $audio_format) {
$this->format = $audio_format;
}
public function set_voice(string $audio_voice) {
$this->voice = $audio_voice;
}
public function set_language(string $audio_language) {
$this->language = $audio_language;
}
public function set_translate(string $audio_translate) {
$this->translate = $audio_translate;
}
public function set_message(string $audio_message) {
$this->message = $audio_message;
}
public function is_language_enabled() : bool {
//return the whether engine is handles languages
return false;
}
public function is_translate_enabled() : bool {
//return the whether engine is able to translate
return false;
}
public function get_voices() : array {
$voices = array(
"alloy" => "alloy",
"echo" => "echo",
"fable" => "fable",
"nova" => "nova",
"onyx" => "onyx",
"shimmer" => "shimmer"
);
//return the languages array
return $voices;
}
public function get_languages() : array {
//create the languages array
$languages = array(
"af" => "Afrikaans",
"ar" => "Arabic",
"hy" => "Armenian",
"az" => "Azerbaijani",
"be" => "Belarusian",
"bs" => "Bosnian",
"bg" => "Bulgarian",
"ca" => "Catalan",
"zh" => "Chinese",
"hr" => "Croatian",
"cs" => "Czech",
"da" => "Danish",
"nl" => "Dutch",
"en" => "English",
"et" => "Estonian",
"fi" => "Finnish",
"fr" => "French",
"gl" => "Galician",
"de" => "German",
"el" => "Greek",
"he" => "Hebrew",
"hi" => "Hindi",
"hu" => "Hungarian",
"is" => "Icelandic",
"id" => "Indonesian",
"it" => "Italian",
"ja" => "Japanese",
"kn" => "Kannada",
"kk" => "Kazakh",
"ko" => "Korean",
"lv" => "Latvian",
"lt" => "Lithuanian",
"mk" => "Macedonian",
"ms" => "Malay",
"mr" => "Marathi",
"mi" => "Maori",
"ne" => "Nepali",
"no" => "Norwegian",
"fa" => "Persian",
"pl" => "Polish",
"pt" => "Portuguese",
"ro" => "Romanian",
"ru" => "Russian",
"sr" => "Serbian",
"sk" => "Slovak",
"sl" => "Slovenian",
"es" => "Spanish",
"sw" => "Swahili",
"sv" => "Swedish",
"tl" => "Tagalog",
"ta" => "Tamil",
"th" => "Thai",
"tr" => "Turkish",
"uk" => "Ukrainian",
"ur" => "Urdu",
"vi" => "Vietnamese",
"cy" => "Welsh"
);
//return the languages array
return $languages;
}
/**
* speech - text to speech
*/
public function speech() : bool {
// set the request URL
$url = 'https://api.openai.com/v1/audio/speech';
// set the request headers
$headers = [
'Authorization: Bearer ' . $this->speech_key,
'Content-Type: application/json'
];
// set the http data
$data['model'] = 'tts-1-hd';
$data['input'] = $this->message;
$data['voice'] = $this->voice;
$data['response_format'] = 'wav';
if (isset($this->language)) {
$data['language'] = $this->language;
}
if (isset($this->translate)) {
$data['task'] = 'translate';
}
// initialize curl handle
$ch = curl_init($url);
// set the curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// run the curl request and get the response
$response = curl_exec($ch);
// close the handle
curl_close($ch);
// check for errors
if ($response === false) {
return false;
}
else {
// save the audio file
file_put_contents($this->path.'/'.$this->filename, $response);
return true;
}
}
/**
* transcribe - speech to text
*/
public function transcribe() : string {
// initialize a curl handle
$ch = curl_init();
// set the URL for the request
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/audio/transcriptions');
// set the request method to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the request headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->transcribe_key,
'Content-Type: multipart/form-data'
));
// set the POST data
$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
$post_data['model'] = 'whisper-1';
$post_data['response_format'] = 'text';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// return the response as a string instead of outputting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// run the curl request and transcription message
$this->message = curl_exec($ch);
// check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
exit;
}
// close the handle
curl_close($ch);
// return the transcription
if (empty($this->message)) {
return '';
}
else {
return trim($this->message);
}
}
public function set_model(string $model): void {
if (array_key_exists($model, $this->get_models())) {
$this->model = $model;
}
}
public function get_models(): array {
return [
'tts-1-hd' => 'tts-1-hd'
];
}
}
}
?>

View File

@@ -1,21 +0,0 @@
<?php
//define the template class
if (!interface_exists('ai_speech')) {
interface ai_speech {
public function get_languages() : array;
public function get_models(): array;
public function get_voices() : array;
public function is_language_enabled() : bool;
public function set_filename(string $audio_filename);
public function set_format(string $audio_format);
public function set_language(string $audio_language);
public function set_message(string $audio_message);
public function set_model(string $audio_model): void;
public function set_path(string $audio_path);
public function set_voice(string $audio_voice);
public function speech() : bool;
}
}
?>

View File

@@ -1,14 +0,0 @@
<?php
//define the template class
if (!interface_exists('ai_transcribe')) {
interface ai_transcribe {
public function set_path(string $audio_path);
public function set_filename(string $audio_filename);
public function transcribe() : string;
public function set_language(string $audio_language);
public function get_languages() : array;
}
}
?>