yandex_image_moderation 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +14 -0
- data/lib/yandex_image_moderation/config.rb +29 -0
- data/lib/yandex_image_moderation/error.rb +20 -0
- data/lib/yandex_image_moderation/http_client.rb +32 -0
- data/lib/yandex_image_moderation/result.rb +63 -0
- data/lib/yandex_image_moderation/version.rb +3 -0
- data/lib/yandex_image_moderation.rb +42 -0
- data/yandex_image_moderation.gemspec +26 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 02bdb7bdb855be5fd3a7e4cfe30510dff3e6cdd2
|
4
|
+
data.tar.gz: 389cc34648ed382ba58134c74230922725d2bd8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 421ce9d15b0acdd26f0ff6068a2cda7b69d7a043dd93dc3d7dfaf2cb4d9790b082a8d079a305132cabacd1830572697911f06c7725b009308a8698f2fa2bdb58
|
7
|
+
data.tar.gz: b631b6ad51c9e12579fdc1dae177baf48b55b418bd143d36a1381cd57a43dacca8cc0b61e0f93e916c4f163bfff525081b9e03c97549be76e4f4536f6db691fd
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sergey Tsvetkov (stsvetkov@gmail.com)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# YandexImageModeration
|
2
|
+
|
3
|
+
This gem provides a class for access to Yandex Image Moderation API
|
4
|
+
(https://imagemoderation.yandexdatafactory.com/)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'yandex_image_moderation'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install yandex_image_moderation
|
21
|
+
|
22
|
+
## Configuration
|
23
|
+
|
24
|
+
For Yandex API access you have to provide an API key. Do it by calling config method of the YandexImageModeration class:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
YandexImageModeration.config do |c|
|
28
|
+
c.token = 'your_API_key'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
If you use Rails, this configuration would probably go into a proper initializer.
|
33
|
+
|
34
|
+
In some cases you may need to change URL of the API (i.e. when you use not the SAAS API, but a docker version on your own server).
|
35
|
+
To override the standard URL just add url attribute to the config. In the URL provide the path to the *moderate* service, do not add any parameters like *?model=moderate...*
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
YandexImageModeration.config do |c|
|
39
|
+
c.url = 'http://your_server/path_to/moderate'
|
40
|
+
c.token = 'your_API_key'
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
To moderate an image (remember: Yandex API works with JPEG only) just call a *moderate* methods with a path to the image file as a parameter:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
result = YandexImageModeration.moderate('/var/www/uploads/test.jpg')
|
50
|
+
puts result
|
51
|
+
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tgrave/yandex_image_moderation.
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
62
|
+
|
63
|
+
## Build Status [](http://travis-ci.org/tgrave/yandex_image_moderation)
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rubocop/rake_task'
|
5
|
+
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.test_files = FileList['test/**/*_test.rb', 'test/**/test_*.rb']
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
RuboCop::RakeTask.new
|
13
|
+
|
14
|
+
task default: [:test, :rubocop]
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Configuration of our class
|
2
|
+
module YandexImageModeration
|
3
|
+
class << self
|
4
|
+
# Configure it
|
5
|
+
#
|
6
|
+
# @yieldparam [OpenStruct] an object on which to set configuration
|
7
|
+
# options
|
8
|
+
#
|
9
|
+
# @return [OpenStruct] options
|
10
|
+
def config
|
11
|
+
@options ||= OptionSetter.new
|
12
|
+
yield(@options.config) if block_given?
|
13
|
+
@options.config
|
14
|
+
end
|
15
|
+
|
16
|
+
# Responsible for collecting options in the DSL and creating
|
17
|
+
# lookup objects using those options.
|
18
|
+
class OptionSetter
|
19
|
+
attr_accessor :config
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@config = OpenStruct.new
|
23
|
+
@config.url = 'https://cv-albion.ydf.yandex.net/moderate'.freeze
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private_constant :OptionSetter
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module YandexImageModeration
|
2
|
+
# Error messages that can be raised by this gem. To catch any
|
3
|
+
# related error message, use Error::Base.
|
4
|
+
#
|
5
|
+
# begin
|
6
|
+
# ...
|
7
|
+
# rescue YandexImageModeration::Error::Base => e
|
8
|
+
# puts "YandexImageModeration Error: #{e.message}"
|
9
|
+
# end
|
10
|
+
module Error
|
11
|
+
# Top-level error. All other timezone errors subclass this one.
|
12
|
+
class Base < StandardError; end
|
13
|
+
# Indicates an invalid result received from the server.
|
14
|
+
class InvalidResult < Base; end
|
15
|
+
# Indicates a request failure.
|
16
|
+
class Moderate < Base; end
|
17
|
+
# Indicates an invalid configuration.
|
18
|
+
class InvalidConfig < Base; end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/http/post/multipart'
|
4
|
+
|
5
|
+
# YandexImageModeration module part for HTTP Client
|
6
|
+
module YandexImageModeration
|
7
|
+
# @!visibility private
|
8
|
+
# A basic HTTP Client that handles requests to Yandex.
|
9
|
+
#
|
10
|
+
class NetHTTPClient
|
11
|
+
def initialize(config)
|
12
|
+
uri = parse_url config
|
13
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
14
|
+
@http.open_timeout = config.open_timeout || 10
|
15
|
+
@http.read_timeout = config.read_timeout || 30
|
16
|
+
@http.use_ssl = !(config.url =~ /^https/i).nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(url, body, headers)
|
20
|
+
request = Net::HTTP::Post::Multipart.new(url, body)
|
21
|
+
headers.each { |k, v| request[k.to_s] = v } unless headers.nil? || !headers.is_a?(Hash)
|
22
|
+
@http.request(request)
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def parse_url(config)
|
28
|
+
raise(::YandexImageModeration::Error::InvalidConfig, 'missing url') if config.url.to_s.empty?
|
29
|
+
URI.parse(config.url)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'yandex_image_moderation/error'
|
2
|
+
|
3
|
+
# YandexImageModeration module part for HTTP Client
|
4
|
+
module YandexImageModeration
|
5
|
+
# @!visibility public
|
6
|
+
# Moderation result with some useful methods
|
7
|
+
#
|
8
|
+
class Result
|
9
|
+
attr_reader :data
|
10
|
+
attr_reader :result
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
raise(::YandexImageModeration::Error::InvalidResult, 'no result data') if data.nil?
|
14
|
+
@data = data
|
15
|
+
parse_data
|
16
|
+
end
|
17
|
+
|
18
|
+
def good?
|
19
|
+
@status.to_s == 'ok'
|
20
|
+
end
|
21
|
+
|
22
|
+
def bad?
|
23
|
+
!good?
|
24
|
+
end
|
25
|
+
|
26
|
+
def porn_score
|
27
|
+
@result['pornography']['scores']['explicit'] unless @result.nil? || @result['pornography'].nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def erotic_score
|
31
|
+
@result['moderation']['scores']['erotic'] unless @result.nil? || @result['moderation'].nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
def ad_score
|
35
|
+
@result['ad']['scores']['ads'] unless @result.nil? || @result['ad'].nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def ad?
|
39
|
+
ad_score > 0.5
|
40
|
+
end
|
41
|
+
|
42
|
+
def porn?
|
43
|
+
porn_score > 0.3
|
44
|
+
end
|
45
|
+
|
46
|
+
def erotic?
|
47
|
+
erotic_score > 0.4
|
48
|
+
end
|
49
|
+
|
50
|
+
def predicted_class
|
51
|
+
@result['moderation']['predictedClass'] unless @result.nil? || @result['moderation'].nil?
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def parse_data
|
57
|
+
raise(::YandexImageModeration::Error::InvalidResult,
|
58
|
+
'invalid result') if @data.nil? || !@data.is_a?(Hash) || @data.empty?
|
59
|
+
@status = @data['status']
|
60
|
+
@result = @data['result']['classification'] unless @data['result'].nil?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'yandex_image_moderation/version'
|
3
|
+
require 'yandex_image_moderation/config'
|
4
|
+
require 'yandex_image_moderation/http_client'
|
5
|
+
require 'yandex_image_moderation/result'
|
6
|
+
|
7
|
+
# YandexImageModeration gem code
|
8
|
+
module YandexImageModeration
|
9
|
+
class << self
|
10
|
+
# Returns an instance of the request handler.
|
11
|
+
#
|
12
|
+
# @return [#post] an instance of a request handler
|
13
|
+
def client
|
14
|
+
@client ||= ::YandexImageModeration::NetHTTPClient.new(config)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Send a request for moderation
|
18
|
+
#
|
19
|
+
# @return [Result] an instance of the Result
|
20
|
+
def moderate(file)
|
21
|
+
return if file.nil? || !File.exist?(file)
|
22
|
+
|
23
|
+
url = "#{config.url}?models=moderation,gender,pornography,ad"
|
24
|
+
parse_response client.post(url, prepare_body(file), {})
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def parse_response(response)
|
30
|
+
return unless response.code.to_i == 200
|
31
|
+
raise(::YandexImageModeration::Error::InvalidResult, 'empty response') if response.body.nil?
|
32
|
+
::YandexImageModeration::Result.new JSON.parse(response.body)
|
33
|
+
end
|
34
|
+
|
35
|
+
def prepare_body(file)
|
36
|
+
{
|
37
|
+
'file' => UploadIO.new(file, 'image/jpeg', File.basename(file)),
|
38
|
+
'token' => @options.config.token
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yandex_image_moderation/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'yandex_image_moderation'
|
8
|
+
spec.version = YandexImageModeration::VERSION
|
9
|
+
spec.authors = ['Sergey Tsvetkov']
|
10
|
+
spec.email = ['stsvetkov@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = "yandex_image_moderation-#{YandexImageModeration::VERSION}"
|
13
|
+
spec.description = 'An interface for Yandex Image Moderation API'
|
14
|
+
spec.homepage = 'https://github.com/tgrave/yandex_image_moderation'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'minitest', '~> 5.8'
|
24
|
+
spec.add_development_dependency 'rspec-mocks'
|
25
|
+
spec.add_development_dependency 'rubocop'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex_image_moderation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Tsvetkov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-mocks
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: An interface for Yandex Image Moderation API
|
70
|
+
email:
|
71
|
+
- stsvetkov@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/yandex_image_moderation.rb
|
84
|
+
- lib/yandex_image_moderation/config.rb
|
85
|
+
- lib/yandex_image_moderation/error.rb
|
86
|
+
- lib/yandex_image_moderation/http_client.rb
|
87
|
+
- lib/yandex_image_moderation/result.rb
|
88
|
+
- lib/yandex_image_moderation/version.rb
|
89
|
+
- yandex_image_moderation.gemspec
|
90
|
+
homepage: https://github.com/tgrave/yandex_image_moderation
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.5
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: yandex_image_moderation-0.1.0
|
114
|
+
test_files: []
|