ai tags: add autotagger API client.
Add API client for https://github.com/danbooru/autotagger service.
This commit is contained in:
@@ -38,6 +38,14 @@ class MockServicesController < ApplicationController
|
||||
render json: @data
|
||||
end
|
||||
|
||||
def autotagger_evaluate
|
||||
limit = params.fetch(:limit, 50)
|
||||
predictions = tags(limit).pluck(:name).map { |name| [name, rand(0.0..1.0)] }.to_h
|
||||
data = { filename: "test.jpg", tags: predictions }
|
||||
|
||||
render json: data
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def posts(limit = 10)
|
||||
|
||||
25
app/logical/autotagger_client.rb
Normal file
25
app/logical/autotagger_client.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# API client for the Danbooru Autotagger service. Used for the AI tagging feature.
|
||||
#
|
||||
# @see https://autotagger.donmai.us
|
||||
# @see https://github.com/danbooru/autotagger
|
||||
class AutotaggerClient
|
||||
attr_reader :autotagger_url, :http
|
||||
|
||||
def initialize(autotagger_url: Danbooru.config.autotagger_url.to_s, http: Danbooru::Http.new)
|
||||
@autotagger_url = autotagger_url.chomp("/")
|
||||
@http = http
|
||||
end
|
||||
|
||||
def evaluate(file, limit: 50, confidence: 0.01)
|
||||
return {} if autotagger_url.blank?
|
||||
|
||||
response = http.post("#{autotagger_url}/evaluate", form: { file: HTTP::FormData::File.new(file), threshold: confidence, format: "json" })
|
||||
return {} if !response.status.success?
|
||||
|
||||
tag_names_with_scores = response.parse.first["tags"]
|
||||
tags = Tag.where(name: tag_names_with_scores.keys).index_by(&:name)
|
||||
tag_names_with_scores.transform_keys(&tags)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user