-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
100 lines (88 loc) · 3.47 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
function terminator($q){
urlencode($q);
$api_key = ""; //api.openai.com token
$voicerss_key = ''; // Voicerss token key https://www.voicerss.org/api/
$rapid_key = ''; // voicerss-text-to-speech.p.rapidapi.com
$path_to_tts = '/var/www/html/bots/ai/tts/';
$q = str_replace('"', '', $q);
$prompt = '{
"prompt": "'.$q.'",
"temperature": 0.99,
"max_tokens":4000
}';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.openai.com/v1/engines/text-davinci-003/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $prompt,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $api_key
),
));
echo 'Prompt: ' . $prompt . PHP_EOL;
$response1 = curl_exec($curl);
// Attempt to decode the response as JSON
$response1 = json_decode($response1, true);
// Check if the "choices" key exists in the array
if(isset($response1) && is_array($response1) && array_key_exists("choices", $response1) && !empty($response1["choices"]) && !empty($response1["choices"][0]) && !empty($response1["choices"][0]["text"])) {
$ai_response = $response1["choices"][0]["text"];
}else{
// set a default value if the key does not exist or $response1 is not set
$ai_response = "";
unlink_mp3();
echo "Error: response1 is not set properly or not an array.";
}
// URL encode the AI response
$ai_response = urlencode($ai_response);
// Execute the cURL request to the VoiceRSS API
curl_setopt_array($curl, [
CURLOPT_URL => "https://voicerss-text-to-speech.p.rapidapi.com/?key=$voicerss_key",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "src=". $ai_response ."&hl=en-us&v=Linda&r=0&c=mp3&f=ulaw_44khz_stereo", // see voicerss.org/api/ for voices and settings
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: voicerss-text-to-speech.p.rapidapi.com",
"X-RapidAPI-Key: $rapid_key",
"content-type: application/x-www-form-urlencoded"
],
]);
echo 'Response: ' . $ai_response . PHP_EOL;
$response = curl_exec($curl);
$random_string = substr(rand(), 0, 6);
$mp3file = fopen($path_to_tts . $random_string . ".mp3", "w");
fwrite($mp3file, $response);
fclose($mp3file);
curl_close($curl);
$filesize = filesize($path_to_tts . $random_string . ".mp3");
if (file_exists($path_to_tts . $random_string . ".mp3") && $filesize > 0) {
if ($filesize <= 1024) {
unlink_mp3();
echo "Error: Generated MP3 file is too small. filesize: " . $filesize . " bytes.";
}
} else {
echo "Error: Generated MP3 file is not created or is of 0 bytes.";
}
}
function unlink_mp3(){
global $path_to_tts;
$files = glob($path_to_tts . '*.mp3'); // get a list of all .mp3 files in the folder
foreach($files as $file){ // iterate through the list
unlink($file); // unlink each file
}
}
?>