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.

UserSettings.cs 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Spectre.Console;
  2. namespace LLama.Examples;
  3. internal static class UserSettings
  4. {
  5. private static readonly string SettingsModelPath = Path.Join(AppContext.BaseDirectory, "DefaultModel.env");
  6. private static readonly string SettingsMMprojPath = Path.Join(AppContext.BaseDirectory, "DefaultMMProj.env");
  7. private static readonly string SettingsImagePath = Path.Join(AppContext.BaseDirectory, "DefaultImage.env");
  8. private static string? ReadDefaultPath(string file)
  9. {
  10. if (!File.Exists(file))
  11. return null;
  12. string path = File.ReadAllText(file).Trim();
  13. if (!File.Exists(path))
  14. return null;
  15. return path;
  16. }
  17. private static void WriteDefaultPath(string settings, string path)
  18. {
  19. File.WriteAllText(settings, path);
  20. }
  21. public static string GetModelPath(bool alwaysPrompt = false)
  22. {
  23. var defaultPath = ReadDefaultPath(SettingsModelPath);
  24. var path = defaultPath is null || alwaysPrompt
  25. ? PromptUserForPath()
  26. : PromptUserForPathWithDefault(defaultPath);
  27. if (File.Exists(path))
  28. WriteDefaultPath(SettingsModelPath, path);
  29. return path;
  30. }
  31. // TODO: Refactorize
  32. public static string GetMMProjPath(bool alwaysPrompt = false)
  33. {
  34. var defaultPath = ReadDefaultPath(SettingsMMprojPath);
  35. var path = defaultPath is null || alwaysPrompt
  36. ? PromptUserForPath("MMProj")
  37. : PromptUserForPathWithDefault(defaultPath, "MMProj");
  38. if (File.Exists(path))
  39. WriteDefaultPath(SettingsMMprojPath, path);
  40. return path;
  41. }
  42. // TODO: Refactorize
  43. public static string GetImagePath(bool alwaysPrompt = false)
  44. {
  45. var defaultPath = ReadDefaultPath(SettingsImagePath);
  46. var path = defaultPath is null || alwaysPrompt
  47. ? PromptUserForPath("image")
  48. : PromptUserForPathWithDefault(defaultPath, "image");
  49. if (File.Exists(path))
  50. WriteDefaultPath(SettingsImagePath, path);
  51. return path;
  52. }
  53. private static string PromptUserForPath(string text = "model")
  54. {
  55. return AnsiConsole.Prompt(
  56. new TextPrompt<string>(string.Format("Please input your {0} path:", text) )
  57. .PromptStyle("white")
  58. .Validate(File.Exists, string.Format("[red]ERROR: invalid {0} file path - file does not exist[/]", text) )
  59. );
  60. }
  61. private static string PromptUserForPathWithDefault(string defaultPath, string text = "model")
  62. {
  63. return AnsiConsole.Prompt(
  64. new TextPrompt<string>(string.Format("Please input your {0} path (or ENTER for default):", text) )
  65. .DefaultValue(defaultPath)
  66. .PromptStyle("white")
  67. .Validate(File.Exists, string.Format("[red]ERROR: invalid {0} file path - file does not exist[/]", text))
  68. );
  69. }
  70. }