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.
- Tokenizer API based on
PreTrainedTokenizerBase
- Tokenizer shim to the Microsoft.ML.Tokenizers base class
- Utility classes to access pipelines like:
- Sentence Transformers (Embeddings)
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 results = pipeline.Generate("Tell me a story about a brave knight.", maxLength: 100, temperature: 0.7);
foreach (var result in results)
{
Console.WriteLine(result);
}