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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: edbbf72af6499d1823db172793f3dc183f90e445d4ae45587dc1a0cd4005c15e
4
- data.tar.gz: 04d0881c074e9e84617591768c1b53767d2f509bbb7497c6bc0920bf3bac96b3
3
+ metadata.gz: 40c13e552a887bd1d77fc718be8708b228c9e213c9bb49c3591b113738346bd4
4
+ data.tar.gz: e6dce5cda3fcef66bb9a627ffe23c5324b9395af6bd5eeb3c45a4ea400f52c63
5
5
  SHA512:
6
- metadata.gz: 0ea1480d83e383534c50d14320f5cbfcc7c7a890101f9e0492cbd486dc36e05fbebaedaeea31ccf4f8b5efb3f553349cee1b25ad890457734443054d7e1dfb91
7
- data.tar.gz: da1f798fd89bcaad28a41e67ec2f07e779e8a7d2681ac3d4790f2e27b2adfcfcc02f5c42e7b02c8eb7bc519fea4178f16bad6050576543dde241e8becd18dbec
6
+ metadata.gz: 723f039e065cd9b86111de3d9478cf711d01efdea5a67ce426cf1bcfb4e84396abe9f9969150b743a8f0e54be5caea0401d25a1dcf191c0f09a932189f19fbed
7
+ data.tar.gz: 1566d0fc8446ee1463c0dfbeb37d7eb7021ca0f0d04bdf3d83148f1e7b33eb1d2eea4ddea44dcef91bf9b11f7cd2508cdcfd9120ade6fb72ac70aa5510d22896
data/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  ![](https://images.unsplash.com/photo-1625768376503-68d2495d78c5?q=80&w=2225&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D)
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
- ### entities
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
- ### classification
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
- ### structured extraction
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.2'
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']
@@ -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, 0.5)
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, 0.5)
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, 0.5)
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
 
@@ -83,7 +83,7 @@ module Gliner
83
83
  parsed[:labels],
84
84
  label_positions,
85
85
  prepared,
86
- threshold: options.fetch(:threshold, 0.5)
86
+ threshold: options.fetch(:threshold, Gliner.config.threshold)
87
87
  )
88
88
  end
89
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gliner
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
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 load(dir, file: 'model_int8.onnx')
46
- self.model = Model.from_dir(dir, file: file)
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.0
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.2'
141
+ version: '3.3'
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  requirements:
143
144
  - - ">="