https://github.com/ankane/transformers-ruby Skip to content Navigation Menu Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + GitHub Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions By size + Enterprise + Teams + Startups By industry + Healthcare + Financial services + Manufacturing By use case + CI/CD & Automation + DevOps + DevSecOps * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} ankane / transformers-ruby Public * Notifications You must be signed in to change notification settings * Fork 4 * Star 325 State-of-the-art transformers for Ruby License Apache-2.0 license 325 stars 4 forks Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 2 * Pull requests 0 * Actions * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Security * Insights ankane/transformers-ruby This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master BranchesTags Go to file Code Folders and files Last commit Last Name Name message commit date Latest commit History 2 Commits .github/workflows .github/workflows lib lib licenses licenses test test .gitignore .gitignore CHANGELOG.md CHANGELOG.md Gemfile Gemfile LICENSE.txt LICENSE.txt README.md README.md Rakefile Rakefile transformers-rb.gemspec transformers-rb.gemspec View all files Repository files navigation * README * Apache-2.0 license Transformers.rb State-of-the-art transformers for Ruby Build Status Installation First, install Torch.rb. Then add this line to your application's Gemfile: gem "transformers-rb" Getting Started * Models * Pipelines Models sentence-transformers/all-MiniLM-L6-v2 Docs sentences = ["This is an example sentence", "Each sentence is converted"] model = Transformers::SentenceTransformer.new("sentence-transformers/all-MiniLM-L6-v2") embeddings = model.encode(sentences) sentence-transformers/multi-qa-MiniLM-L6-cos-v1 Docs query = "How many people live in London?" docs = ["Around 9 Million people live in London", "London is known for its financial district"] model = Transformers::SentenceTransformer.new("sentence-transformers/multi-qa-MiniLM-L6-cos-v1") query_emb = model.encode(query) doc_emb = model.encode(docs) scores = Torch.mm(Torch.tensor([query_emb]), Torch.tensor(doc_emb).transpose(0, 1))[0].cpu.to_a doc_score_pairs = docs.zip(scores).sort_by { |d, s| -s } mixedbread-ai/mxbai-embed-large-v1 Docs def transform_query(query) "Represent this sentence for searching relevant passages: #{query}" end docs = [ transform_query("puppy"), "The dog is barking", "The cat is purring" ] model = Transformers::SentenceTransformer.new("mixedbread-ai/mxbai-embed-large-v1") embeddings = model.encode(docs) opensearch-project/opensearch-neural-sparse-encoding-v1 Docs docs = ["The dog is barking", "The cat is purring", "The bear is growling"] model_id = "opensearch-project/opensearch-neural-sparse-encoding-v1" model = Transformers::AutoModelForMaskedLM.from_pretrained(model_id) tokenizer = Transformers::AutoTokenizer.from_pretrained(model_id) special_token_ids = tokenizer.special_tokens_map.map { |_, token| tokenizer.vocab[token] } feature = tokenizer.(docs, padding: true, truncation: true, return_tensors: "pt", return_token_type_ids: false) output = model.(**feature)[0] values, _ = Torch.max(output * feature[:attention_mask].unsqueeze(-1), dim: 1) values = Torch.log(1 + Torch.relu(values)) values[0.., special_token_ids] = 0 embeddings = values.to_a Pipelines Named-entity recognition ner = Transformers.pipeline("ner") ner.("Ruby is a programming language created by Matz") Sentiment analysis classifier = Transformers.pipeline("sentiment-analysis") classifier.("We are very happy to show you the Transformers library.") Question answering qa = Transformers.pipeline("question-answering") qa.(question: "Who invented Ruby?", context: "Ruby is a programming language created by Matz") Feature extraction extractor = Transformers.pipeline("feature-extraction") extractor.("We are very happy to show you the Transformers library.") Image classification classifier = Transformers.pipeline("image-classification") classifier.(URI("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg")) Image feature extraction extractor = Transformers.pipeline("image-feature-extraction") extractor.(URI("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg")) API This library follows the Transformers Python API. Only a few model architectures are currently supported: * BERT * DistilBERT * ViT History View the changelog Contributing Everyone is encouraged to help improve this project. Here are a few ways you can help: * Report bugs * Fix bugs and submit pull requests * Write, clarify, or fix documentation * Suggest or add new features To get started with development: git clone https://github.com/ankane/transformers-ruby.git cd transformers-ruby bundle install bundle exec rake download:files bundle exec rake test About State-of-the-art transformers for Ruby Resources Readme License Apache-2.0 license Activity Stars 325 stars Watchers 4 watching Forks 4 forks Report repository Releases 1 tags Packages 0 No packages published Languages * Ruby 100.0% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.