Skip to content

Commit

Permalink
DEV: Add attribution to AI captioned images (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
keegangeorge committed Feb 21, 2024
1 parent d88dceb commit 97f3cba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Expand Up @@ -121,7 +121,11 @@ def caption_image
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"),
Expand Down
2 changes: 2 additions & 0 deletions config/locales/server.en.yml
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions spec/requests/ai_helper/assistant_controller_spec.rb
Expand Up @@ -112,6 +112,9 @@
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) }
Expand All @@ -131,7 +134,7 @@
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
Expand Down Expand Up @@ -177,7 +180,7 @@
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
Expand All @@ -187,7 +190,7 @@
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
Expand Down

0 comments on commit 97f3cba

Please sign in to comment.