omniai 1.0.9 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f4665d73734c57eaf861adb6a941c7e3102c5de00ec4739f29b69123b829a1d
4
- data.tar.gz: '03487dbb919fb7821be77fcbecac4d0adefa5c62d511fffa2ca06d6c3f28c946'
3
+ metadata.gz: 7a31351fb7cc352c108f55c3df8d57020bf2d520b935dce75eb87e62e4a0737c
4
+ data.tar.gz: a8ca504f40836a0072dcf4549767632a19bb6497da53390404fc1399ae264522
5
5
  SHA512:
6
- metadata.gz: 105c7d3af61b4f79b1bdfb15c083ea48cbe92bb6b81bc2ccdccef2f8873ed2b358cbf3a7c3418187a7b2b4d27d0768a5c1a8ce36866d263eb107cee91842011f
7
- data.tar.gz: 4ddad063456e0d1a98c6b21c284fb66ba5ce672f6b4df4f03522ebf3f341269ebecf28607132a84120b9de621b9e5ca65b219b0f072bbf36bc5f4970461458a4
6
+ metadata.gz: 76f22ab7edf30b0f5aedbe071d7a1ff0fd5c1b2a5ab1f5a36273b9041c62a27796a510a4c73fd52f27606ff4a5e8d84fbb46cea03a6949da71ada89cdc62daba
7
+ data.tar.gz: 73d4718a9a593e5c520f48f8a1fa6c06560a2f3447c609f1a85ecbbda9c7d55dc45a6bec9d57de5054b1936be1d21266bbcb3324096a8337db1cf05d7359f09a
data/README.md CHANGED
@@ -111,3 +111,11 @@ stream = proc do |chunk|
111
111
  end
112
112
  client.chat('Tell me a joke.', stream:)
113
113
  ```
114
+
115
+ ### Transcribe
116
+
117
+ Clients that support chat (e.g. OpenAI w/ "Whisper", etc) convert recordings to text via the following calls:
118
+
119
+ ```ruby
120
+ client.transcribe(file.path)
121
+ ```
data/lib/omniai/chat.rb CHANGED
@@ -33,6 +33,10 @@ module OmniAI
33
33
  SYSTEM = 'system'
34
34
  end
35
35
 
36
+ module Format
37
+ JSON = :json
38
+ end
39
+
36
40
  def self.process!(...)
37
41
  new(...).process!
38
42
  end
data/lib/omniai/client.rb CHANGED
@@ -45,5 +45,19 @@ module OmniAI
45
45
  def chat(messages, model:, temperature: nil, format: nil, stream: nil)
46
46
  raise NotImplementedError, "#{self.class.name}#chat undefined"
47
47
  end
48
+
49
+ # @raise [OmniAI::Error]
50
+ #
51
+ # @param file [IO]
52
+ # @param model [String]
53
+ # @param language [String, nil] optional
54
+ # @param prompt [String, nil] optional
55
+ # @param temperature [Float, nil] optional
56
+ # @param format [Symbol] :text, :srt, :vtt, or :json (default)
57
+ #
58
+ # @return text [OmniAI::Transcribe::Transcription]
59
+ def transcribe(file, model:, language: nil, prompt: nil, temperature: nil, format: Transcription::Format::JSON)
60
+ raise NotImplementedError, "#{self.class.name}#speak undefined"
61
+ end
48
62
  end
49
63
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ class Transcribe
5
+ # A transcription returned by the API.
6
+ class Transcription
7
+ attr_accessor :data, :format
8
+
9
+ # @param data [Hash]
10
+ def initialize(data:, format:)
11
+ @data = data
12
+ @format = format
13
+ end
14
+
15
+ # @return [String]
16
+ def text
17
+ @data['text']
18
+ end
19
+
20
+ # @return [String]
21
+ def inspect
22
+ "#<#{self.class} text=#{text.inspect} format=#{format.inspect}>"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ # An abstract class that provides a consistent interface for processing transcribe requests.
5
+ #
6
+ # Usage:
7
+ #
8
+ # class OmniAI::OpenAI::Transcribe < OmniAI::Transcribe
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 Transcribe
28
+ module Language
29
+ AFRIKAANS = 'af'
30
+ ARABIC = 'ar'
31
+ ARMENIAN = 'hy'
32
+ AZERBAIJANI = 'az'
33
+ BELARUSIAN = 'be'
34
+ BOSNIAN = 'bs'
35
+ BULGARIAN = 'bg'
36
+ CATALAN = 'ca'
37
+ CHINESE = 'zh'
38
+ CROATIAN = 'hr'
39
+ CZECH = 'cs'
40
+ DANISH = 'da'
41
+ DUTCH = 'nl'
42
+ ENGLISH = 'en'
43
+ ESTONIAN = 'et'
44
+ FINNISH = 'fi'
45
+ FRENCH = 'fr'
46
+ GALICIAN = 'gl'
47
+ GERMAN = 'de'
48
+ GREEK = 'el'
49
+ HEBREW = 'he'
50
+ HINDI = 'hi'
51
+ HUNGARIAN = 'hu'
52
+ ICELANDIC = 'is'
53
+ INDONESIAN = 'id'
54
+ ITALIAN = 'it'
55
+ JAPANESE = 'ja'
56
+ KANNADA = 'kn'
57
+ KAZAKH = 'kk'
58
+ KOREAN = 'ko'
59
+ LATVIAN = 'lv'
60
+ LITHUANIAN = 'lt'
61
+ MACEDONIAN = 'mk'
62
+ MALAY = 'ms'
63
+ MARATHI = 'mr'
64
+ MAORI = 'mi'
65
+ NEPALI = 'ne'
66
+ NORWEGIAN = 'no'
67
+ PERSIAN = 'fa'
68
+ POLISH = 'pl'
69
+ PORTUGUESE = 'pt'
70
+ ROMANIAN = 'ro'
71
+ RUSSIAN = 'ru'
72
+ SERBIAN = 'sr'
73
+ SLOVAK = 'sk'
74
+ SLOVENIAN = 'sl'
75
+ SPANISH = 'es'
76
+ SWAHILI = 'sw'
77
+ SWEDISH = 'sv'
78
+ TAGALOG = 'tl'
79
+ TAMIL = 'ta'
80
+ THAI = 'th'
81
+ TURKISH = 'tr'
82
+ UKRAINIAN = 'uk'
83
+ URDU = 'ur'
84
+ VIETNAMESE = 'vi'
85
+ WELSH = 'cy'
86
+ end
87
+
88
+ module Format
89
+ JSON = 'json'
90
+ TEXT = 'text'
91
+ VTT = 'vtt'
92
+ SRT = 'srt'
93
+ end
94
+
95
+ def self.process!(...)
96
+ new(...).process!
97
+ end
98
+
99
+ # @param path [String] required
100
+ # @param client [OmniAI::Client] the client
101
+ # @param model [String] required
102
+ # @param language [String, nil] optional
103
+ # @param prompt [String, nil] optional
104
+ # @param temperature [Float, nil] optional
105
+ # @param format [String, nil] optional
106
+ def initialize(path, client:, model:, language: nil, prompt: nil, temperature: nil, format: Format::JSON)
107
+ @path = path
108
+ @model = model
109
+ @language = language
110
+ @prompt = prompt
111
+ @temperature = temperature
112
+ @format = format
113
+ @client = client
114
+ end
115
+
116
+ # @return [OmniAI::Transcribe::Transcription]
117
+ # @raise [ExecutionError]
118
+ def process!
119
+ response = request!
120
+ raise HTTPError, response.flush unless response.status.ok?
121
+
122
+ data = @format.eql?(Format::JSON) ? response.parse : { text: String(response.body) }
123
+ Transcription.new(format: @format, data:)
124
+ end
125
+
126
+ protected
127
+
128
+ # @return [Hash]
129
+ def payload
130
+ {
131
+ file: HTTP::FormData::File.new(@path),
132
+ model: @model,
133
+ language: @language,
134
+ prompt: @prompt,
135
+ temperature: @temperature,
136
+ }.compact
137
+ end
138
+
139
+ # @return [String]
140
+ def path
141
+ raise NotImplementedError, "#{self.class.name}#path undefined"
142
+ end
143
+
144
+ # @return [HTTP::Response]
145
+ def request!
146
+ @client
147
+ .connection
148
+ .accept(@format.eql?(Format::JSON) ? :json : :text)
149
+ .post(path, form: payload)
150
+ end
151
+ end
152
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniAI
4
- VERSION = '1.0.9'
4
+ VERSION = '1.1.0'
5
5
  end
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.0.9
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
@@ -76,6 +76,8 @@ files:
76
76
  - lib/omniai/chat/usage.rb
77
77
  - lib/omniai/client.rb
78
78
  - lib/omniai/config.rb
79
+ - lib/omniai/transcribe.rb
80
+ - lib/omniai/transcribe/transcription.rb
79
81
  - lib/omniai/version.rb
80
82
  homepage: https://github.com/ksylvest/omniai
81
83
  licenses: []