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
|
render json: @data
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def posts(limit = 10)
|
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
|
||||||
@@ -542,6 +542,16 @@ module Danbooru
|
|||||||
# "http://localhost:3000/mock/iqdb"
|
# "http://localhost:3000/mock/iqdb"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The URL for the Danbooru Autotagger service (https://github.com/danbooru/autotagger). Optional.
|
||||||
|
#
|
||||||
|
# Used for the AI tagging feature. Set this to http://localhost:3000/mock/autotagger
|
||||||
|
# to enable a fake server for development purposes, or do
|
||||||
|
# `docker run --rm -p 5000:5000 ghcr.io/danbooru/autotagger` to run a real server.
|
||||||
|
def autotagger_url
|
||||||
|
# "http://localhost:3000/mock/autotagger"
|
||||||
|
# "http://localhost:5000"
|
||||||
|
end
|
||||||
|
|
||||||
def aws_credentials
|
def aws_credentials
|
||||||
Aws::Credentials.new(Danbooru.config.aws_access_key_id, Danbooru.config.aws_secret_access_key)
|
Aws::Credentials.new(Danbooru.config.aws_access_key_id, Danbooru.config.aws_secret_access_key)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -365,6 +365,7 @@ Rails.application.routes.draw do
|
|||||||
get "/mock/reportbooru/post_views/rank" => "mock_services#reportbooru_post_views", as: "mock_reportbooru_post_views"
|
get "/mock/reportbooru/post_views/rank" => "mock_services#reportbooru_post_views", as: "mock_reportbooru_post_views"
|
||||||
get "/mock/iqdb/query" => "mock_services#iqdb_query", as: "mock_iqdb_query"
|
get "/mock/iqdb/query" => "mock_services#iqdb_query", as: "mock_iqdb_query"
|
||||||
post "/mock/iqdb/query" => "mock_services#iqdb_query"
|
post "/mock/iqdb/query" => "mock_services#iqdb_query"
|
||||||
|
get "/mock/autotagger/evaluate" => "mock_services#autotagger_evaluate", as: "mock_autotagger_evaluate"
|
||||||
|
|
||||||
match "*other", to: "static#not_found", via: :all
|
match "*other", to: "static#not_found", via: :all
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user