blib/lib/Langertha/Role/Transcription.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 73.0% 19 / 26 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 93.0% 133 / 143 CC 17 SCAR 16.2
CC
17
Cov
92%
transcription_file_part
11.1
_build_transcription_model
3.5
simple_transcription_result
2.0
CRAP 5.1
« blib/lib/Langertha/Reasoning/Profile.pm ↑ Summary blib/lib/Langertha/Engine/Groq.pm »
1 package Langertha::Role::Transcription;
2 # ABSTRACT: Role for APIs with transcription functionality
3 106 our $VERSION = '0.503';
4 106 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 106 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 106 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 106 ▶use Scalar::Util qw( openhandle );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 106 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 106 ▶use Langertha::CallResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The _f methods send through the engine's async backend (k292): injected
12 # client > Net::Async::HTTP > the sync LWP shim (ADR 0027).
13 106 with 'Langertha::Role::AsyncHTTP';
14  
15 106 requires qw(
16 transcription_request
17 transcription_response
18 );
19  
20 106 has transcription_model => (
21 is => 'ro',
22 isa => 'Str',
23 lazy_build => 1,
24 );
25 40 ▶sub _build_transcription_model {
Subroutine _build_transcription_model: called CC=3 SCAR=12.5
26 40 my ( $self ) = @_;
27 40 ▶ croak "".(ref $self)." can't handle models!" unless $self->does('Langertha::Role::Models');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->does("Langertha::Role::Models") 0 40 40
28 40 ▶ return $self->default_transcription_model if $self->can('default_transcription_model');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("default_transcription_model") 40 0 40
29 0 return $self->model;
30 }
31  
32  
33 62 ▶sub transcription_file_part {
Subroutine transcription_file_part: called CC=11 SCAR=24.0
Pod documented
34 62 my ( $self, $input, $filename ) = @_;
35 62 ▶ croak "".(ref $self).": no audio given for transcription" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 62 62
36 62 my $bytes;
37 62 ▶ if ( ref $input eq 'SCALAR' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $input eq 'SCALAR') { } 28 34 62
elsif (&openhandle($input)) { } 1 33 34
elsif (not ref $input and index($input, "\000") != -1) { } 1 32 33
38 28 $bytes = $$input;
39 }
40 elsif ( openhandle($input) ) {
41 1 binmode $input;
42 1 local $/;
43 1 $bytes = readline $input;
44 1 ▶ croak "".(ref $self).": cannot read audio from filehandle: $!" unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 1 1
45 }
46 ▶ elsif ( !ref $input && index( $input, "\0" ) >= 0 ) {
Condition100%3/3
not ref $input and index($input, "\0") != -1
!ll&&!rl&&r
2841
MC/DC100%2/2
not ref $input and index($input, "\0") != -1
not ref $input index($input, "\0") != -1
Truth table100%3/3
not(A) and B
A= not ref $inputB= index($input, "\0") != -1
ABresult
0X0
100
111
47 # No path contains a NUL byte, and nearly every audio container header
48 # does (RIFF sizes, ID3, OggS, fLaC) -- so this is content, and it never
49 # reaches open() or a file test. -- karr k287
50 1 $bytes = $input;
51 }
52 else {
53 32 ▶ return [ $input, defined $filename ? ( $filename ) : () ];
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 1 31 32
54 }
55 30 ▶ croak "".(ref $self).": audio content must be bytes, not a character string"
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1) 1 29 30
Condition66%2/3
utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1)
!ll&&!rl&&r
2901
MC/DC50%1/2
utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1)
utf8::is_utf8($bytes) not utf8::downgrade($bytes, 1)
Truth table66%2/3
A and not(B)
A= utf8::is_utf8($bytes)B= not utf8::downgrade($bytes, 1)
ABresult
0X0
100
111
56 if utf8::is_utf8($bytes) && !utf8::downgrade( $bytes, 1 );
57 29 ▶ return [ undef, $filename // 'audio',
Condition100%2/2
$filename // "audio"
l!l
281
MC/DC100%1/1
$filename // "audio"
$filename
Truth table100%2/2
A
A= $filename
Aresult
11
00
58 'Content-Type' => 'application/octet-stream', Content => $bytes ];
59 }
60  
61  
62 62 ▶sub transcription {
Subroutine transcription: called CC=1 SCAR=0.0
Pod documented
63 62 my ( $self, $file_or_content, %extra ) = @_;
64 62 return $self->transcription_request($file_or_content, %extra);
65 }
66  
67  
68 3 ▶sub simple_transcription {
Subroutine simple_transcription: called CC=1 SCAR=0.0
Pod documented
69 3 my ( $self, $file_or_content, %extra ) = @_;
70 3 my $request = $self->transcription($file_or_content, %extra);
71 3 my $response = $self->user_agent->request($request);
72 3 return $request->response_call->($response);
73 }
74  
75  
76 9 ▶async sub simple_transcription_f {
Subroutine simple_transcription_f: called CC=1 SCAR=0.0
Pod documented
77 9 my ( $self, $file_or_content, %extra ) = @_;
78 9 my $request = $self->transcription($file_or_content, %extra);
79 9 my $response = await $self->_async_do_request_f( request => $request );
80 8 return $request->response_call->($response);
81 106 }
82  
83  
84 3 ▶sub simple_transcription_result {
Subroutine simple_transcription_result: called CC=2 SCAR=7.0
Pod documented
85 3 my ( $self, $file_or_content, %extra ) = @_;
86 3 ▶ croak "".(ref $self)." has no transcription_result" unless $self->can('transcription_result');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("transcription_result") 0 3 3
87 3 my $request = $self->transcription($file_or_content, %extra);
88 3 return $self->transcription_result( $self->user_agent->request($request) );
89 }
90  
91  
92 7 ▶async sub simple_transcription_result_f {
Subroutine simple_transcription_result_f: called CC=2 SCAR=7.0
Pod documented
93 7 my ( $self, $file_or_content, %extra ) = @_;
94 7 ▶ croak "".(ref $self)." has no transcription_result" unless $self->can('transcription_result');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("transcription_result") 0 7 7
95 7 my $request = $self->transcription($file_or_content, %extra);
96 7 my $response = await $self->_async_do_request_f( request => $request );
97 6 return $self->transcription_result($response);
98 106 }
99  
100  
101 1 ▶sub simple_transcription_call {
Subroutine simple_transcription_call: called CC=2 SCAR=7.0
Pod documented
102 1 my ( $self, $file_or_content, %extra ) = @_;
103 1 ▶ my $model = exists $extra{model} ? $extra{model} : $self->transcription_model;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'model'} ? : 0 1 1
104 1 my $request = $self->transcription($file_or_content, %extra);
105 1 my $t0 = [gettimeofday];
106 1 my $response = $self->user_agent->request($request);
107 1 my $elapsed = tv_interval($t0);
108 1 my $value = $request->response_call->($response);
109 1 return Langertha::CallResult->from_http_response( $self, $response,
110 value => $value, model => $model, total_seconds => $elapsed );
111 }
112  
113  
114 6 ▶async sub simple_transcription_call_f {
Subroutine simple_transcription_call_f: called CC=2 SCAR=6.9
Pod documented
115 6 my ( $self, $file_or_content, %extra ) = @_;
116 6 ▶ my $model = exists $extra{model} ? $extra{model} : $self->transcription_model;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'model'} ? : 2 4 6
117 6 my $request = $self->transcription($file_or_content, %extra);
118 6 my $t0 = [gettimeofday];
119 6 my $response = await $self->_async_do_request_f( request => $request );
120 6 my $elapsed = tv_interval($t0);
121 6 my $value = $request->response_call->($response);
122 6 return Langertha::CallResult->from_http_response( $self, $response,
123 value => $value, model => $model, total_seconds => $elapsed );
124 106 }
125  
126  
127  
128 106 1;
129  
130 __END__
131  
132 =pod
133  
134 =encoding UTF-8
135  
136 =head1 NAME
137  
138 Langertha::Role::Transcription - Role for APIs with transcription functionality
139  
140 =head1 VERSION
141  
142 version 0.503
143  
144 =head2 transcription_model
145  
146 The model name to use for transcription requests. Lazily defaults to
147 C<default_transcription_model> if the engine provides it, otherwise falls back
148 to the general C<model> attribute from L<Langertha::Role::Models>.
149  
150 =head2 transcription_file_part
151  
152 my $part = $engine->transcription_file_part($audio, $filename);
153  
154 Turns the audio argument of L</transcription> into the multipart file part.
155 C<$audio> is one of:
156  
157 =over
158  
159 =item * a path to the audio file (a plain string);
160  
161 =item * a reference to a scalar holding the audio bytes: C<\$bytes>;
162  
163 =item * an open filehandle, read to the end in binary mode;
164  
165 =item * a plain string that contains a NUL byte, taken as the audio bytes
166 (no path can contain one). Prefer C<\$bytes>: a short content without a NUL
167 would be taken as a path.
168  
169 =back
170  
171 C<$filename> is the name sent with the part; it defaults to the basename of
172 the path, and to C<audio> for in-memory content. Hosted APIs (OpenAI, Groq)
173 detect the format from the filename's extension, so pass one such as
174 C<speech.mp3> with in-memory audio.
175  
176 =head2 transcription
177  
178 my $request = $engine->transcription($audio, %extra);
179 my $request = $engine->transcription(\$bytes, filename => 'speech.mp3');
180  
181 Builds and returns a transcription HTTP request object for the given audio:
182 a path, C<\$bytes>, or a filehandle (see L</transcription_file_part>).
183 C<filename> in C<%extra> sets the uploaded filename; the other C<%extra> pairs
184 are sent as form fields. Use L</simple_transcription> to execute the request
185 and get the transcript directly.
186  
187 =head2 simple_transcription
188  
189 my $text = $engine->simple_transcription($audio, %extra);
190 my $text = $engine->simple_transcription('/path/to/audio.mp3');
191 my $text = $engine->simple_transcription(\$audio_bytes,
192 filename => 'audio.mp3', language => 'en');
193  
194 Sends a transcription request for the audio (a path, C<\$bytes> or a
195 filehandle, see L</transcription_file_part>) and returns the transcript text. Blocks until the request completes. Additional options such as
196 C<language> can be passed as C<%extra> key/value pairs.
197 L</simple_transcription_f> is the non-blocking variant.
198  
199 =head2 simple_transcription_f
200  
201 my $text = await $engine->simple_transcription_f('/path/to/audio.mp3');
202 my $text = await $engine->simple_transcription_f(\$audio_bytes,
203 filename => 'audio.mp3', language => 'en');
204  
205 Async variant of L</simple_transcription>: same arguments, returns a
206 L<Future> that resolves to the transcript text and fails with the same error
207 text. The multipart upload goes through the engine's async backend
208 (L<Langertha::Role::AsyncHTTP>), so L<Langertha::Role::HTTP/user_agent_timeout>
209 bounds it on L<Net::Async::HTTP> too; without that module it runs
210 synchronously over LWP. The audio is read into the request body when the
211 call is made.
212  
213 =head2 simple_transcription_result
214  
215 my $result = $engine->simple_transcription_result('/path/to/audio.mp3',
216 response_format => 'verbose_json',
217 'timestamp_granularities[]' => [qw( word segment )],
218 );
219 say $_->{word}, ' @ ', $_->{start} for @{ $result->{words} };
220  
221 Like L</simple_transcription>, but returns the whole parsed answer as a
222 HashRef (see L<Langertha::Role::OpenAICompatible/transcription_result>)
223 instead of only the text. C<verbose_json> and word timestamps need a model
224 that offers them (C<whisper-1>, Groq, Whisper servers); OpenAI's default
225 C<gpt-transcribe> answers C<json> only.
226  
227 =head2 simple_transcription_result_f
228  
229 my $result = await $engine->simple_transcription_result_f($audio,
230 response_format => 'verbose_json');
231  
232 Async variant of L</simple_transcription_result>: returns a L<Future> that
233 resolves to the parsed answer as a HashRef, like L</simple_transcription_f>
234 does for the text.
235  
236 =head2 simple_transcription_call
237  
238 my $result = $engine->simple_transcription_call('/path/to/audio.mp3');
239 say $result->value; # the transcript text
240 say $result->usage->input_tokens if $result->has_usage;
241 my $segments = $result->raw->{segments}; # verbose_json
242  
243 Like L</simple_transcription>, but returns a L<Langertha::CallResult>: the
244 transcript text as C<value>, plus the provider's token C<usage> (OpenAI's
245 C<gpt-transcribe>), this response's C<rate_limit>, the model and the measured
246 C<total_seconds>. A JSON answer is kept whole in C<raw> (C<segments>,
247 C<words>, C<duration>, a duration-billed C<usage>).
248  
249 It is not called C<simple_transcription_result> because that name already
250 returns the parsed answer as a HashRef, and keeps doing so.
251  
252 =head2 simple_transcription_call_f
253  
254 my $result = await $engine->simple_transcription_call_f(\$bytes,
255 filename => 'speech.mp3');
256  
257 Async variant of L</simple_transcription_call>, sent like
258 L</simple_transcription_f>: resolves to the L<Langertha::CallResult> and fails
259 with the same error text.
260  
261 =head1 SEE ALSO
262  
263 =over
264  
265 =item * L<Langertha::Role::HTTP> - HTTP transport layer
266  
267 =item * L<Langertha::Role::Models> - Model selection (provides C<transcription_model>)
268  
269 =item * L<Langertha::Engine::Whisper> - Whisper-compatible transcription server
270  
271 =item * L<Langertha::Engine::Groq> - Groq's hosted Whisper transcription
272  
273 =back
274  
275 =head1 SUPPORT
276  
277 =head2 Issues
278  
279 Please report bugs and feature requests on GitHub at
280 L<https://github.com/Getty/langertha/issues>.
281  
282 =head2 IRC
283  
284 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
285  
286 =head1 CONTRIBUTING
287  
288 Contributions are welcome! Please fork the repository and submit a pull request.
289  
290 =head1 AUTHOR
291  
292 Torsten Raudssus <getty@cpan.org>
293  
294 =head1 COPYRIGHT AND LICENSE
295  
296 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
297  
298 This is free software; you can redistribute it and/or modify it under
299 the same terms as the Perl 5 programming language system itself.
300  
301 =cut
« blib/lib/Langertha/Reasoning/Profile.pm ↑ Summary blib/lib/Langertha/Engine/Groq.pm »