gliner 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +20 -5
- data/gliner.gemspec +1 -1
- data/lib/gliner/classifier.rb +1 -1
- data/lib/gliner/configuration.rb +15 -0
- data/lib/gliner/model.rb +2 -2
- data/lib/gliner/tasks/entity_extraction.rb +1 -1
- data/lib/gliner/tasks/json_extraction.rb +1 -1
- data/lib/gliner/version.rb +1 -1
- data/lib/gliner.rb +27 -4
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 40c13e552a887bd1d77fc718be8708b228c9e213c9bb49c3591b113738346bd4
|
|
4
|
+
data.tar.gz: e6dce5cda3fcef66bb9a627ffe23c5324b9395af6bd5eeb3c45a4ea400f52c63
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 723f039e065cd9b86111de3d9478cf711d01efdea5a67ce426cf1bcfb4e84396abe9f9969150b743a8f0e54be5caea0401d25a1dcf191c0f09a932189f19fbed
|
|
7
|
+
data.tar.gz: 1566d0fc8446ee1463c0dfbeb37d7eb7021ca0f0d04bdf3d83148f1e7b33eb1d2eea4ddea44dcef91bf9b11f7cd2508cdcfd9120ade6fb72ac70aa5510d22896
|
data/README.md
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
Minimal Ruby inference wrapper for the **GLiNER2** ONNX model using:
|
|
6
|
-
|
|
7
5
|
## Install
|
|
8
6
|
|
|
9
7
|
```ruby
|
|
@@ -11,11 +9,18 @@ gem "gliner"
|
|
|
11
9
|
```
|
|
12
10
|
|
|
13
11
|
## Usage
|
|
14
|
-
|
|
12
|
+
|
|
13
|
+
### Entities
|
|
15
14
|
|
|
16
15
|
```ruby
|
|
17
16
|
require "gliner"
|
|
18
17
|
|
|
18
|
+
Gliner.configure do |config|
|
|
19
|
+
config.threshold = 0.2
|
|
20
|
+
config.model_dir = "/path/to/gliner2-multi-v1"
|
|
21
|
+
config.model_file = "model.onnx"
|
|
22
|
+
end
|
|
23
|
+
|
|
19
24
|
Gliner.load("path/to/gliner2-multi-v1")
|
|
20
25
|
|
|
21
26
|
text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
|
|
@@ -43,7 +48,7 @@ model = Gliner[labels]
|
|
|
43
48
|
pp model["Email John Doe at john@example.com.", threshold: 0.5]
|
|
44
49
|
```
|
|
45
50
|
|
|
46
|
-
###
|
|
51
|
+
### Classification
|
|
47
52
|
|
|
48
53
|
```ruby
|
|
49
54
|
model = Gliner.classify[
|
|
@@ -61,7 +66,7 @@ Expected shape:
|
|
|
61
66
|
{"sentiment"=>"negative"}
|
|
62
67
|
```
|
|
63
68
|
|
|
64
|
-
###
|
|
69
|
+
### Structured extraction
|
|
65
70
|
|
|
66
71
|
```ruby
|
|
67
72
|
text = "iPhone 15 Pro Max with 256GB storage, A17 Pro chip, priced at $1199."
|
|
@@ -101,6 +106,16 @@ This implementation expects a directory containing:
|
|
|
101
106
|
- (optional) `config.json` with `max_width` and `max_seq_len`
|
|
102
107
|
|
|
103
108
|
One publicly available ONNX export is `cuerbot/gliner2-multi-v1` on Hugging Face.
|
|
109
|
+
By default, `model_int8.onnx` is used; set `config.model_file` or `GLINER_MODEL_FILE` to override.
|
|
110
|
+
|
|
111
|
+
You can also configure the model directory in code:
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
Gliner.configure do |config|
|
|
115
|
+
config.model_dir = "/path/to/model_dir"
|
|
116
|
+
config.model_file = "model_int8.onnx"
|
|
117
|
+
end
|
|
118
|
+
```
|
|
104
119
|
|
|
105
120
|
## Integration test
|
|
106
121
|
|
data/gliner.gemspec
CHANGED
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
|
11
11
|
spec.description = 'Basic Ruby inference wrapper for the GLiNER2 ONNX model.'
|
|
12
12
|
spec.homepage = 'https://github.com/elcuervo/gliner'
|
|
13
13
|
spec.license = 'MIT'
|
|
14
|
-
spec.required_ruby_version = '>= 3.
|
|
14
|
+
spec.required_ruby_version = '>= 3.3'
|
|
15
15
|
|
|
16
16
|
spec.files = Dir.glob('lib/**/*') + Dir.glob('bin/*') + %w[README.md LICENSE gliner.gemspec]
|
|
17
17
|
spec.require_paths = ['lib']
|
data/lib/gliner/classifier.rb
CHANGED
|
@@ -5,7 +5,6 @@ require 'gliner/position_iteration'
|
|
|
5
5
|
module Gliner
|
|
6
6
|
class Classifier
|
|
7
7
|
include PositionIteration
|
|
8
|
-
|
|
9
8
|
def initialize(inference, max_width:)
|
|
10
9
|
@inference = inference
|
|
11
10
|
@max_width = max_width
|
|
@@ -64,5 +63,6 @@ module Gliner
|
|
|
64
63
|
def format_label(label, score, include_confidence)
|
|
65
64
|
include_confidence ? { 'label' => label, 'confidence' => score } : label
|
|
66
65
|
end
|
|
66
|
+
|
|
67
67
|
end
|
|
68
68
|
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gliner
|
|
4
|
+
class Configuration
|
|
5
|
+
DEFAULT_THRESHOLD = 0.5
|
|
6
|
+
|
|
7
|
+
attr_accessor :threshold, :model_dir, :model_file
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@threshold = DEFAULT_THRESHOLD
|
|
11
|
+
@model_dir = nil
|
|
12
|
+
@model_file = nil
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/gliner/model.rb
CHANGED
|
@@ -100,7 +100,7 @@ module Gliner
|
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
def extract_entities(text, entity_types, **options)
|
|
103
|
-
threshold = options.fetch(:threshold,
|
|
103
|
+
threshold = options.fetch(:threshold, Gliner.config.threshold)
|
|
104
104
|
include_confidence = options.fetch(:include_confidence, false)
|
|
105
105
|
include_spans = options.fetch(:include_spans, false)
|
|
106
106
|
|
|
@@ -125,7 +125,7 @@ module Gliner
|
|
|
125
125
|
end
|
|
126
126
|
|
|
127
127
|
def extract_json(text, structures, **options)
|
|
128
|
-
threshold = options.fetch(:threshold,
|
|
128
|
+
threshold = options.fetch(:threshold, Gliner.config.threshold)
|
|
129
129
|
include_confidence = options.fetch(:include_confidence, false)
|
|
130
130
|
include_spans = options.fetch(:include_spans, false)
|
|
131
131
|
|
|
@@ -29,7 +29,7 @@ module Gliner
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def process_output(logits, parsed, prepared, options)
|
|
32
|
-
threshold = options.fetch(:threshold,
|
|
32
|
+
threshold = options.fetch(:threshold, Gliner.config.threshold)
|
|
33
33
|
format_opts = FormatOptions.from(options)
|
|
34
34
|
label_positions = options[:label_positions] || inference.label_positions_for(prepared.word_ids, parsed[:labels].length)
|
|
35
35
|
|
data/lib/gliner/version.rb
CHANGED
data/lib/gliner.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'gliner/version'
|
|
4
|
+
require 'gliner/configuration'
|
|
4
5
|
require 'gliner/model'
|
|
5
6
|
require 'gliner/runners/prepared_task'
|
|
6
7
|
require 'gliner/runners/entity_runner'
|
|
@@ -41,13 +42,25 @@ module Gliner
|
|
|
41
42
|
|
|
42
43
|
class << self
|
|
43
44
|
attr_writer :model
|
|
45
|
+
attr_writer :config
|
|
44
46
|
|
|
45
|
-
def
|
|
46
|
-
|
|
47
|
+
def configure
|
|
48
|
+
yield(config)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def config
|
|
52
|
+
@config ||= Configuration.new
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def load(dir, file: nil)
|
|
56
|
+
file ||= ENV['GLINER_MODEL_FILE']
|
|
57
|
+
file ||= config.model_file
|
|
58
|
+
|
|
59
|
+
self.model = Model.from_dir(dir, file: file || 'model_int8.onnx')
|
|
47
60
|
end
|
|
48
61
|
|
|
49
62
|
def model
|
|
50
|
-
@model ||= model_from_env
|
|
63
|
+
@model ||= model_from_config || model_from_env
|
|
51
64
|
end
|
|
52
65
|
|
|
53
66
|
def model!
|
|
@@ -64,6 +77,16 @@ module Gliner
|
|
|
64
77
|
|
|
65
78
|
private
|
|
66
79
|
|
|
80
|
+
def model_from_config
|
|
81
|
+
dir = config.model_dir
|
|
82
|
+
return nil if dir.nil? || dir.empty?
|
|
83
|
+
|
|
84
|
+
file = config.model_file
|
|
85
|
+
return Model.from_dir(dir) if file.nil? || file.empty?
|
|
86
|
+
|
|
87
|
+
Model.from_dir(dir, file: file)
|
|
88
|
+
end
|
|
89
|
+
|
|
67
90
|
def model_from_env
|
|
68
91
|
dir = ENV.fetch('GLINER_MODEL_DIR', nil)
|
|
69
92
|
return nil if dir.nil? || dir.empty?
|
|
@@ -76,7 +99,7 @@ module Gliner
|
|
|
76
99
|
model = self.model
|
|
77
100
|
return model if model
|
|
78
101
|
|
|
79
|
-
raise Error, 'No model loaded. Call Gliner.load("/path/to/model") or set GLINER_MODEL_DIR.'
|
|
102
|
+
raise Error, 'No model loaded. Call Gliner.load("/path/to/model"), set config.model_dir, or set GLINER_MODEL_DIR.'
|
|
80
103
|
end
|
|
81
104
|
|
|
82
105
|
def runner_for(config)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gliner
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- elcuervo
|
|
@@ -108,6 +108,7 @@ files:
|
|
|
108
108
|
- lib/gliner/config/entity_types.rb
|
|
109
109
|
- lib/gliner/config/field_spec.rb
|
|
110
110
|
- lib/gliner/config_parser.rb
|
|
111
|
+
- lib/gliner/configuration.rb
|
|
111
112
|
- lib/gliner/inference.rb
|
|
112
113
|
- lib/gliner/inference/session_validator.rb
|
|
113
114
|
- lib/gliner/input_builder.rb
|
|
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
137
138
|
requirements:
|
|
138
139
|
- - ">="
|
|
139
140
|
- !ruby/object:Gem::Version
|
|
140
|
-
version: '3.
|
|
141
|
+
version: '3.3'
|
|
141
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
143
|
requirements:
|
|
143
144
|
- - ">="
|