Skip to content

Commit

Permalink
ai tags: save ai tags on upload.
Browse files Browse the repository at this point in the history
Save the AI tags when a media asset is uploaded.
  • Loading branch information
evazion committed Jun 28, 2022
1 parent c0c435d commit a9fe73a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -56,6 +56,7 @@ gem "public_suffix"
gem "elastic-apm"
gem "debug"
gem "ffaker"
gem "composite_primary_keys"

group :development do
gem 'rubocop', require: false
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -133,6 +133,8 @@ GEM
codecov (0.6.0)
simplecov (>= 0.15, < 0.22)
coderay (1.1.3)
composite_primary_keys (14.0.4)
activerecord (~> 7.0.2)
concurrent-ruby (1.1.10)
crass (1.0.6)
daemons (1.4.1)
Expand Down Expand Up @@ -528,6 +530,7 @@ DEPENDENCIES
capybara
clockwork
codecov
composite_primary_keys
crass
daemons
debug
Expand Down
13 changes: 13 additions & 0 deletions app/logical/media_file.rb
Expand Up @@ -189,6 +189,19 @@ def preview(width, height, **options)
nil
end

# Return a set of AI-inferred tags for this image. Performs an API call to
# the Autotagger service. The Autotagger service must be running, otherwise
# it will return an empty list of tags.
#
# @return [Array<AITag>] The list of AI tags.
def ai_tags(autotagger: AutotaggerClient.new)
tags = autotagger.evaluate(self)

tags.map do |tag, score|
AITag.new(tag: tag, score: (100*score).round)
end
end

def attributes
{
path: path,
Expand Down
4 changes: 3 additions & 1 deletion app/models/ai_tag.rb
@@ -1,11 +1,13 @@
# frozen_string_literal: true

class AITag < ApplicationRecord
self.primary_keys = :media_asset_id, :tag_id

belongs_to :tag
belongs_to :media_asset
has_one :post, through: :media_asset

validates :score, inclusion: { in: (0.0..1.0) }
validates :score, inclusion: { in: (0..100) }

def self.named(name)
name = $1.downcase if name =~ /\A(rating:.)/i
Expand Down
9 changes: 9 additions & 0 deletions app/models/media_asset.rb
Expand Up @@ -27,6 +27,7 @@ class Error < StandardError; end

scope :public_only, -> { where(is_public: true) }
scope :private_only, -> { where(is_public: false) }
scope :without_ai_tags, -> { where.not(AITag.where("ai_tags.media_asset_id = media_assets.id").select(1).arel.exists) }

# Processing: The asset's files are currently being resized and distributed to the backend servers.
# Active: The asset has been successfully uploaded and is ready to use.
Expand Down Expand Up @@ -279,6 +280,14 @@ def file=(file_or_path)
self.duration = media_file.duration
self.media_metadata = MediaMetadata.new(file: media_file)
self.pixiv_ugoira_frame_data = PixivUgoiraFrameData.new(data: media_file.frame_data, content_type: "image/jpeg") if is_ugoira?
self.ai_tags = media_file.preview(360, 360).ai_tags # XXX should do this in parallel with thumbnail generation.
end

def regenerate_ai_tags!
with_lock do
ai_tags.each(&:destroy!)
update!(ai_tags: variant(:"360x360").open_file.ai_tags)
end
end

def expunge!
Expand Down

0 comments on commit a9fe73a

Please sign in to comment.