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

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