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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. };
  23. using var model = LLamaWeights.LoadFromFile(parameters);
  24. using var context = model.CreateContext(parameters);
  25. // Llava Init
  26. using var clipModel = LLavaWeights.LoadFromFile(multiModalProj);
  27. var ex = new InteractiveExecutor(context, clipModel );
  28. Console.ForegroundColor = ConsoleColor.Yellow;
  29. 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 );
  30. Console.WriteLine("To send an image, enter its filename in curly braces, like this {c:/image.jpg}.");
  31. var inferenceParams = new InferenceParams() { Temperature = 0.1f, AntiPrompts = new List<string> { "\nUSER:" }, MaxTokens = maxTokens };
  32. do
  33. {
  34. // Evaluate if we have images
  35. //
  36. var imageMatches = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Value);
  37. var imageCount = imageMatches.Count();
  38. var hasImages = imageCount > 0;
  39. byte[][] imageBytes = null;
  40. if (hasImages)
  41. {
  42. var imagePathsWithCurlyBraces = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Value);
  43. var imagePaths = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Groups[1].Value);
  44. try
  45. {
  46. imageBytes = imagePaths.Select(File.ReadAllBytes).ToArray();
  47. }
  48. catch (IOException exception)
  49. {
  50. Console.ForegroundColor = ConsoleColor.Red;
  51. Console.Write(
  52. $"Could not load your {(imageCount == 1 ? "image" : "images")}:");
  53. Console.Write($"{exception.Message}");
  54. Console.ForegroundColor = ConsoleColor.Yellow;
  55. Console.WriteLine("Please try again.");
  56. break;
  57. }
  58. int index = 0;
  59. foreach (var path in imagePathsWithCurlyBraces)
  60. {
  61. // First image replace to tag <image, the rest of the images delete the tag
  62. if (index++ == 0)
  63. prompt = prompt.Replace(path, "<image>");
  64. else
  65. prompt = prompt.Replace(path, "");
  66. }
  67. Console.ForegroundColor = ConsoleColor.Yellow;
  68. Console.WriteLine($"Here are the images, that are sent to the chat model in addition to your message.");
  69. Console.WriteLine();
  70. foreach (var consoleImage in imageBytes?.Select(bytes => new CanvasImage(bytes)))
  71. {
  72. consoleImage.MaxWidth = 50;
  73. AnsiConsole.Write(consoleImage);
  74. }
  75. Console.WriteLine();
  76. Console.ForegroundColor = ConsoleColor.Yellow;
  77. Console.WriteLine($"The images were scaled down for the console only, the model gets full versions.");
  78. Console.WriteLine($"Write /exit or press Ctrl+c to return to main menu.");
  79. Console.WriteLine();
  80. // Initilize Images in executor
  81. //
  82. ex.ImagePaths = imagePaths.ToList();
  83. }
  84. Console.ForegroundColor = Color.White;
  85. await foreach (var text in ex.InferAsync(prompt, inferenceParams))
  86. {
  87. Console.Write(text);
  88. }
  89. Console.Write(" ");
  90. Console.ForegroundColor = ConsoleColor.Green;
  91. prompt = Console.ReadLine();
  92. Console.WriteLine();
  93. // let the user finish with exit
  94. //
  95. if (prompt.Equals("/exit", StringComparison.OrdinalIgnoreCase))
  96. break;
  97. }
  98. while(true);
  99. }
  100. }
  101. }