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.2 kB

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