Added video transcription
All checks were successful
Push image to registry / build-image (push) Successful in 5m59s
All checks were successful
Push image to registry / build-image (push) Successful in 5m59s
This commit is contained in:
14
app/Services/FileTools/Transcription/IAudioTranscriptor.php
Normal file
14
app/Services/FileTools/Transcription/IAudioTranscriptor.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\FileTools\Transcription;
|
||||
|
||||
interface IAudioTranscriptor
|
||||
{
|
||||
/**
|
||||
* Perform transcription on the given audio file.
|
||||
*
|
||||
* @param string $filePath The path to the audio file to be transcribed.
|
||||
* @return string The transcribed text from the audio file.
|
||||
*/
|
||||
public function transcribe(string $filePath): ?string;
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\FileTools\Transcription;
|
||||
|
||||
use Log;
|
||||
|
||||
class OpenAIAPIAudioTranscriptor implements IAudioTranscriptor
|
||||
{
|
||||
|
||||
private function getHeaders(): array
|
||||
{
|
||||
return [
|
||||
'Authorization: ' . (config('llm.api.transcription.token') ? 'Bearer ' . config('llm.api.transcription.token') : ''),
|
||||
//'Content-Type: application/json',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function transcribe(string $filePath): ?string
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
Log::error("File not found: {$filePath}");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Make a call to the API with curl
|
||||
// Example of working curl command:
|
||||
// curl -s "SPEACHES_BASE_URL/v1/audio/transcriptions" -F "file=@/home/ninluc/Downloads/memeSalto/m19.mp3" -F "model=MODEL_ID"
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => config('llm.api.transcription.host') . '/audio/transcriptions',
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => [
|
||||
'file' => new \CURLFile($filePath),
|
||||
'model' => config('llm.models.transcription.name'),
|
||||
],
|
||||
CURLOPT_HTTPHEADER => $this->getHeaders(),
|
||||
]);
|
||||
$response = curl_exec($curl);
|
||||
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
curl_close($curl);
|
||||
if ($httpCode !== 200) {
|
||||
Log::error("Error during transcription: HTTP code {$httpCode} : {$response}");
|
||||
return null;
|
||||
}
|
||||
$responseData = json_decode($response, true);
|
||||
return $responseData['text'] ?? null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user