Nomad changes
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s

This commit is contained in:
Henrik Jess Nielsen
2026-06-01 23:40:55 +02:00
parent 72b1a0a6ed
commit b4c07d3693
5723 changed files with 1130655 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
using Kreuzberg;
using System.Net.Http;
using System.Text.Json;
class CloudOcrBackend : IOcrBackend
{
private readonly string _apiKey;
private readonly HttpClient _httpClient;
private readonly string _apiEndpoint;
public CloudOcrBackend(string apiKey, string apiEndpoint = "https://api.example.com/ocr")
{
_apiKey = apiKey;
_apiEndpoint = apiEndpoint;
_httpClient = new HttpClient();
}
public string Name => "cloud-ocr-backend";
public string Process(ReadOnlySpan<byte> imageBytes, OcrConfig? config)
{
return Task.Run(async () =>
{
try
{
var bytes = imageBytes.ToArray();
using var content = new MultipartFormDataContent();
content.Add(new ByteArrayContent(bytes), "image");
var request = new HttpRequestMessage(
HttpMethod.Post,
_apiEndpoint
)
{
Content = content,
Headers =
{
{ "Authorization", $"Bearer {_apiKey}" }
}
};
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var jsonContent = await response.Content.ReadAsStringAsync();
return jsonContent;
}
catch (HttpRequestException ex)
{
throw new KreuzbergOcrException($"Cloud OCR service error: {ex.Message}");
}
}).GetAwaiter().GetResult();
}
public void Dispose()
{
_httpClient?.Dispose();
}
}
class Program
{
static void Main()
{
using var backend = new CloudOcrBackend(apiKey: "your-api-key-here");
KreuzbergLib.RegisterOcrBackend(backend);
try
{
var config = new ExtractionConfig
{
Ocr = new OcrConfig
{
Backend = "cloud-ocr-backend"
}
};
var result = KreuzbergLib.ExtractFileSync("document.pdf", config);
Console.WriteLine($"OCR text: {result.Content}");
}
catch (KreuzbergOcrException ex)
{
Console.WriteLine($"OCR error: {ex.Message}");
}
catch (KreuzbergException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}