unstructured 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 66c48cd9be308e1d08567869d61c5fb450a1251928dd9f013313096224b4735b
4
+ data.tar.gz: 77ddd812edc3de3c23a1419e11179906c51f78ebe9ac3849f4401fd61d931436
5
+ SHA512:
6
+ metadata.gz: 9c3b456532eb9739f883f3299d69556259687df63c4b253b8caf3d0745aca6f7563eeaf29110e6c1660bb6ab7aaed8094362ae3af28c27cfd2e0f6b656e5e087
7
+ data.tar.gz: a8e7bc04334a13d214e7d9f32bb0f8e6cc512632e1c96c14873c6c931030aac552a9ac5815b7df6755ca52edf4ed16785abc501972cae7f642854ec0fcf9c24a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+ Metrics/MethodLength:
15
+ Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jose Fernando Davila
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,46 @@
1
+ # Unstructured
2
+ This is a Ruby client for the [Unstructured API](https://unstructured-io.github.io/unstructured/api.html).
3
+
4
+ ## Installation
5
+
6
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+
16
+ ## Usage
17
+ ```ruby
18
+ server_url = "https://api.unstructured.io" # "http://localhost:8000" if you are running a docker container
19
+ client = Unstructured::Client.new(server_url: server_url, api_key: ENV["UNSTRUCTURED_API_KEY"])
20
+
21
+ file_path = "./your_document.pdf"
22
+ params = {
23
+ strategy: "fast",
24
+ hi_res_model_name: "yolox",
25
+ pdf_infer_table_structure: true,
26
+ chunking_strategy: "by_title",
27
+ max_characters: 1000,
28
+ new_after_n_chars: 1000
29
+ }
30
+ chunks = client.partition(file_path, params)
31
+
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TelosLabs/unstructured.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unstructured
4
+ class ChunkCollection < Collection
5
+ def self.type
6
+ Unstructured::Chunk
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday/multipart"
5
+ require "marcel"
6
+
7
+ module Unstructured
8
+ class Client
9
+ SERVER_URL = "https://api.unstructured.io"
10
+
11
+ attr_reader :server_url, :api_key_auth, :adapter
12
+
13
+ def initialize(server_url: nil, api_key_auth: nil, adapter: Faraday.default_adapter, stubs: nil)
14
+ @server_url = server_url || SERVER_URL
15
+ @api_key_auth = api_key_auth
16
+ @adapter = adapter
17
+ @stubs = stubs
18
+ end
19
+
20
+ def connection
21
+ @connection ||= Faraday.new do |conn|
22
+ conn.url_prefix = server_url
23
+ conn.request :multipart
24
+ conn.response :json, content_type: "application/json"
25
+ conn.adapter adapter, @stubs
26
+ conn.headers["unstructured-api-key"] = api_key_auth if api_key_auth
27
+ end
28
+ end
29
+
30
+ def partition(file_path, params = {}, headers: {})
31
+ ChunkCollection.from_response handle_response(connection.post(
32
+ "/general/v0/general",
33
+ default_params.merge(params).merge({
34
+ files: files(file_path)
35
+ }),
36
+ headers
37
+ ))
38
+ end
39
+
40
+ def inspect
41
+ "#<#{self.class.name} server_url=#{server_url.inspect}>"
42
+ end
43
+
44
+ private
45
+
46
+ def files(file_path)
47
+ mime_type = Marcel::MimeType.for Pathname.new(file_path)
48
+ Faraday::UploadIO.new(file_path, mime_type)
49
+ end
50
+
51
+ def default_params
52
+ {
53
+ strategy: "fast",
54
+ hi_res_model_name: "yolox",
55
+ pdf_infer_table_structure: true,
56
+ chunking_strategy: "by_title"
57
+ }
58
+ end
59
+
60
+ def handle_response(response)
61
+ message = response
62
+ case response.status
63
+ when 401
64
+ raise Error, message
65
+ when 404
66
+ raise Error, message
67
+ when 422
68
+ raise Error, message
69
+ when 500
70
+ raise Error, message
71
+ when 200..299
72
+ response.body
73
+ else
74
+ raise Error, message
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unstructured
4
+ class Collection
5
+ include Enumerable
6
+
7
+ attr_reader :data, :total
8
+
9
+ def self.from_response(response)
10
+ new(
11
+ data: response.map { |attrs| type.new(attrs) },
12
+ raw_data: response,
13
+ total: response.length
14
+ )
15
+ end
16
+
17
+ def initialize(data:, raw_data:, total:)
18
+ @data = data
19
+ @raw_data = raw_data
20
+ @total = total
21
+ end
22
+
23
+ def to_h
24
+ @raw_data
25
+ end
26
+
27
+ def each(&block)
28
+ return to_enum(__method__) { @data.size } unless block_given?
29
+
30
+ @data.each(&block)
31
+ end
32
+
33
+ def self.type
34
+ raise NotImplementedError
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unstructured
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+ module Unstructured
5
+ class Object
6
+ def initialize(attributes)
7
+ @attributes = OpenStruct.new(attributes)
8
+ end
9
+
10
+ def method_missing(method, *args, &block)
11
+ method = method.to_s
12
+ attribute = @attributes.send(method, *args, &block)
13
+ attribute.is_a?(Hash) ? Object.new(attribute) : attribute
14
+ end
15
+
16
+ def respond_to_missing?(_method, _include_private = false)
17
+ true
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unstructured
4
+ class Chunk < Object
5
+ def self.from_response(response)
6
+ new(response)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unstructured
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "unstructured/version"
4
+
5
+ module Unstructured
6
+ autoload :Client, "unstructured/client"
7
+ autoload :Collection, "unstructured/collection"
8
+ autoload :Error, "unstructured/error"
9
+ autoload :Object, "unstructured/object"
10
+
11
+ autoload :ChunkCollection, "unstructured/chunk_collection"
12
+ autoload :Chunk, "unstructured/objects/chunk"
13
+ end
@@ -0,0 +1,4 @@
1
+ module Unstructured
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unstructured
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jose Fernando Davila
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-multipart
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: marcel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.23.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.23.0
69
+ description: Unstructured API wrapper. This gem allows you to interact with the Unstructured
70
+ API.
71
+ email:
72
+ - davila.jose23@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/unstructured.rb
83
+ - lib/unstructured/chunk_collection.rb
84
+ - lib/unstructured/client.rb
85
+ - lib/unstructured/collection.rb
86
+ - lib/unstructured/error.rb
87
+ - lib/unstructured/object.rb
88
+ - lib/unstructured/objects/chunk.rb
89
+ - lib/unstructured/version.rb
90
+ - sig/unstructured.rbs
91
+ homepage: https://github.com/TelosLabs/unstructured
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ homepage_uri: https://github.com/TelosLabs/unstructured
96
+ source_code_uri: https://github.com/TelosLabs/unstructured
97
+ changelog_uri: https://github.com/TelosLabs/unstructured
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.0.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.5.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Unstructured API wrapper.
117
+ test_files: []