You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SafeLlavaImageEmbedHandle.cs 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using LLama;
  7. using LLama.Exceptions;
  8. namespace LLama.Native
  9. {
  10. /// <summary>
  11. /// A Reference to a llava Image Embed handle
  12. /// </summary>
  13. public sealed class SafeLlavaImageEmbedHandle
  14. : SafeLLamaHandleBase
  15. {
  16. private SafeLlavaImageEmbedHandle(IntPtr handle)
  17. : base(handle, true)
  18. {
  19. }
  20. private SafeLlavaImageEmbedHandle()
  21. {}
  22. /// <summary>
  23. /// Create an image embed from an image file
  24. /// </summary>
  25. /// <param name="ctxLlava"></param>
  26. /// <param name="ctxLlama"></param>
  27. /// <param name="image">Path to the image file. Supported formats:
  28. /// <list type="bullet">
  29. /// <item>JPG</item>
  30. /// <item>PNG</item>
  31. /// <item>BMP</item>
  32. /// <item>TGA</item>
  33. /// </list>
  34. /// </param>
  35. /// <returns></returns>
  36. /// <exception cref="InvalidOperationException"></exception>
  37. public static SafeLlavaImageEmbedHandle CreateFromFileName( SafeLlavaModelHandle ctxLlava, LLamaContext ctxLlama, string image )
  38. {
  39. // Try to open the image file, this will check:
  40. // - File exists (automatically throws FileNotFoundException)
  41. // - File is readable (explicit check)
  42. // This provides better error messages that llama.cpp, which would throw an access violation exception in both cases.
  43. using (var fs = new FileStream(image, FileMode.Open))
  44. if (!fs.CanRead)
  45. throw new InvalidOperationException($"Llava image file '{image}' is not readable");
  46. return NativeApi.llava_image_embed_make_with_filename(ctxLlava, (int) ctxLlama.BatchThreads, image);
  47. }
  48. /// <summary>
  49. /// Create an image embed from the bytes of an image.
  50. /// </summary>
  51. /// <param name="ctxLlava"></param>
  52. /// <param name="ctxLlama"></param>
  53. /// <param name="image">Image bytes. Supported formats:
  54. /// <list type="bullet">
  55. /// <item>JPG</item>
  56. /// <item>PNG</item>
  57. /// <item>BMP</item>
  58. /// <item>TGA</item>
  59. /// </list>
  60. /// </param>
  61. /// <returns></returns>
  62. public static SafeLlavaImageEmbedHandle CreateFromMemory( SafeLlavaModelHandle ctxLlava, LLamaContext ctxLlama, byte[] image )
  63. {
  64. return NativeApi.llava_image_embed_make_with_bytes(ctxLlava, (int) ctxLlama.BatchThreads, image, image.Length);
  65. }
  66. /// <inheritdoc />
  67. protected override bool ReleaseHandle()
  68. {
  69. NativeApi.llava_image_embed_free(DangerousGetHandle());
  70. SetHandle(IntPtr.Zero);
  71. return true;
  72. }
  73. }
  74. }