forked from SciSharp/LLamaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSettings.cs
More file actions
87 lines (70 loc) · 2.88 KB
/
UserSettings.cs
File metadata and controls
87 lines (70 loc) · 2.88 KB
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
81
82
83
84
85
86
87
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 string? ReadDefaultPath(string file)
{
if (!File.Exists(file))
return null;
string path = File.ReadAllText(file).Trim();
if (!File.Exists(path))
return null;
return path;
}
private static void WriteDefaultPath(string settings, string path)
{
File.WriteAllText(settings, path);
}
public static string GetModelPath(bool alwaysPrompt = false)
{
var defaultPath = ReadDefaultPath(SettingsModelPath);
var path = defaultPath is null || alwaysPrompt
? PromptUserForPath()
: PromptUserForPathWithDefault(defaultPath);
if (File.Exists(path))
WriteDefaultPath(SettingsModelPath, path);
return path;
}
// TODO: Refactorize
public static string GetMMProjPath(bool alwaysPrompt = false)
{
var defaultPath = ReadDefaultPath(SettingsMMprojPath);
var path = defaultPath is null || alwaysPrompt
? PromptUserForPath("MMProj")
: PromptUserForPathWithDefault(defaultPath, "MMProj");
if (File.Exists(path))
WriteDefaultPath(SettingsMMprojPath, path);
return path;
}
// TODO: Refactorize
public static string GetImagePath(bool alwaysPrompt = false)
{
var defaultPath = ReadDefaultPath(SettingsImagePath);
var path = defaultPath is null || alwaysPrompt
? PromptUserForPath("image")
: PromptUserForPathWithDefault(defaultPath, "image");
if (File.Exists(path))
WriteDefaultPath(SettingsImagePath, path);
return path;
}
private static string PromptUserForPath(string text = "model")
{
return AnsiConsole.Prompt(
new TextPrompt<string>(string.Format("Please input your {0} path:", text) )
.PromptStyle("white")
.Validate(File.Exists, string.Format("[red]ERROR: invalid {0} file path - file does not exist[/]", text) )
);
}
private static string PromptUserForPathWithDefault(string defaultPath, string text = "model")
{
return AnsiConsole.Prompt(
new TextPrompt<string>(string.Format("Please input your {0} path (or ENTER for default):", text) )
.DefaultValue(defaultPath)
.PromptStyle("white")
.Validate(File.Exists, string.Format("[red]ERROR: invalid {0} file path - file does not exist[/]", text))
);
}
}