hugging-face 0.3.3 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b1fd1e517f21466be454d55dcc88e0344acaa6fd91900335615f632b734595e
4
- data.tar.gz: c865080f1350cc0e056f0ff190b6c99373cc32a287bb9c0e558ce777456ca057
3
+ metadata.gz: e332f1b35fbd0fb35cae8001b5fa7ecae8aeb0326f49d63f7d9bba8872ae1df9
4
+ data.tar.gz: 895a1b725527941626559dc30d85b54204a3e9c2b06d196f3d061108eac5b8df
5
5
  SHA512:
6
- metadata.gz: 803618ef477824cafbf3b5d37e76d3bcfcc6b9cfd90632e55df733f9b7f14d7ef6e4d5403ce9f1828f6459ee32b511783c2d8f3217caf7ba3ee87123f0372754
7
- data.tar.gz: 62cfb9bb3f0f6905f9963d7997b7deecabdda75a0771f8625604bde1dd7c883ab76bc43e811355b99f26428d0ae84397deb1a5741824fa3c32fcd0e80fa6cc1c
6
+ metadata.gz: d55632ba6532b38545db4664ce8529cbb942fc41d65a7810251a04c2add4a8637311df1c2a4af91f76e3c2707dda7625104af6ad0fa9d36412f334bc27825c5c
7
+ data.tar.gz: 853625dc46f48785ba7fe257b44ccf34894adf03b81d8dcd0479c2f15b5e7b73f6c0459eac9725ae13214c77dd359a7ecd0f241dfa5c1658c136c920eba2fc32
data/Gemfile.lock CHANGED
@@ -1,25 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hugging-face (0.3.3)
5
- faraday (~> 1.0)
4
+ hugging-face (0.3.5)
5
+ faraday (>= 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- addressable (2.8.4)
10
+ addressable (2.8.5)
11
11
  public_suffix (>= 2.0.2, < 6.0)
12
+ base64 (0.1.1)
12
13
  crack (0.4.5)
13
14
  rexml
14
15
  diff-lcs (1.5.0)
15
- faraday (1.2.0)
16
- multipart-post (>= 1.2, < 3)
17
- ruby2_keywords
16
+ faraday (2.7.11)
17
+ base64
18
+ faraday-net_http (>= 2.0, < 3.1)
19
+ ruby2_keywords (>= 0.0.4)
20
+ faraday-net_http (3.0.2)
18
21
  hashdiff (1.0.1)
19
- multipart-post (2.3.0)
20
- public_suffix (5.0.1)
22
+ public_suffix (5.0.3)
21
23
  rake (13.0.6)
22
- rexml (3.2.5)
24
+ rexml (3.2.6)
23
25
  rspec (3.12.0)
24
26
  rspec-core (~> 3.12.0)
25
27
  rspec-expectations (~> 3.12.0)
@@ -29,18 +31,19 @@ GEM
29
31
  rspec-expectations (3.12.3)
30
32
  diff-lcs (>= 1.2.0, < 2.0)
31
33
  rspec-support (~> 3.12.0)
32
- rspec-mocks (3.12.5)
34
+ rspec-mocks (3.12.6)
33
35
  diff-lcs (>= 1.2.0, < 2.0)
34
36
  rspec-support (~> 3.12.0)
35
- rspec-support (3.12.0)
37
+ rspec-support (3.12.1)
36
38
  ruby2_keywords (0.0.5)
37
- webmock (3.18.1)
39
+ webmock (3.19.1)
38
40
  addressable (>= 2.8.0)
39
41
  crack (>= 0.3.2)
40
42
  hashdiff (>= 0.4.0, < 2.0.0)
41
43
 
42
44
  PLATFORMS
43
45
  arm64-darwin-21
46
+ arm64-darwin-22
44
47
  x86_64-darwin-19
45
48
  x86_64-linux
46
49
 
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Alex Chaplinsky
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -18,14 +18,19 @@ $ gem install hugging-face
18
18
 
19
19
  ## Usage
20
20
 
21
+
22
+ ### Inference API
23
+
24
+ The inference API is a free Machine Learning API from Hugging Face. It is meant for prototyping and not produciton use, see below for Inference Endpoints, the product for use with production LLMs.
25
+
21
26
  ```ruby
22
27
  require "hugging_face"
23
28
  ```
24
29
 
25
- Instantiate a HuggigFace Inference API client:
30
+ Instantiate a HuggingFace Inference API client:
26
31
 
27
32
  ```ruby
28
- client = HuggingFace::InferenceApi.new(api_key: ENV['HUGGING_FACE_API_KEY'])
33
+ client = HuggingFace::InferenceApi.new(api_token: ENV['HUGGING_FACE_API_TOKEN'])
29
34
  ```
30
35
 
31
36
  Question answering:
@@ -55,6 +60,40 @@ Embedding:
55
60
  client.embedding(input: ['How to build a ruby gem?', 'How to install ruby gem?'])
56
61
  ```
57
62
 
63
+ Sentiment:
64
+
65
+ ```ruby
66
+ client.sentiment(input: ['My life sucks', 'Life is a miracle'])
67
+ ```
68
+
69
+ ### Inference Endpoints
70
+
71
+ With this same gem, you can also access endpoints, which are best described by Hugging Face: "[Inference Endpoints](https://huggingface.co/docs/inference-endpoints/index) provides a secure production solution to easily deploy models on a dedicated and autoscaling infrastructure managed by Hugging Face. An Inference Endpoint is built from a model from the Hub."
72
+
73
+ Once you've created an endpoint by choosing a model and desired infrastructure, you'll be given an endpoint URL, something like: https://eyic1edp3ah0g5ln.us-east-1.aws.endpoints.huggingface.cloud
74
+
75
+ You'll need your token to access your endpoint if you've chosen for it to be protected. You can also choose to have a public endpoint.
76
+
77
+ Since you choose the LLM task in your endpoint config (e.g. Text Classification, Summarisation, Question answering), there is no need to pass that argument.
78
+
79
+ To access from the gem. instantiate a HuggingFace Endpoint API client:
80
+
81
+ ```ruby
82
+ endpoint = HuggingFace::EndpointsApi.new(api_token: ENV['HUGGING_FACE_API_TOKEN'])
83
+ ```
84
+
85
+ Example with input from question answering task:
86
+
87
+ ```ruby
88
+ endpoint.request(endpoint_url: "https://your-end-point.us-east-1.aws.endpoints.huggingface.cloud", input: { context: some_text, question: question }
89
+ ```
90
+
91
+ Example call to an endpoint configured for summarisation, including passing params:
92
+
93
+ ```ruby
94
+ endpoint.request(endpoint_url: "https://your-end-point.us-east-1.aws.endpoints.huggingface.cloud", input: some_text, params: { min_length: 32, max_length: 64 } )
95
+ ```
96
+
58
97
 
59
98
  ## Development
60
99
 
@@ -20,9 +20,12 @@ module HuggingFace
20
20
  Faraday.new(url, headers: @headers)
21
21
  end
22
22
 
23
- def request(connection:, input:)
24
- response = connection.post { |req| req.body = input.to_json }
25
-
23
+ def request(connection:, input:, params: nil)
24
+ response = connection.post { |req|
25
+ req.body = input.to_json
26
+ req.params = params if params
27
+ }
28
+
26
29
  if response.success?
27
30
  return parse_response response
28
31
  else
@@ -0,0 +1,29 @@
1
+ module HuggingFace
2
+ class EndpointsApi < BaseApi
3
+
4
+ def request(endpoint_url:, input:, params: nil)
5
+ retries = 0
6
+
7
+ endpoint_connection = build_connection endpoint_url
8
+
9
+ begin
10
+ return super(connection: endpoint_connection, input: { inputs: input }, params: params )
11
+ rescue ServiceUnavailable => exception
12
+
13
+ if retries < MAX_RETRY
14
+ logger.debug('Service unavailable, retrying...')
15
+ retries += 1
16
+ sleep 5
17
+ retry
18
+ else
19
+ raise exception
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ private
26
+
27
+
28
+ end
29
+ end
@@ -5,11 +5,12 @@ module HuggingFace
5
5
  # Retry connecting to the model for 1 minute
6
6
  MAX_RETRY = 60
7
7
 
8
- # Deafult models that can be overriden by 'model' param
8
+ # Default models that can be overriden by 'model' param
9
9
  QUESTION_ANSWERING_MODEL = 'distilbert-base-cased-distilled-squad'
10
10
  SUMMARIZATION_MODEL = "sshleifer/distilbart-xsum-12-6"
11
11
  GENERATION_MODEL = "distilgpt2"
12
12
  EMBEDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
13
+ SENTIMENT_MODEL = "distilbert-base-uncased-finetuned-sst-2-english"
13
14
 
14
15
  def call(input:, model:)
15
16
  request(connection: connection(model), input: input)
@@ -33,6 +34,11 @@ module HuggingFace
33
34
  request connection: connection(model), input: { inputs: input }
34
35
  end
35
36
 
37
+ def sentiment(input:, model: SENTIMENT_MODEL)
38
+ request connection: connection(model), input: { inputs: input }
39
+ end
40
+
41
+
36
42
  private
37
43
 
38
44
  def connection(model)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HuggingFace
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.5"
5
5
  end
data/lib/hugging_face.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "hugging_face/version"
4
4
  require_relative "hugging_face/base_api"
5
5
  require_relative "hugging_face/inference_api"
6
+ require_relative "hugging_face/endpoints_api"
6
7
 
7
8
  module HuggingFace
8
9
  class Error < StandardError; end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hugging-face
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Chaplinsky
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-16 00:00:00.000000000 Z
11
+ date: 2024-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
27
  description: Ruby client for Hugging Face API
@@ -36,10 +36,12 @@ files:
36
36
  - CODE_OF_CONDUCT.md
37
37
  - Gemfile
38
38
  - Gemfile.lock
39
+ - LICENSE
39
40
  - README.md
40
41
  - Rakefile
41
42
  - lib/hugging_face.rb
42
43
  - lib/hugging_face/base_api.rb
44
+ - lib/hugging_face/endpoints_api.rb
43
45
  - lib/hugging_face/inference_api.rb
44
46
  - lib/hugging_face/version.rb
45
47
  - sig/hugging_face.rbs
@@ -64,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
66
  - !ruby/object:Gem::Version
65
67
  version: '0'
66
68
  requirements: []
67
- rubygems_version: 3.3.7
69
+ rubygems_version: 3.3.26
68
70
  signing_key:
69
71
  specification_version: 4
70
72
  summary: Ruby client for Hugging Face API