Skip to content

Commit

Permalink
Better describe the transcription
Browse files Browse the repository at this point in the history
  • Loading branch information
ksylvest committed Jun 17, 2024
1 parent 4c7dd78 commit fe59651
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
omniai (1.1.1)
omniai (1.1.2)
event_stream_parser
http
zeitwerk
Expand Down
4 changes: 2 additions & 2 deletions lib/omniai/transcribe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def process!
response = request!
raise HTTPError, response.flush unless response.status.ok?

data = @format.nil? || @format.eql?(Format::JSON) ? response.parse : { text: String(response.body) }
Transcription.new(format: @format, data:)
text = @format.nil? || @format.eql?(Format::JSON) ? response.parse['text'] : String(response.body)
Transcription.new(text:, model: @model, format: @format)
end

protected
Expand Down
18 changes: 8 additions & 10 deletions lib/omniai/transcribe/transcription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ module OmniAI
class Transcribe
# A transcription returned by the API.
class Transcription
attr_accessor :data, :format
attr_accessor :text, :model, :format

# @param data [Hash]
def initialize(data:, format:)
@data = data
# @param text [String]
# @param model [String]
# @param format [String]
def initialize(text:, model:, format:)
@text = text
@model = model
@format = format
end

# @return [String]
def text
@data['text']
end

# @return [String]
def inspect
"#<#{self.class} text=#{text.inspect} format=#{format.inspect}>"
"#<#{self.class} text=#{text.inspect}>"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/omniai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module OmniAI
VERSION = '1.1.1'
VERSION = '1.1.2'
end
16 changes: 10 additions & 6 deletions spec/omniai/transcribe/transcription_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# frozen_string_literal: true

RSpec.describe OmniAI::Transcribe::Transcription do
subject(:transcription) { described_class.new(format: OmniAI::Transcribe::Format::JSON, data: { 'text' => 'Hi!' }) }

describe '#format' do
it { expect(transcription.format).to eq('json') }
end
subject(:transcription) { described_class.new(text: 'Hi!', model: 'whisper', format: 'text') }

describe '#text' do
it { expect(transcription.text).to eq('Hi!') }
end

describe '#model' do
it { expect(transcription.model).to eq('whisper') }
end

describe '#format' do
it { expect(transcription.format).to eq('text') }
end

describe '#inspect' do
it { expect(transcription.inspect).to eq('#<OmniAI::Transcribe::Transcription text="Hi!" format="json">') }
it { expect(transcription.inspect).to eq('#<OmniAI::Transcribe::Transcription text="Hi!">') }
end
end
16 changes: 15 additions & 1 deletion spec/omniai/transcribe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def path
end

describe '.process!' do
subject(:process!) { FakeTranscribe.process!(path, client:, model:) }
subject(:process!) { FakeTranscribe.process!(path, client:, model:, format:) }

let(:format) { described_class::Format::JSON }

let(:client) { FakeClient.new(api_key: '...') }
let(:model) { FakeTranscribe::Model::FAKE }
Expand All @@ -53,5 +55,17 @@ def path

it { expect { process! }.to raise_error(OmniAI::HTTPError) }
end

context 'when OK with a non-JSON format' do
before do
stub_request(:post, 'http://localhost:8080/transcribe')
.to_return(status: 200, body: 'Hi!')
end

let(:format) { described_class::Format::TEXT }

it { expect(process!).to be_a(OmniAI::Transcribe::Transcription) }
it { expect(process!.text).to eq('Hi!') }
end
end
end

0 comments on commit fe59651

Please sign in to comment.