forked from SciSharp/LLamaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserSettings.cs
80 lines (65 loc) · 2.71 KB
/
UserSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Spectre.Console;
namespace LLama.Examples;
internal static class UserSettings
{
private static readonly string SettingsModelPath = Path.Join(AppContext.BaseDirectory, "DefaultModel.env");
private static readonly string SettingsMMprojPath = Path.Join(AppContext.BaseDirectory, "DefaultMMProj.env");
private static readonly string SettingsImagePath = Path.Join(AppContext.BaseDirectory, "DefaultImage.env");
private static readonly string WhisperModelPath = Path.Join(AppContext.BaseDirectory, "DefaultWhisper.env");
private static string? ReadDefaultPath(string file)
{
if (!File.Exists(file))
return null;
string path = File.ReadAllText(file).Trim();
if (!File.Exists(path))
return null;
return path;
}
public static string GetModelPath(bool alwaysPrompt = false)
{
return PromptPath("model.gguf", SettingsModelPath, alwaysPrompt);
}
public static string GetMMProjPath(bool alwaysPrompt = false)
{
return PromptPath("mmproj", SettingsMMprojPath, alwaysPrompt);
}
public static string GetImagePath(bool alwaysPrompt = false)
{
return PromptPath("image", SettingsImagePath, alwaysPrompt);
}
public static string GetWhisperPath(bool alwaysPrompt = false)
{
return PromptPath("whisper model.bin", WhisperModelPath, alwaysPrompt);
}
private static string PromptPath(string label, string saveFile, bool alwaysPrompt)
{
var defaultPath = ReadDefaultPath(saveFile);
var path = defaultPath is null || alwaysPrompt
? PromptUserForPath(label)
: PromptUserForPathWithDefault(defaultPath, label);
if (File.Exists(path))
WriteDefaultPath(saveFile, path);
return path;
static void WriteDefaultPath(string settings, string path)
{
File.WriteAllText(settings, path);
}
static string PromptUserForPath(string text = "model")
{
return AnsiConsole.Prompt(
new TextPrompt<string>($"Please input your {text} path:")
.PromptStyle("white")
.Validate(File.Exists, $"[red]ERROR: invalid {text} file path - file does not exist[/]")
);
}
static string PromptUserForPathWithDefault(string defaultPath, string text = "model")
{
return AnsiConsole.Prompt(
new TextPrompt<string>($"Please input your {text} path (or ENTER for default):")
.DefaultValue(defaultPath)
.PromptStyle("white")
.Validate(File.Exists, $"[red]ERROR: invalid {text} file path - file does not exist[/]")
);
}
}
}