HuggingFace

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using LangChain.Providers;
using LangChain.Providers.HuggingFace;
using LangChain.Providers.HuggingFace.Predefined;

using var client = new HttpClient();
var provider = new HuggingFaceProvider(apiKey: string.Empty, client);
var gpt2Model = new Gpt2Model(provider);

var gp2ModelResponse = await gpt2Model.GenerateAsync("What would be a good company name be for name a company that makes colorful socks?");

Console.WriteLine("### GP2 Response");
Console.WriteLine(gp2ModelResponse);

const string imageToTextModel = "Salesforce/blip-image-captioning-base";
var model = new HuggingFaceImageToTextModel(provider, imageToTextModel);

var path = Path.Combine(Path.GetTempPath(), "solar_system.png");
var imageData = await File.ReadAllBytesAsync(path);
var binaryData = new BinaryData(imageData, "image/jpg");

var imageToTextResponse = await model.GenerateTextFromImageAsync(new ImageToTextRequest
{
    Image = binaryData
});

Console.WriteLine("\n\n### ImageToText Response");
Console.WriteLine(imageToTextResponse.Text);

Console.ReadLine();