DEV: Add attribution to AI captioned images (#483)

This commit is contained in:
Keegan George 2024-02-21 10:10:22 -08:00 committed by GitHub
parent d88dceb49d
commit 97f3cba603
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 4 deletions

View File

@ -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"),

View File

@ -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:

View File

@ -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