cloudflare-ai 0.7.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -13
- data/lib/cloudflare/ai/client.rb +12 -1
- data/lib/cloudflare/ai/clients/{image_helpers.rb → media_helpers.rb} +10 -1
- data/lib/cloudflare/ai/models.rb +3 -3
- data/lib/cloudflare/ai/results/automatic_speech_recognition.rb +13 -0
- data/lib/cloudflare/ai/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08feb777620d0767e6ab08f5366c9a207facc25783f9fe497f381e894e02876c'
|
4
|
+
data.tar.gz: cacc6945ed2914fa62280e5afd8a0695f7c108139737e3785cbc74a24d26b79b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31b20441f2791a3c93a8ccde7d5674e2daaf8190cd348b2c941849b84edc6f04fcef9bfd2a3ef6de8e4c39c3a9d2f03e3388a0f8efd6fc6391ca83fafee111d5
|
7
|
+
data.tar.gz: b695c2971236bb8aa182fe8c36bd63fd9c7303dfda70aee767a098532fbb274e720ed165e73515800b3b64a6a77d5e124b3048dcc2100bff7e039b0245577bbf
|
data/README.md
CHANGED
@@ -14,18 +14,14 @@ generation to make legal services more accessible. [Email me](mailto:cloudflare-
|
|
14
14
|
|
15
15
|
If you're looking for legal help, it's best to book a slot via https://www.krishnan.ca.
|
16
16
|
|
17
|
-
#
|
18
|
-
|
19
|
-
* [x]
|
20
|
-
* [x]
|
21
|
-
* [ ]
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
* [x] [Translation](https://developers.cloudflare.com/workers-ai/models/translation/)
|
26
|
-
* [x] [Image Classification](https://developers.cloudflare.com/workers-ai/models/image-classification/)
|
27
|
-
* [x] [Text-to-Image](https://developers.cloudflare.com/workers-ai/models/text-to-image/)
|
28
|
-
* [ ] [Automatic Speech Recognition](https://developers.cloudflare.com/workers-ai/models/speech-recognition/)
|
17
|
+
# Supported features
|
18
|
+
* [x] [Text Generation](https://developers.cloudflare.com/workers-ai/models/text-generation/)
|
19
|
+
* [x] [Text Embeddings](https://developers.cloudflare.com/workers-ai/models/text-embeddings/)
|
20
|
+
* [x] [Text Classification](https://developers.cloudflare.com/workers-ai/models/text-classification/)
|
21
|
+
* [x] [Translation](https://developers.cloudflare.com/workers-ai/models/translation/)
|
22
|
+
* [x] [Image Classification](https://developers.cloudflare.com/workers-ai/models/image-classification/)
|
23
|
+
* [x] [Text-to-Image](https://developers.cloudflare.com/workers-ai/models/text-to-image/)
|
24
|
+
* [x] [Automatic Speech Recognition](https://developers.cloudflare.com/workers-ai/models/speech-recognition/)
|
29
25
|
|
30
26
|
# Table of Contents
|
31
27
|
|
@@ -180,9 +176,23 @@ All invocations of the `draw` method returns a `Cloudflare::AI::Results::TextToI
|
|
180
176
|
result = client.translate(text: "Hello Jello", source_lang: "en", target_lang: "fr")
|
181
177
|
p result.translated_text # => Hola Jello
|
182
178
|
```
|
179
|
+
#### Result object
|
180
|
+
All invocations of the `translate` method returns a `Cloudflare::AI::Results::Translate`.
|
181
|
+
|
183
182
|
|
183
|
+
### Automatic speech recognition
|
184
|
+
You can pass either a URL (source_url:) or a file (audio:) to the `transcribe` method.
|
185
|
+
```ruby
|
186
|
+
result = client.transcribe(source_url: "http://example.org/path/to/audio.wav")
|
187
|
+
p result.text # => "Hello Jello."
|
188
|
+
p result.word_count # => 2
|
189
|
+
p result.to_json # => {"result":{"text":"Hello Jello.","word_count":2,"words":[{"word":"Hello","start":0,"end":1.340000033378601},{"word":"Jello.","start":1.340000033378601,"end":1.340000033378601}},"success":true,"errors":[],"messages":[]}
|
190
|
+
|
191
|
+
result = client.transcribe(audio: File.open("/path/to/audio.wav"))
|
192
|
+
# ...
|
193
|
+
```
|
184
194
|
#### Result object
|
185
|
-
All invocations of the `
|
195
|
+
All invocations of the `transcribe` method returns a `Cloudflare::AI::Results::Transcribe`.
|
186
196
|
|
187
197
|
# Logging
|
188
198
|
|
data/lib/cloudflare/ai/client.rb
CHANGED
@@ -2,7 +2,7 @@ require "event_stream_parser"
|
|
2
2
|
require "faraday"
|
3
3
|
|
4
4
|
class Cloudflare::AI::Client
|
5
|
-
include Cloudflare::AI::Clients::
|
5
|
+
include Cloudflare::AI::Clients::MediaHelpers
|
6
6
|
include Cloudflare::AI::Clients::TextGenerationHelpers
|
7
7
|
|
8
8
|
attr_reader :url, :account_id, :api_token
|
@@ -59,6 +59,17 @@ class Cloudflare::AI::Client
|
|
59
59
|
Cloudflare::AI::Results::TextEmbedding.new(connection.post(url, payload).body)
|
60
60
|
end
|
61
61
|
|
62
|
+
def transcribe(source_url: nil, audio: nil, model_name: Cloudflare::AI::Models.automatic_speech_recognition.first)
|
63
|
+
raise ArgumentError, "Must provide either audio_url or audio" if [source_url, audio].compact.size != 1
|
64
|
+
|
65
|
+
audio = download_audio(source_url) if source_url
|
66
|
+
|
67
|
+
url = service_url_for(account_id: account_id, model_name: model_name)
|
68
|
+
response = post_request_with_binary_file(url, audio)
|
69
|
+
|
70
|
+
Cloudflare::AI::Results::AutomaticSpeechRecognition.new(response.body)
|
71
|
+
end
|
72
|
+
|
62
73
|
def translate(text:, target_lang:, source_lang: "en", model_name: Cloudflare::AI::Models.translation.first)
|
63
74
|
url = service_url_for(account_id: account_id, model_name: model_name)
|
64
75
|
payload = {text: text, target_lang: target_lang, source_lang: source_lang}.to_json
|
@@ -3,9 +3,18 @@ require "faraday/multipart"
|
|
3
3
|
module Cloudflare
|
4
4
|
module AI
|
5
5
|
module Clients
|
6
|
-
module
|
6
|
+
module MediaHelpers
|
7
7
|
private
|
8
8
|
|
9
|
+
def download_audio(source_url)
|
10
|
+
download_result = Faraday.new(source_url).get
|
11
|
+
binary_file = Tempfile.new(["cloudflare-ai-automatic-speech-recognition", ".wav"])
|
12
|
+
binary_file.binmode
|
13
|
+
binary_file.write(download_result.body)
|
14
|
+
binary_file.rewind
|
15
|
+
binary_file
|
16
|
+
end
|
17
|
+
|
9
18
|
def post_request_with_binary_file(url, file)
|
10
19
|
connection.post do |req|
|
11
20
|
req.url url
|
data/lib/cloudflare/ai/models.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
class Cloudflare::AI::Models
|
2
2
|
class << self
|
3
3
|
def text_generation
|
4
|
-
%w[@cf/meta/llama-2-7b-chat-fp16 @cf/meta/llama-2-7b-chat-int8 @
|
4
|
+
%w[@hf/thebloke/llamaguard-7b-awq @hf/thebloke/neural-chat-7b-v3-1-awq @cf/meta/llama-2-7b-chat-fp16 @cf/mistral/mistral-7b-instruct-v0.1 @hf/thebloke/codellama-7b-instruct-awq @cf/meta/llama-2-7b-chat-int8 @hf/thebloke/mistral-7b-instruct-v0.1-awq @hf/thebloke/deepseek-coder-6.7b-base-awq @hf/thebloke/openhermes-2.5-mistral-7b-awq @hf/thebloke/deepseek-coder-6.7b-instruct-awq @hf/thebloke/zephyr-7b-beta-awq]
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
7
|
+
def automatic_speech_recognition
|
8
8
|
%w[@cf/openai/whisper]
|
9
9
|
end
|
10
10
|
|
@@ -31,7 +31,7 @@ class Cloudflare::AI::Models
|
|
31
31
|
def all
|
32
32
|
{
|
33
33
|
text_generation: text_generation,
|
34
|
-
|
34
|
+
automatic_speech_recognition: automatic_speech_recognition,
|
35
35
|
translation: translation,
|
36
36
|
text_classification: text_classification,
|
37
37
|
image_classification: image_classification,
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Cloudflare::AI::Results::AutomaticSpeechRecognition < Cloudflare::AI::Result
|
2
|
+
def text
|
3
|
+
result&.dig(:text) # nil if no shape
|
4
|
+
end
|
5
|
+
|
6
|
+
def word_count
|
7
|
+
result&.dig(:word_count) # nil if no shape
|
8
|
+
end
|
9
|
+
|
10
|
+
def words
|
11
|
+
result&.dig(:words) # nil if no shape
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloudflare-ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ajay Krishnan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -107,12 +107,13 @@ files:
|
|
107
107
|
- README.md
|
108
108
|
- lib/cloudflare/ai.rb
|
109
109
|
- lib/cloudflare/ai/client.rb
|
110
|
-
- lib/cloudflare/ai/clients/
|
110
|
+
- lib/cloudflare/ai/clients/media_helpers.rb
|
111
111
|
- lib/cloudflare/ai/clients/text_generation_helpers.rb
|
112
112
|
- lib/cloudflare/ai/contextual_logger.rb
|
113
113
|
- lib/cloudflare/ai/message.rb
|
114
114
|
- lib/cloudflare/ai/models.rb
|
115
115
|
- lib/cloudflare/ai/result.rb
|
116
|
+
- lib/cloudflare/ai/results/automatic_speech_recognition.rb
|
116
117
|
- lib/cloudflare/ai/results/image_classification.rb
|
117
118
|
- lib/cloudflare/ai/results/text_classification.rb
|
118
119
|
- lib/cloudflare/ai/results/text_embedding.rb
|