omniai 1.1.4 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -4
- data/lib/omniai/chat.rb +1 -1
- data/lib/omniai/client.rb +21 -0
- data/lib/omniai/speak.rb +138 -0
- data/lib/omniai/transcribe.rb +1 -1
- data/lib/omniai/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e94ad519b08013feb50cd317eaed5f66188929d02c6c03f25bd19e3fe5b4f3a9
|
4
|
+
data.tar.gz: 988e4191a7928a785f24e277e56683a8afb3253a6e90fb42e0cf8e52d9996990
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e6599fd2d0d6d88710b684ee92e19e638dd9aa0ae71d60ca05e710a4ea998e39e8429a6e10c6b7f179d1957f16e60d5c0a8f614bbb36bce847a28373f87daed
|
7
|
+
data.tar.gz: 326eedd109e5cb0cca4ea758bd02bf6ce51eeae8da9be58a5f892bea20e951155af34b326ecafdd3c0d46a3dee04aa375fa48f634af28d7b09f1677df9553d3a
|
data/README.md
CHANGED
@@ -96,7 +96,7 @@ client.chat('Tell me a joke.', stream:)
|
|
96
96
|
|
97
97
|
### Transcribe
|
98
98
|
|
99
|
-
Clients that support
|
99
|
+
Clients that support transcribe (e.g. OpenAI w/ "Whisper") convert recordings to text via the following calls:
|
100
100
|
|
101
101
|
#### Transcriptions with Path
|
102
102
|
|
@@ -108,7 +108,30 @@ transcription.text # '...'
|
|
108
108
|
#### Transcriptions with Files
|
109
109
|
|
110
110
|
```ruby
|
111
|
-
|
112
|
-
transcription = client.transcribe(file)
|
113
|
-
transcription.text # '...'
|
111
|
+
File.open("example.ogg", "rb") do |file|
|
112
|
+
transcription = client.transcribe(file)
|
113
|
+
transcription.text # '...'
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
### Speak
|
118
|
+
|
119
|
+
Clients that support speak (e.g. OpenAI w/ "Whisper") convert text to recordings via the following calls:
|
120
|
+
|
121
|
+
#### Speech with Stream
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
File.open('example.ogg', 'wb') do |file|
|
125
|
+
client.speak('The quick brown fox jumps over a lazy dog.', voice: 'HAL') do |chunk|
|
126
|
+
file << chunk
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
#### Speech with File
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
tempfile = client.speak('The quick brown fox jumps over a lazy dog.', voice: 'HAL')
|
135
|
+
tempfile.close
|
136
|
+
tempfile.unlink
|
114
137
|
```
|
data/lib/omniai/chat.rb
CHANGED
data/lib/omniai/client.rb
CHANGED
@@ -59,5 +59,26 @@ module OmniAI
|
|
59
59
|
def transcribe(io, model:, language: nil, prompt: nil, temperature: nil, format: nil)
|
60
60
|
raise NotImplementedError, "#{self.class.name}#speak undefined"
|
61
61
|
end
|
62
|
+
|
63
|
+
# @raise [OmniAI::Error]
|
64
|
+
#
|
65
|
+
# @param input [String] required
|
66
|
+
# @param model [String] required
|
67
|
+
# @param voice [String] required
|
68
|
+
# @param speed [Float] optional
|
69
|
+
# @param format [String] optional (default "aac"):
|
70
|
+
# - "aac"
|
71
|
+
# - "mp3"
|
72
|
+
# - "flac"
|
73
|
+
# - "opus"
|
74
|
+
# - "pcm"
|
75
|
+
# - "wav"
|
76
|
+
#
|
77
|
+
# @yield [output] optional
|
78
|
+
#
|
79
|
+
# @return [Tempfile``]
|
80
|
+
def speak(input, model:, voice:, speed: nil, format: nil, &stream)
|
81
|
+
raise NotImplementedError, "#{self.class.name}#speak undefined"
|
82
|
+
end
|
62
83
|
end
|
63
84
|
end
|
data/lib/omniai/speak.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
# An abstract class that provides a consistent interface for processing speak requests.
|
5
|
+
#
|
6
|
+
# Usage:
|
7
|
+
#
|
8
|
+
# class OmniAI::OpenAI::Speak < OmniAI::Speakw
|
9
|
+
# module Model
|
10
|
+
# WHISPER_1 = "whisper-1"
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# protected
|
14
|
+
#
|
15
|
+
# # @return [Hash]
|
16
|
+
# def payload
|
17
|
+
# raise NotImplementedError, "#{self.class.name}#payload undefined"
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# # @return [String]
|
21
|
+
# def path
|
22
|
+
# raise NotImplementedError, "#{self.class.name}#path undefined"
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# client.transcribe(File.open("..."), model: "...", format: :json)
|
27
|
+
class Speak
|
28
|
+
module Format
|
29
|
+
AAC = 'aac'
|
30
|
+
FLAC = 'flac'
|
31
|
+
MP3 = 'mp3'
|
32
|
+
OPUS = 'opus'
|
33
|
+
PCM = 'pcm'
|
34
|
+
WAV = 'wav'
|
35
|
+
end
|
36
|
+
|
37
|
+
# @raise [HTTPError]
|
38
|
+
#
|
39
|
+
# @param client [OmniAI::Client] required
|
40
|
+
# @param input [String] required
|
41
|
+
# @param model [String] required
|
42
|
+
# @param voice [String] required
|
43
|
+
# @param speed [Float] optional
|
44
|
+
# @param format [String] optional (default "aac"):
|
45
|
+
# - "aac"
|
46
|
+
# - "mp3"
|
47
|
+
# - "flac"
|
48
|
+
# - "opus"
|
49
|
+
# - "pcm"
|
50
|
+
# - "wav"
|
51
|
+
#
|
52
|
+
# @yield [chunk]
|
53
|
+
#
|
54
|
+
# @return [Tempfile]
|
55
|
+
def self.process!(input, client:, model:, voice:, speed: nil, format: nil, &)
|
56
|
+
new(input, client:, model:, voice:, speed:, format:).process!(&)
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param client [OmniAI::Client] required
|
60
|
+
# @param input [String] required
|
61
|
+
# @param model [String] required
|
62
|
+
# @param voice [String] required
|
63
|
+
# @param speed [Float] optional
|
64
|
+
# @param format [String] optional (default "aac"):
|
65
|
+
# - "aac"
|
66
|
+
# - "mp3"
|
67
|
+
# - "flac"
|
68
|
+
# - "opus"
|
69
|
+
# - "pcm"
|
70
|
+
# - "wav"
|
71
|
+
def initialize(input, client:, model:, voice:, speed: nil, format: nil)
|
72
|
+
@input = input
|
73
|
+
@client = client
|
74
|
+
@model = model
|
75
|
+
@voice = voice
|
76
|
+
@speed = speed
|
77
|
+
@format = format
|
78
|
+
end
|
79
|
+
|
80
|
+
# @raise [HTTPError]
|
81
|
+
#
|
82
|
+
# @yield [chunk]
|
83
|
+
#
|
84
|
+
# @return [Tempfile]
|
85
|
+
def process!(&block)
|
86
|
+
response = request!
|
87
|
+
raise HTTPError, response.flush unless response.status.ok?
|
88
|
+
|
89
|
+
if block
|
90
|
+
stream!(response:, &block)
|
91
|
+
else
|
92
|
+
fetch!(response:)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
# @param response [HTTP::Response]
|
99
|
+
#
|
100
|
+
# @yield [chunk]
|
101
|
+
def stream!(response:, &block)
|
102
|
+
response.body.each { |chunk| block.call(chunk) }
|
103
|
+
end
|
104
|
+
|
105
|
+
# @param response [HTTP::Response]
|
106
|
+
#
|
107
|
+
# @return [Tempfile]
|
108
|
+
def fetch!(response:)
|
109
|
+
tempfile = Tempfile.new
|
110
|
+
tempfile.binmode
|
111
|
+
response.body.each { |chunk| tempfile << chunk }
|
112
|
+
tempfile.rewind
|
113
|
+
tempfile
|
114
|
+
end
|
115
|
+
|
116
|
+
# @return [Hash]
|
117
|
+
def payload
|
118
|
+
{
|
119
|
+
model: @model,
|
120
|
+
voice: @voice,
|
121
|
+
input: @input,
|
122
|
+
speed: @speed,
|
123
|
+
}.compact
|
124
|
+
end
|
125
|
+
|
126
|
+
# @return [String]
|
127
|
+
def path
|
128
|
+
raise NotImplementedError, "#{self.class.name}#path undefined"
|
129
|
+
end
|
130
|
+
|
131
|
+
# @return [HTTP::Response]
|
132
|
+
def request!
|
133
|
+
@client
|
134
|
+
.connection
|
135
|
+
.post(path, json: payload)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
data/lib/omniai/transcribe.rb
CHANGED
@@ -113,8 +113,8 @@ module OmniAI
|
|
113
113
|
@client = client
|
114
114
|
end
|
115
115
|
|
116
|
+
# @raise [HTTPError]
|
116
117
|
# @return [OmniAI::Transcribe::Transcription]
|
117
|
-
# @raise [ExecutionError]
|
118
118
|
def process!
|
119
119
|
response = request!
|
120
120
|
raise HTTPError, response.flush unless response.status.ok?
|
data/lib/omniai/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/omniai/chat/usage.rb
|
77
77
|
- lib/omniai/client.rb
|
78
78
|
- lib/omniai/config.rb
|
79
|
+
- lib/omniai/speak.rb
|
79
80
|
- lib/omniai/transcribe.rb
|
80
81
|
- lib/omniai/transcribe/transcription.rb
|
81
82
|
- lib/omniai/version.rb
|