-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extension LLava with in memory images #653
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
using LLama.Exceptions; | ||
using LLama.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using System.Net.Http; | ||
|
||
namespace LLama | ||
{ | ||
|
@@ -100,7 +101,7 @@ | |
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) | ||
{ | ||
var state = await JsonSerializer.DeserializeAsync<InteractiveExecutorState>(fs); | ||
await LoadState(state); | ||
Check warning on line 104 in LLama/LLamaInteractExecutor.cs GitHub Actions / Test (linux-release)
|
||
} | ||
} | ||
|
||
|
@@ -154,15 +155,21 @@ | |
{ | ||
if (image.Type == ImageData.DataType.ImagePath && image.Data != null) | ||
{ | ||
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromFileName(ClipModel.NativeHandle, Context, image.Data.ToString())); | ||
_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) | ||
{ | ||
throw new NotImplementedException(); | ||
using var httpClient = new HttpClient(); | ||
var uri = new Uri((string)image.Data); | ||
var imageBytes = httpClient.GetByteArrayAsync(uri).Result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
if (imageBytes != null && imageBytes.Length > 0) | ||
{ | ||
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, imageBytes)); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -191,11 +198,11 @@ | |
/// <param name="inferenceParams"></param> | ||
/// <param name="args"></param> | ||
/// <returns></returns> | ||
protected override async Task<(bool, IReadOnlyList<string>)> PostProcess(IInferenceParams inferenceParams, InferStateArgs args) | ||
Check warning on line 201 in LLama/LLamaInteractExecutor.cs GitHub Actions / Test (linux-release)
Check warning on line 201 in LLama/LLamaInteractExecutor.cs GitHub Actions / Test (osx-release)
|
||
{ | ||
if (_embed_inps.Count <= _consumedTokensCount) | ||
{ | ||
if (_last_n_tokens.TokensEndsWithAnyString(args.Antiprompts, Context.NativeHandle.ModelHandle, Context.Encoding)) | ||
Check warning on line 205 in LLama/LLamaInteractExecutor.cs GitHub Actions / Test (linux-release)
Check warning on line 205 in LLama/LLamaInteractExecutor.cs GitHub Actions / Test (osx-release)
|
||
args.WaitForInput = true; | ||
|
||
if (_pastTokensCount > 0 && args.WaitForInput) | ||
|
@@ -242,7 +249,7 @@ | |
|
||
// Images | ||
foreach( var image in _imageEmbedHandles ) | ||
ClipModel.EvalImageEmbed(Context, image, ref _pastTokensCount); | ||
|
||
// Post-image Tokens | ||
end = Context.NativeHandle.Decode(_embeds.GetRange(_EmbedImagePosition, _embeds.Count - _EmbedImagePosition), LLamaSeqId.Zero, batch, ref _pastTokensCount); | ||
|
@@ -274,7 +281,7 @@ | |
if (!string.IsNullOrEmpty(_pathSession) && args.NeedToSaveSession) | ||
{ | ||
args.NeedToSaveSession = false; | ||
SaveSessionFile(_pathSession); | ||
} | ||
|
||
LLamaToken id; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can all of this logic be moved out of the executor? Maybe a base interface with separate classes for the different variations.
For example:
e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the same after adding more and more to the code. The reading/downloading of the image should be the task of the user of the library.