From 97f3cba6033d2b4d5eae74ae77e14cb31bb429eb Mon Sep 17 00:00:00 2001 From: Keegan George Date: Wed, 21 Feb 2024 10:10:22 -0800 Subject: [PATCH] DEV: Add attribution to AI captioned images (#483) --- .../discourse_ai/ai_helper/assistant_controller.rb | 6 +++++- config/locales/server.en.yml | 2 ++ spec/requests/ai_helper/assistant_controller_spec.rb | 9 ++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/controllers/discourse_ai/ai_helper/assistant_controller.rb b/app/controllers/discourse_ai/ai_helper/assistant_controller.rb index 2df162df..53ca1f9b 100644 --- a/app/controllers/discourse_ai/ai_helper/assistant_controller.rb +++ b/app/controllers/discourse_ai/ai_helper/assistant_controller.rb @@ -121,7 +121,11 @@ module DiscourseAi final_image_url, current_user, ) - render json: { caption: caption }, status: 200 + render json: { + caption: + "#{caption} (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})", + }, + status: 200 end rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed, Net::HTTPBadResponse render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"), diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 5eafe23a..9f0660a9 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -147,6 +147,8 @@ en: attribution: stable_diffusion_xl: "Image by Stable Diffusion XL" dall_e_3: "Image by DALL-E 3" + image_caption: + attribution: "Captioned by AI" ai_bot: personas: diff --git a/spec/requests/ai_helper/assistant_controller_spec.rb b/spec/requests/ai_helper/assistant_controller_spec.rb index 752b1144..005fc501 100644 --- a/spec/requests/ai_helper/assistant_controller_spec.rb +++ b/spec/requests/ai_helper/assistant_controller_spec.rb @@ -112,6 +112,9 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do fab!(:upload) { Fabricate(:upload) } let(:image_url) { "#{Discourse.base_url}#{upload.url}" } let(:caption) { "A picture of a cat sitting on a table" } + let(:caption_with_attrs) do + "A picture of a cat sitting on a table (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})" + end context "when logged in as an allowed user" do fab!(:user) { Fabricate(:user, refresh_auto_groups: true) } @@ -131,7 +134,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do post "/discourse-ai/ai-helper/caption_image", params: { image_url: image_url } expect(response.status).to eq(200) - expect(response.parsed_body["caption"]).to eq(caption) + expect(response.parsed_body["caption"]).to eq(caption_with_attrs) end it "returns a 502 error when the completion call fails" do @@ -177,7 +180,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do group.add(user) post "/discourse-ai/ai-helper/caption_image", params: { image_url: image_url } expect(response.status).to eq(200) - expect(response.parsed_body["caption"]).to eq(caption) + expect(response.parsed_body["caption"]).to eq(caption_with_attrs) end context "if the input URL is for a secure upload but not on the secure-uploads path" do @@ -187,7 +190,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do group.add(user) post "/discourse-ai/ai-helper/caption_image", params: { image_url: image_url } expect(response.status).to eq(200) - expect(response.parsed_body["caption"]).to eq(caption) + expect(response.parsed_body["caption"]).to eq(caption_with_attrs) end end end