Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davilajose23 committed May 22, 2024
0 parents commit 2df3dd8
Show file tree
Hide file tree
Showing 25 changed files with 567 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.3.0'

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
15 changes: 15 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
AllCops:
TargetRubyVersion: 3.0

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Style/Documentation:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/MethodLength:
Enabled: false
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in unstructured.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"
89 changes: 89 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
PATH
remote: .
specs:
unstructured (0.1.0)
faraday (~> 2.9.0)
faraday-multipart (~> 1.0.4)
marcel (~> 1.0.4)
webmock (~> 3.23.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
bigdecimal (3.1.8)
crack (1.0.0)
bigdecimal
rexml
diff-lcs (1.5.1)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.1.0)
net-http
hashdiff (1.1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
marcel (1.0.4)
multipart-post (2.4.1)
net-http (0.4.1)
uri
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
public_suffix (5.0.5)
racc (1.7.3)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.1)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.63.5)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)
uri (0.13.0)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)
unstructured!

BUNDLED WITH
2.5.7
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Jose Fernando Davila

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Unstructured
This is a Ruby client for the [Unstructured API](https://unstructured-io.github.io/unstructured/api.html).

## Installation

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.

Install the gem and add to the application's Gemfile by executing:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

## Usage
```ruby
server_url = "https://api.unstructured.io" # "http://localhost:8000" if you are running a docker container
client = Unstructured::Client.new(server_url: server_url, api_key: ENV["UNSTRUCTURED_API_KEY"])

file_path = "./your_document.pdf"
params = {
strategy: "fast",
hi_res_model_name: "yolox",
pdf_infer_table_structure: true,
chunking_strategy: "by_title",
max_characters: 1000,
new_after_n_chars: 1000
}
chunks = client.partition(file_path, params)

```

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TelosLabs/unstructured.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[spec rubocop]
18 changes: 18 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "unstructured"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

client = Unstructured::Client.new(
server_url: "http://localhost:8000",
api_key_auth: ENV["UNSTRUCTURED_API_KEY"]
)

client.partition("./README.md")

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
13 changes: 13 additions & 0 deletions lib/unstructured.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative "unstructured/version"

module Unstructured
autoload :Client, "unstructured/client"
autoload :Collection, "unstructured/collection"
autoload :Error, "unstructured/error"
autoload :Object, "unstructured/object"

autoload :ChunkCollection, "unstructured/chunk_collection"
autoload :Chunk, "unstructured/objects/chunk"
end
9 changes: 9 additions & 0 deletions lib/unstructured/chunk_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Unstructured
class ChunkCollection < Collection
def self.type
Unstructured::Chunk
end
end
end
78 changes: 78 additions & 0 deletions lib/unstructured/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

require "faraday"
require "faraday/multipart"
require "marcel"

module Unstructured
class Client
SERVER_URL = "https://api.unstructured.io"

attr_reader :server_url, :api_key_auth, :adapter

def initialize(server_url: nil, api_key_auth: nil, adapter: Faraday.default_adapter, stubs: nil)
@server_url = server_url || SERVER_URL
@api_key_auth = api_key_auth
@adapter = adapter
@stubs = stubs
end

def connection
@connection ||= Faraday.new do |conn|
conn.url_prefix = server_url
conn.request :multipart
conn.response :json, content_type: "application/json"
conn.adapter adapter, @stubs
conn.headers["unstructured-api-key"] = api_key_auth if api_key_auth
end
end

def partition(file_path, params = {}, headers: {})
ChunkCollection.from_response handle_response(connection.post(
"/general/v0/general",
default_params.merge(params).merge({
files: files(file_path)
}),
headers
))
end

def inspect
"#<#{self.class.name} server_url=#{server_url.inspect}>"
end

private

def files(file_path)
mime_type = Marcel::MimeType.for Pathname.new(file_path)
Faraday::UploadIO.new(file_path, mime_type)
end

def default_params
{
strategy: "fast",
hi_res_model_name: "yolox",
pdf_infer_table_structure: true,
chunking_strategy: "by_title"
}
end

def handle_response(response)
message = response
case response.status
when 401
raise Error, message
when 404
raise Error, message
when 422
raise Error, message
when 500
raise Error, message
when 200..299
response.body
else
raise Error, message
end
end
end
end
Loading

0 comments on commit 2df3dd8

Please sign in to comment.