From be401e9c46c7162cc1ff51bbde309280986b6686 Mon Sep 17 00:00:00 2001 From: Ajay Krishnan <50063680+ajaynomics@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:14:39 -0500 Subject: [PATCH 1/4] support for image to text models --- lib/cloudflare/ai/results/image_to_text.rb | 4 ++- .../ai/results/image_to_text_test.rb | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/cloudflare/ai/results/image_to_text_test.rb diff --git a/lib/cloudflare/ai/results/image_to_text.rb b/lib/cloudflare/ai/results/image_to_text.rb index 2e955f2..91a6ad5 100644 --- a/lib/cloudflare/ai/results/image_to_text.rb +++ b/lib/cloudflare/ai/results/image_to_text.rb @@ -1,3 +1,5 @@ class Cloudflare::AI::Results::ImageToText < Cloudflare::AI::Result - # Empty seam kept for consistency with other result objects that have more complexity. + def description + result&.dig(:description) # nil if no shape + end end diff --git a/test/cloudflare/ai/results/image_to_text_test.rb b/test/cloudflare/ai/results/image_to_text_test.rb new file mode 100644 index 0000000..4793fdf --- /dev/null +++ b/test/cloudflare/ai/results/image_to_text_test.rb @@ -0,0 +1,29 @@ +require "test_helper" + +class Cloudflare::AI::Results::ImageToTextTest < Minitest::Test + def setup + @result = Cloudflare::AI::Results::ImageToText.new(successful_response_json) + end + + def test_successful_result + assert @result.success? + refute @result.failure? + + assert_equal successful_response_json["result"]["description"], @result.description + end + + def test_to_json + assert_equal successful_response_json.to_json, @result.to_json + end + + private + + def successful_response_json + { + result: {description: "a cute cat"}, + success: true, + errors: [], + messages: [] + }.deep_stringify_keys + end +end From 2106a70d2661d0aa4224210634ebabbd16b3cd67 Mon Sep 17 00:00:00 2001 From: Ajay Krishnan <50063680+ajaynomics@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:46:18 -0500 Subject: [PATCH 2/4] fix typo in model for object detection --- lib/cloudflare/ai/models.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cloudflare/ai/models.rb b/lib/cloudflare/ai/models.rb index 9a1640f..c003788 100644 --- a/lib/cloudflare/ai/models.rb +++ b/lib/cloudflare/ai/models.rb @@ -28,7 +28,7 @@ def image_to_text end def object_detection - %w[@cf/meta/det-resnet-50] + %w[@cf/meta/detr-resnet-50] end def summarization From 184aa3462d02ffb3b4acf0cdef50fb4b06400d93 Mon Sep 17 00:00:00 2001 From: Ajay Krishnan <50063680+ajaynomics@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:46:51 -0500 Subject: [PATCH 3/4] updated docs for new endpoint and bump gem --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 2ede387..5db161a 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,36 @@ result = client.transcribe(audio: File.open("/path/to/audio.wav")) #### Result object All invocations of the `transcribe` method returns a `Cloudflare::AI::Results::Transcribe`. +### Summarization +```ruby +result = client.summarize(text: "This text should be a lot longer.") +p result.summary # => {"result":{"summary":"Short text"},"success":true,"errors":[],"messages":[]} +``` +#### Result object +All invocations of the `summarize` method returns a `Cloudflare::AI::Results::Summarization` object. + +### Object detection +The object detection endpoint accepts either a path to a file or a file stream. + +```ruby +result = client.detect_objects(image: "/path/to/cat.jpg") +result = client.classify(image: File.open("/path/to/cat.jpg")) +``` + +#### Result object +All invocations of the `detect_objects` method returns a `Cloudflare::AI::Results::ObjectDetection` object. + +### Image-to-text +The captioning endpoint accepts either a path to a file or a file stream. + +```ruby +client.caption(image: "/path/to/cat.jpg").description # => "a cat sitting on a couch" +client.caption(image: File.open("/path/to/cat.jpg")).description # => "a cat sitting on a couch" +``` + +#### Result object +All invocations of the `caption` method returns a `Cloudflare::AI::Results::ImageToText` object. + # Logging This gem uses standard logging mechanisms and defaults to `:warn` level. Most messages are at info level, but we will add debug or warn statements as needed. From f9f5129fc9d228f71fb32e075147b8a6665b4875 Mon Sep 17 00:00:00 2001 From: Ajay Krishnan <50063680+ajaynomics@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:47:04 -0500 Subject: [PATCH 4/4] bump gem --- Gemfile.lock | 2 +- lib/cloudflare/ai/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f4bc1ee..05b0df7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - cloudflare-ai (0.9.1) + cloudflare-ai (0.9.2) activemodel (~> 7.0) activesupport (~> 7.0) event_stream_parser (~> 1.0) diff --git a/lib/cloudflare/ai/version.rb b/lib/cloudflare/ai/version.rb index 91e38f3..7afef2c 100644 --- a/lib/cloudflare/ai/version.rb +++ b/lib/cloudflare/ai/version.rb @@ -2,6 +2,6 @@ module Cloudflare module AI - VERSION = "0.9.1" + VERSION = "0.9.2" end end