Browse Source

Simplifying image handling

pull/653/head
Zoli Somogyi 1 year ago
parent
commit
f4fad825c7
5 changed files with 7 additions and 66 deletions
  1. +1
    -1
      LLama.Examples/Examples/LlavaInteractiveModeExecute.cs
  2. +1
    -43
      LLama/Abstractions/ILLamaExecutor.cs
  3. +2
    -2
      LLama/LLamaExecutorBase.cs
  4. +1
    -18
      LLama/LLamaInteractExecutor.cs
  5. +2
    -2
      LLama/LLamaStatelessExecutor.cs

+ 1
- 1
LLama.Examples/Examples/LlavaInteractiveModeExecute.cs View File

@@ -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));
} }
} }




+ 1
- 43
LLama/Abstractions/ILLamaExecutor.cs View File

@@ -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; }
}
} }

+ 2
- 2
LLama/LLamaExecutorBase.cs View File

@@ -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;


+ 1
- 18
LLama/LLamaInteractExecutor.cs View File

@@ -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>");


+ 2
- 2
LLama/LLamaStatelessExecutor.cs View File

@@ -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;


Loading…
Cancel
Save