Skip to content

Commit e007b0c

Browse files
committed
[feat] editor window
1 parent 1b4b499 commit e007b0c

File tree

6 files changed

+277
-172
lines changed

6 files changed

+277
-172
lines changed

Editor/OllamaSupportWindow.cs

Lines changed: 54 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,37 @@
1-
using System.Collections.Generic;
21
using UnityEditor;
32
using UnityEngine;
4-
using System.Diagnostics;
53
using System.Linq;
6-
using Debug = UnityEngine.Debug;
4+
using System.Diagnostics;
5+
using Debug = UnityEngine.Debug;
6+
using System.Text.RegularExpressions;
77

88
public class OllamaSupportWindow : EditorWindow
99
{
10+
private OllamaSupport ollama;
1011
private Texture2D logo;
11-
private bool isOllamaInstalled = false;
12-
private bool isPlatformSupported = true;
13-
private string platformName;
14-
15-
private string downloadUrl;
16-
private readonly string downloadUrlMac = "https://ollama.com/download/mac";
17-
private readonly string downloadUrlLinux = "https://ollama.com/download/linux";
18-
private readonly string downloadUrlWindows = "https://ollama.com/download/windows";
19-
20-
public class ModelInfo
21-
{
22-
public string model;
23-
public string parameters;
24-
public string size;
25-
public string name;
26-
public bool isDownloaded;
2712

28-
public string DisplayName => $"{model} {parameters} ({size}) {(isDownloaded ? " [Downloaded]" : "")}";
29-
}
30-
31-
private int selectedModelIndex = 0;
32-
33-
List<ModelInfo> models = new ()
34-
{
35-
new () { model = "DeepSeek-R1", parameters = "1.5B", size = "1.1GB", name = "deepseek-r1:1.5b" },
36-
new () { model = "DeepSeek-R1", parameters = "7B", size = "4.7GB", name = "deepseek-r1:7b" },
37-
new () { model = "DeepSeek-R1", parameters = "8B", size = "4.9GB", name = "deepseek-r1:8b" },
38-
new () { model = "DeepSeek-R1", parameters = "14B", size = "9GB", name = "deepseek-r1:14b" },
39-
40-
new () { model = "Phi 4", parameters = "14B", size = "9.1GB", name = "phi4" },
41-
new () { model = "Phi 3", parameters = "3.8B", size = "2.2GB", name = "phi3" },
42-
new () { model = "Phi 3", parameters = "14B", size = "7.9GB", name = "phi3:14b" },
43-
44-
new () { model = "Llama 3.2", parameters = "1B", size = "1.3GB", name = "llama3.2:1b" },
45-
new () { model = "Llama 3.2", parameters = "3B", size = "2.0GB", name = "llama3.2:3b" },
46-
new () { model = "Llama 3.1", parameters = "8B", size = "4.7GB", name = "llama3.1" },
13+
private int selectedModelIndex;
4714

48-
new () { model = "Gemma 2", parameters = "2B", size = "1.6GB", name = "gemma2:2b" },
49-
new () { model = "Gemma 2", parameters = "9B", size = "5.5GB", name = "gemma2" },
50-
new () { model = "Gemma 2", parameters = "27B", size = "16GB", name = "gemma2:27b" },
51-
52-
new () { model = "Mistral", parameters = "7B", size = "4.1GB", name = "mistral" },
53-
};
15+
private bool isDownloading;
5416

5517
[MenuItem("Tools/Neocortex/Ollama Support")]
5618
public static void ShowWindow()
5719
{
5820
var window = GetWindow<OllamaSupportWindow>(false, "Neocortex Ollama Support", true);
59-
window.minSize = new Vector2(512, 512);
60-
window.maxSize = new Vector2(512, 512);
21+
window.minSize = new Vector2(512, 352);
22+
window.maxSize = new Vector2(512, 352);
6123
}
6224

6325
private void OnEnable()
6426
{
27+
ollama = new OllamaSupport();
6528
logo = Resources.Load<Texture2D>("Visuals/ollama_x_neocortex");
66-
SetPlatformDependedStrings();
67-
CheckOllamaInstallation();
68-
CheckInstalledModels();
29+
ollama.CheckOllamaInstallation();
30+
ollama.CheckInstalledModels();
31+
ollama.SetPlatformDependedStrings();
6932
}
7033

71-
private void OnGUI()
34+
private async void OnGUI()
7235
{
7336
if (logo)
7437
{
@@ -85,151 +48,72 @@ private void OnGUI()
8548
}
8649

8750
EditorGUILayout.BeginVertical("Box");
88-
89-
EditorGUILayout.HelpBox("This window provides support for Ollama." +
90-
"\nClick the button below to download the necessary tools.", MessageType.Info);
9151

92-
string buttonMessage = isPlatformSupported
93-
? isOllamaInstalled ? "Ollama is Already Installed" : $"Download Ollama for {platformName}"
94-
: "Platform Unsupported";
95-
GUI.enabled = !isOllamaInstalled && isPlatformSupported;
96-
if (GUILayout.Button(buttonMessage, GUILayout.Height(30)))
97-
{
98-
Application.OpenURL(downloadUrl);
99-
}
100-
GUI.enabled = true;
101-
102-
GUILayout.Space(10);
103-
104-
EditorGUILayout.HelpBox("At least 8 GB RAM is needed for 7B models, 16 GB for 13B models, and 32 GB for 33B models." +
105-
$"\nCurrent Device: VRAM: {SystemInfo.graphicsMemorySize/1024f:F1} GB, RAM: {SystemInfo.systemMemorySize/1024f:F1} GB", MessageType.Warning);
106-
107-
selectedModelIndex = EditorGUILayout.Popup("Select Model:", selectedModelIndex, models.Select(m => m.DisplayName).ToArray());
52+
EditorGUILayout.HelpBox("This window provides support for Ollama.\nClick the button below to download the necessary tools.", MessageType.Info);
10853

109-
bool isSelectedModelDownloaded = models[selectedModelIndex].isDownloaded;
110-
GUI.enabled = !isSelectedModelDownloaded;
111-
if (GUILayout.Button(isSelectedModelDownloaded ? "Model is Already Downloaded" : "Download Model", GUILayout.Height(30)))
54+
string buttonMessage = ollama.IsOllamaInstalled ? "Ollama is Already Installed" : $"Download Ollama for {ollama.PlatformName}";
55+
56+
GUI.enabled = !ollama.IsOllamaInstalled;
57+
if (GUILayout.Button(buttonMessage, GUILayout.Height(30)))
11258
{
113-
DownloadModel();
59+
Application.OpenURL(ollama.DownloadUrl);
11460
}
11561
GUI.enabled = true;
11662

117-
EditorGUILayout.EndVertical();
118-
}
63+
GUILayout.Space(10);
11964

120-
private void CheckOllamaInstallation()
121-
{
122-
ProcessStartInfo psi = new ProcessStartInfo
123-
{
124-
FileName = "cmd.exe",
125-
Arguments = "where ollama",
126-
RedirectStandardOutput = true,
127-
UseShellExecute = false,
128-
CreateNoWindow = true
129-
};
65+
EditorGUILayout.HelpBox(
66+
"At least 8 GB RAM is needed for 7B models, 16 GB for 13B models, and 32 GB for 33B models." +
67+
$"\nCurrent Device: VRAM: {SystemInfo.graphicsMemorySize / 1024f:F1} GB, RAM: {SystemInfo.systemMemorySize / 1024f:F1} GB",
68+
MessageType.Warning);
13069

131-
using (Process process = new Process { StartInfo = psi })
132-
{
133-
process.Start();
134-
string output = process.StandardOutput.ReadToEnd();
135-
process.WaitForExit();
70+
selectedModelIndex = EditorGUILayout.Popup("Select Model:", selectedModelIndex, ollama.Models.Select(m => m.DisplayName).ToArray());
13671

137-
isOllamaInstalled = !string.IsNullOrEmpty(output);
138-
}
139-
}
140-
141-
private void CheckInstalledModels()
142-
{
143-
if (isOllamaInstalled)
72+
bool isSelectedModelDownloaded = ollama.Models[selectedModelIndex].isDownloaded;
73+
GUI.enabled = !isSelectedModelDownloaded;
74+
if (GUILayout.Button("Download Model", GUILayout.Height(30)))
14475
{
145-
ProcessStartInfo psi = new ProcessStartInfo
76+
if (isDownloading)
14677
{
147-
FileName = "ollama.exe",
148-
Arguments = "list",
149-
RedirectStandardOutput = true,
150-
UseShellExecute = false,
151-
CreateNoWindow = true
152-
};
153-
154-
using (Process process = new Process { StartInfo = psi })
78+
Debug.LogWarning("A download is already in progress.");
79+
}
80+
else
15581
{
156-
process.Start();
157-
string output = process.StandardOutput.ReadToEnd();
158-
process.WaitForExit();
159-
160-
List<string> downloadedModels = ExtractModelNames(output);
161-
162-
foreach (var model in models)
82+
EditorApplication.update += UpdateProgressBar;
83+
isDownloading = true;
84+
ollama.DownloadModel(selectedModelIndex, () =>
16385
{
164-
model.isDownloaded = downloadedModels.Contains(model.name);
165-
}
86+
isDownloading = false;
87+
ollama.CheckInstalledModels();
88+
});
16689
}
16790
}
168-
}
169-
170-
private void DownloadModel()
171-
{
172-
ProcessStartInfo psi = new ProcessStartInfo
173-
{
174-
FileName = "ollama.exe",
175-
Arguments = "pull " + models[selectedModelIndex].name,
176-
RedirectStandardOutput = true,
177-
UseShellExecute = false,
178-
CreateNoWindow = true
179-
};
180-
181-
using (Process process = new Process { StartInfo = psi })
91+
GUI.enabled = true;
92+
93+
GUI.enabled = isSelectedModelDownloaded;
94+
if (GUILayout.Button("Delete Model", GUILayout.Height(30)))
18295
{
183-
process.Start();
184-
string output = process.StandardOutput.ReadToEnd();
185-
process.WaitForExit();
186-
187-
Debug.Log(output);
96+
ollama.DeleteModel(selectedModelIndex);
97+
ollama.CheckInstalledModels();
18898
}
99+
GUI.enabled = true;
100+
101+
EditorGUILayout.EndVertical();
189102
}
190103

191-
private List<string> ExtractModelNames(string rawText)
104+
private void UpdateProgressBar()
192105
{
193-
List<string> modelNames = new List<string>();
194-
string[] lines = rawText.Split('\n');
195-
196-
for (int i = 1; i < lines.Length; i++)
106+
if (isDownloading)
197107
{
198-
string[] columns = lines[i].Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
199-
if (columns.Length > 0)
108+
if (EditorUtility.DisplayCancelableProgressBar("Downloading Model", ollama.ProgressText, ollama.ProgressValue))
200109
{
201-
modelNames.Add(columns[0]);
110+
isDownloading = false;
111+
ollama.CancelRequest();
202112
}
203113
}
204-
205-
return modelNames;
206-
}
207-
208-
private void SetPlatformDependedStrings()
209-
{
210-
if (SystemInfo.operatingSystem.Contains("Windows"))
211-
{
212-
downloadUrl = downloadUrlWindows;
213-
platformName = "Windows";
214-
return;
215-
}
216-
217-
if (SystemInfo.operatingSystem.Contains("Linux"))
218-
{
219-
downloadUrl = downloadUrlLinux;
220-
platformName = "Linux";
221-
return;
222-
}
223-
224-
if (SystemInfo.operatingSystem.Contains("Mac OS"))
114+
else
225115
{
226-
downloadUrl = downloadUrlMac;
227-
platformName = "Mac OS";
228-
return;
116+
EditorUtility.ClearProgressBar();
229117
}
230-
231-
isPlatformSupported = false;
232-
platformName = "Unknown";
233118
}
234119
}
235-

Runtime/ModelInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class ModelInfo
2+
{
3+
public string model;
4+
public string parameters;
5+
public string size;
6+
public string name;
7+
public bool isDownloaded;
8+
9+
public string DisplayName => $"{model} {parameters} ({size}) {(isDownloaded ? " [Downloaded]" : "")}";
10+
}

Runtime/ModelInfo.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)