Skip to content

Joe-joey1/transformerssharp

 
 

Repository files navigation

TransformersSharp

Logo

A little wrapper for Hugging Face Transformers in C#. This is not a comprehensive 1:1 mapping of the whole HuggingFace transformers package, because the API is enormous.

If you need a specific feature, toggle or pipeline API clone this repo and make adjustments.

This project was created using CSnakes and will fetch Python, PyTorch, and Hugging Face Transformers automatically, so you don't need to install them manually.

Full Documentation

Features

Usage

For example, the Python code:

from transformers import pipeline
import torch

pipeline = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B", torch_dtype=torch.bfloat16)
results = pipeline("Tell me a story about a brave knight.", max_length=100, temperature=0.7)
for result in results:
    print(result["generated_text"])

Is equivalent to:

using TransformersSharp.Pipelines;

var pipeline = TextGenerationPipeline.FromModel("Qwen/Qwen2.5-0.5B", TorchDtype.BFloat16);
var messages = new List<IReadOnlyDictionary<string, string>>
{
    new Dictionary<string, string> { { "role", "user" }, { "content", "Tell me a story about a brave knight." } }
};
var results = pipeline.Generate(messages, maxLength: 100, temperature: 0.7);
foreach (var result in results)
{
    foreach (var kvp in result)
    {
        Console.WriteLine($"{kvp.Key}: {kvp.Value}");
    }
}

About

A little wrapper for hugging face transformers in C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 82.1%
  • Python 17.9%