Skip to content

Commit f4fad82

Browse files
committed
Simplifying image handling
1 parent 44a82b0 commit f4fad82

File tree

5 files changed

+7
-66
lines changed

5 files changed

+7
-66
lines changed

LLama.Examples/Examples/LlavaInteractiveModeExecute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static async Task Run()
102102
//
103103
foreach (var image in imagePaths)
104104
{
105-
ex.Images.Add(new ImageData(ImageData.DataType.ImagePath, image));
105+
ex.Images.Add(File.ReadAllBytes(image));
106106
}
107107
}
108108

LLama/Abstractions/ILLamaExecutor.cs

+1-43
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface ILLamaExecutor
2727
/// <summary>
2828
/// List of images: Image filen path, uri or image byte array. See ImageData.
2929
/// </summary>
30-
public List<ImageData> Images { get; }
30+
public List<byte[]> Images { get; }
3131

3232
/// <summary>
3333
/// Asynchronously infers a response from the model.
@@ -38,46 +38,4 @@ public interface ILLamaExecutor
3838
/// <returns></returns>
3939
IAsyncEnumerable<string> InferAsync(string text, IInferenceParams? inferenceParams = null, CancellationToken token = default);
4040
}
41-
42-
/// <summary>
43-
/// Holds image data
44-
/// </summary>
45-
public class ImageData
46-
{
47-
/// <summary>
48-
/// constructor
49-
/// </summary>
50-
/// <param name="type"></param>
51-
/// <param name="data"></param>
52-
public ImageData(DataType type, object data) { Type = type; Data = data; }
53-
54-
/// <summary>
55-
/// the possible types of image data
56-
/// </summary>
57-
public enum DataType
58-
{
59-
/// <summary>
60-
/// file path
61-
/// </summary>
62-
ImagePath,
63-
/// <summary>
64-
/// byte array
65-
/// </summary>
66-
ImageBytes,
67-
/// <summary>
68-
/// uri
69-
/// </summary>
70-
ImageURL
71-
}
72-
73-
/// <summary>
74-
/// the type of this image data
75-
/// </summary>
76-
public DataType Type { get; set; }
77-
78-
/// <summary>
79-
/// the image data (string, byte array or uri)
80-
/// </summary>
81-
public object? Data { get; set; }
82-
}
8341
}

LLama/LLamaExecutorBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public bool IsMultiModal
7979
public LLavaWeights? ClipModel { get; }
8080

8181
/// <inheritdoc />
82-
public List<ImageData> Images { get; set; }
82+
public List<byte[]> Images { get; set; }
8383

8484
/// <summary>
8585
/// Current "mu" value for mirostat sampling
@@ -95,7 +95,7 @@ public bool IsMultiModal
9595
/// <param name="logger"></param>
9696
protected StatefulExecutorBase(LLamaContext context, ILogger? logger = null)
9797
{
98-
Images = new List<ImageData>();
98+
Images = new List<byte[]>();
9999
_logger = logger;
100100
Context = context;
101101
_pastTokensCount = 0;

LLama/LLamaInteractExecutor.cs

+1-18
Original file line numberDiff line numberDiff line change
@@ -153,24 +153,7 @@ private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = tru
153153
{
154154
foreach (var image in Images)
155155
{
156-
if (image.Type == ImageData.DataType.ImagePath && image.Data != null)
157-
{
158-
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromFileName(ClipModel.NativeHandle, Context, (string)image.Data));
159-
}
160-
else if (image.Type == ImageData.DataType.ImageBytes && image.Data != null)
161-
{
162-
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, (byte[])image.Data));
163-
}
164-
else if (image.Type == ImageData.DataType.ImageURL && image.Data != null)
165-
{
166-
using var httpClient = new HttpClient();
167-
var uri = new Uri((string)image.Data);
168-
var imageBytes = httpClient.GetByteArrayAsync(uri).Result;
169-
if (imageBytes != null && imageBytes.Length > 0)
170-
{
171-
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, imageBytes));
172-
}
173-
}
156+
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, image));
174157
}
175158

176159
int imageIndex = text.IndexOf("<image>");

LLama/LLamaStatelessExecutor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class StatelessExecutor
3434
public LLavaWeights? ClipModel { get; }
3535

3636
/// <inheritdoc />
37-
public List<ImageData> Images { get; set; }
37+
public List<byte[]> Images { get; set; }
3838

3939
/// <summary>
4040
/// The context used by the executor when running the inference.
@@ -49,7 +49,7 @@ public class StatelessExecutor
4949
/// <param name="logger"></param>
5050
public StatelessExecutor(LLamaWeights weights, IContextParams @params, ILogger? logger = null)
5151
{
52-
Images = new List<ImageData>();
52+
Images = new List<byte[]>();
5353
_weights = weights;
5454
_params = @params;
5555
_logger = logger;

0 commit comments

Comments
 (0)