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.

LlavaInteractiveModeExecute.cs 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Text.RegularExpressions;
  2. using LLama.Batched;
  3. using LLama.Common;
  4. using Spectre.Console;
  5. using LLama.Abstractions;
  6. namespace LLama.Examples.Examples
  7. {
  8. // This example shows how to chat with LLaVA model with both image and text as input.
  9. // It uses the interactive executor to inference.
  10. public class LlavaInteractiveModeExecute
  11. {
  12. public static async Task Run()
  13. {
  14. string multiModalProj = UserSettings.GetMMProjPath();
  15. string modelPath = UserSettings.GetModelPath();
  16. string modelImage = UserSettings.GetImagePath();
  17. const int maxTokens = 1024;
  18. var prompt = $"{{{modelImage}}}\nUSER:\nProvide a full description of the image.\nASSISTANT:\n";
  19. var parameters = new ModelParams(modelPath)
  20. {
  21. ContextSize = 4096,
  22. Seed = 1337,
  23. GpuLayerCount = 10
  24. };
  25. using var model = LLamaWeights.LoadFromFile(parameters);
  26. using var context = model.CreateContext(parameters);
  27. // Llava Init
  28. using var clipModel = LLavaWeights.LoadFromFile(multiModalProj);
  29. var ex = new InteractiveExecutor(context, clipModel );
  30. Console.ForegroundColor = ConsoleColor.Yellow;
  31. Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to {0} and the context size is {1}.", maxTokens, parameters.ContextSize );
  32. Console.WriteLine("To send an image, enter its filename in curly braces, like this {c:/image.jpg}.");
  33. var inferenceParams = new InferenceParams() { Temperature = 0.1f, AntiPrompts = new List<string> { "\nUSER:" }, MaxTokens = maxTokens };
  34. do
  35. {
  36. // Evaluate if we have images
  37. //
  38. var imageMatches = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Value);
  39. var imageCount = imageMatches.Count();
  40. var hasImages = imageCount > 0;
  41. byte[][] imageBytes = null;
  42. if (hasImages)
  43. {
  44. var imagePathsWithCurlyBraces = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Value);
  45. var imagePaths = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Groups[1].Value);
  46. try
  47. {
  48. imageBytes = imagePaths.Select(File.ReadAllBytes).ToArray();
  49. }
  50. catch (IOException exception)
  51. {
  52. Console.ForegroundColor = ConsoleColor.Red;
  53. Console.Write(
  54. $"Could not load your {(imageCount == 1 ? "image" : "images")}:");
  55. Console.Write($"{exception.Message}");
  56. Console.ForegroundColor = ConsoleColor.Yellow;
  57. Console.WriteLine("Please try again.");
  58. break;
  59. }
  60. int index = 0;
  61. foreach (var path in imagePathsWithCurlyBraces)
  62. {
  63. // First image replace to tag <image, the rest of the images delete the tag
  64. if (index++ == 0)
  65. prompt = prompt.Replace(path, "<image>");
  66. else
  67. prompt = prompt.Replace(path, "");
  68. }
  69. Console.ForegroundColor = ConsoleColor.Yellow;
  70. Console.WriteLine($"Here are the images, that are sent to the chat model in addition to your message.");
  71. Console.WriteLine();
  72. foreach (var consoleImage in imageBytes?.Select(bytes => new CanvasImage(bytes)))
  73. {
  74. consoleImage.MaxWidth = 50;
  75. AnsiConsole.Write(consoleImage);
  76. }
  77. Console.WriteLine();
  78. Console.ForegroundColor = ConsoleColor.Yellow;
  79. Console.WriteLine($"The images were scaled down for the console only, the model gets full versions.");
  80. Console.WriteLine($"Write /exit or press Ctrl+c to return to main menu.");
  81. Console.WriteLine();
  82. // Initilize Images in executor
  83. //
  84. foreach (var image in imagePaths)
  85. {
  86. ex.Images.Add(File.ReadAllBytes(image));
  87. }
  88. }
  89. Console.ForegroundColor = Color.White;
  90. await foreach (var text in ex.InferAsync(prompt, inferenceParams))
  91. {
  92. Console.Write(text);
  93. }
  94. Console.Write(" ");
  95. Console.ForegroundColor = ConsoleColor.Green;
  96. prompt = Console.ReadLine();
  97. Console.WriteLine();
  98. // let the user finish with exit
  99. //
  100. if (prompt.Equals("/exit", StringComparison.OrdinalIgnoreCase))
  101. break;
  102. }
  103. while(true);
  104. }
  105. }
  106. }