-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathWhisperAudioToTextActionHandler.php
More file actions
88 lines (65 loc) · 2.65 KB
/
Copy pathWhisperAudioToTextActionHandler.php
File metadata and controls
88 lines (65 loc) · 2.65 KB
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
<?php
declare(strict_types=1);
namespace App\AI\Handler;
use App\AI\ActionType\TranscribeAudioActionType;
use Ibexa\Contracts\ConnectorAi\Action\ActionHandlerInterface;
use Ibexa\Contracts\ConnectorAi\Action\DataType\Text;
use Ibexa\Contracts\ConnectorAi\Action\Response\TextResponse;
use Ibexa\Contracts\ConnectorAi\ActionInterface;
use Ibexa\Contracts\ConnectorAi\ActionResponseInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
final class WhisperAudioToTextActionHandler implements ActionHandlerInterface
{
private const string TIMESTAMP_FORMAT = '/^\[\d{2}:\d{2}\.\d{3} --> \d{2}:\d{2}\.\d{3}]\s*/';
public function supports(ActionInterface $action): bool
{
return $action->getActionTypeIdentifier() === TranscribeAudioActionType::IDENTIFIER;
}
public function handle(ActionInterface $action, array $context = []): ActionResponseInterface
{
/** @var \App\AI\DataType\Audio $input */
$input = $action->getInput();
$path = $this->saveInputToFile($input->getBase64());
$arguments = ['whisper'];
$language = $action->getRuntimeContext()?->get('languageCode');
if ($language !== null) {
$arguments[] = sprintf('--language=%s', substr((string) $language, 0, 2));
}
$arguments[] = '--output_format=txt';
$arguments[] = $path;
$process = new Process($arguments);
$process->run();
if (!$process->isSuccessful()) {
unlink($path);
throw new ProcessFailedException($process);
}
$output = $process->getOutput();
$includeTimestamps = $action->getActionContext()
?->getActionTypeOptions()
->get('include_timestamps', false)
?? false;
if (!$includeTimestamps) {
$output = $this->removeTimestamps($output);
}
unlink($path);
return new TextResponse(new Text([$output]));
}
public static function getIdentifier(): string
{
return 'whisper_audio_to_text';
}
private function removeTimestamps(string $text): string
{
$lines = explode(PHP_EOL, $text);
$processedLines = array_map(static fn (string $line): string => preg_replace(self::TIMESTAMP_FORMAT, '', $line) ?? '', $lines);
return implode(PHP_EOL, $processedLines);
}
private function saveInputToFile(string $audioEncodedInBase64): string
{
$filename = uniqid('audio');
$path = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . $filename;
file_put_contents($path, base64_decode($audioEncodedInBase64));
return $path;
}
}