| @@ -102,7 +102,7 @@ namespace LLama.Examples.Examples | |||||
| // | // | ||||
| foreach (var image in imagePaths) | foreach (var image in imagePaths) | ||||
| { | { | ||||
| ex.Images.Add(new ImageData(ImageData.DataType.ImagePath, image)); | |||||
| ex.Images.Add(File.ReadAllBytes(image)); | |||||
| } | } | ||||
| } | } | ||||
| @@ -27,7 +27,7 @@ namespace LLama.Abstractions | |||||
| /// <summary> | /// <summary> | ||||
| /// List of images: Image filen path, uri or image byte array. See ImageData. | /// List of images: Image filen path, uri or image byte array. See ImageData. | ||||
| /// </summary> | /// </summary> | ||||
| public List<ImageData> Images { get; } | |||||
| public List<byte[]> Images { get; } | |||||
| /// <summary> | /// <summary> | ||||
| /// Asynchronously infers a response from the model. | /// Asynchronously infers a response from the model. | ||||
| @@ -38,46 +38,4 @@ namespace LLama.Abstractions | |||||
| /// <returns></returns> | /// <returns></returns> | ||||
| IAsyncEnumerable<string> InferAsync(string text, IInferenceParams? inferenceParams = null, CancellationToken token = default); | IAsyncEnumerable<string> InferAsync(string text, IInferenceParams? inferenceParams = null, CancellationToken token = default); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Holds image data | |||||
| /// </summary> | |||||
| public class ImageData | |||||
| { | |||||
| /// <summary> | |||||
| /// constructor | |||||
| /// </summary> | |||||
| /// <param name="type"></param> | |||||
| /// <param name="data"></param> | |||||
| public ImageData(DataType type, object data) { Type = type; Data = data; } | |||||
| /// <summary> | |||||
| /// the possible types of image data | |||||
| /// </summary> | |||||
| public enum DataType | |||||
| { | |||||
| /// <summary> | |||||
| /// file path | |||||
| /// </summary> | |||||
| ImagePath, | |||||
| /// <summary> | |||||
| /// byte array | |||||
| /// </summary> | |||||
| ImageBytes, | |||||
| /// <summary> | |||||
| /// uri | |||||
| /// </summary> | |||||
| ImageURL | |||||
| } | |||||
| /// <summary> | |||||
| /// the type of this image data | |||||
| /// </summary> | |||||
| public DataType Type { get; set; } | |||||
| /// <summary> | |||||
| /// the image data (string, byte array or uri) | |||||
| /// </summary> | |||||
| public object? Data { get; set; } | |||||
| } | |||||
| } | } | ||||
| @@ -79,7 +79,7 @@ namespace LLama | |||||
| public LLavaWeights? ClipModel { get; } | public LLavaWeights? ClipModel { get; } | ||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| public List<ImageData> Images { get; set; } | |||||
| public List<byte[]> Images { get; set; } | |||||
| /// <summary> | /// <summary> | ||||
| /// Current "mu" value for mirostat sampling | /// Current "mu" value for mirostat sampling | ||||
| @@ -95,7 +95,7 @@ namespace LLama | |||||
| /// <param name="logger"></param> | /// <param name="logger"></param> | ||||
| protected StatefulExecutorBase(LLamaContext context, ILogger? logger = null) | protected StatefulExecutorBase(LLamaContext context, ILogger? logger = null) | ||||
| { | { | ||||
| Images = new List<ImageData>(); | |||||
| Images = new List<byte[]>(); | |||||
| _logger = logger; | _logger = logger; | ||||
| Context = context; | Context = context; | ||||
| _pastTokensCount = 0; | _pastTokensCount = 0; | ||||
| @@ -153,24 +153,7 @@ namespace LLama | |||||
| { | { | ||||
| foreach (var image in Images) | foreach (var image in Images) | ||||
| { | { | ||||
| if (image.Type == ImageData.DataType.ImagePath && image.Data != null) | |||||
| { | |||||
| _imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromFileName(ClipModel.NativeHandle, Context, (string)image.Data)); | |||||
| } | |||||
| else if (image.Type == ImageData.DataType.ImageBytes && image.Data != null) | |||||
| { | |||||
| _imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, (byte[])image.Data)); | |||||
| } | |||||
| else if (image.Type == ImageData.DataType.ImageURL && image.Data != null) | |||||
| { | |||||
| using var httpClient = new HttpClient(); | |||||
| var uri = new Uri((string)image.Data); | |||||
| var imageBytes = httpClient.GetByteArrayAsync(uri).Result; | |||||
| if (imageBytes != null && imageBytes.Length > 0) | |||||
| { | |||||
| _imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, imageBytes)); | |||||
| } | |||||
| } | |||||
| _imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, image)); | |||||
| } | } | ||||
| int imageIndex = text.IndexOf("<image>"); | int imageIndex = text.IndexOf("<image>"); | ||||
| @@ -34,7 +34,7 @@ namespace LLama | |||||
| public LLavaWeights? ClipModel { get; } | public LLavaWeights? ClipModel { get; } | ||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| public List<ImageData> Images { get; set; } | |||||
| public List<byte[]> Images { get; set; } | |||||
| /// <summary> | /// <summary> | ||||
| /// The context used by the executor when running the inference. | /// The context used by the executor when running the inference. | ||||
| @@ -49,7 +49,7 @@ namespace LLama | |||||
| /// <param name="logger"></param> | /// <param name="logger"></param> | ||||
| public StatelessExecutor(LLamaWeights weights, IContextParams @params, ILogger? logger = null) | public StatelessExecutor(LLamaWeights weights, IContextParams @params, ILogger? logger = null) | ||||
| { | { | ||||
| Images = new List<ImageData>(); | |||||
| Images = new List<byte[]>(); | |||||
| _weights = weights; | _weights = weights; | ||||
| _params = @params; | _params = @params; | ||||
| _logger = logger; | _logger = logger; | ||||