1
- using System . Collections . Generic ;
2
1
using UnityEditor ;
3
2
using UnityEngine ;
4
- using System . Diagnostics ;
5
3
using System . Linq ;
6
- using Debug = UnityEngine . Debug ;
4
+ using System . Diagnostics ;
5
+ using Debug = UnityEngine . Debug ;
6
+ using System . Text . RegularExpressions ;
7
7
8
8
public class OllamaSupportWindow : EditorWindow
9
9
{
10
+ private OllamaSupport ollama ;
10
11
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 ;
27
12
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 ;
47
14
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 ;
54
16
55
17
[ MenuItem ( "Tools/Neocortex/Ollama Support" ) ]
56
18
public static void ShowWindow ( )
57
19
{
58
20
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 ) ;
61
23
}
62
24
63
25
private void OnEnable ( )
64
26
{
27
+ ollama = new OllamaSupport ( ) ;
65
28
logo = Resources . Load < Texture2D > ( "Visuals/ollama_x_neocortex" ) ;
66
- SetPlatformDependedStrings ( ) ;
67
- CheckOllamaInstallation ( ) ;
68
- CheckInstalledModels ( ) ;
29
+ ollama . CheckOllamaInstallation ( ) ;
30
+ ollama . CheckInstalledModels ( ) ;
31
+ ollama . SetPlatformDependedStrings ( ) ;
69
32
}
70
33
71
- private void OnGUI ( )
34
+ private async void OnGUI ( )
72
35
{
73
36
if ( logo )
74
37
{
@@ -85,151 +48,72 @@ private void OnGUI()
85
48
}
86
49
87
50
EditorGUILayout . BeginVertical ( "Box" ) ;
88
-
89
- EditorGUILayout . HelpBox ( "This window provides support for Ollama." +
90
- "\n Click the button below to download the necessary tools." , MessageType . Info ) ;
91
51
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
- $ "\n Current 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.\n Click the button below to download the necessary tools." , MessageType . Info ) ;
108
53
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 ) ) )
112
58
{
113
- DownloadModel ( ) ;
59
+ Application . OpenURL ( ollama . DownloadUrl ) ;
114
60
}
115
61
GUI . enabled = true ;
116
62
117
- EditorGUILayout . EndVertical ( ) ;
118
- }
63
+ GUILayout . Space ( 10 ) ;
119
64
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
+ $ "\n Current Device: VRAM: { SystemInfo . graphicsMemorySize / 1024f : F1} GB, RAM: { SystemInfo . systemMemorySize / 1024f : F1} GB",
68
+ MessageType . Warning ) ;
130
69
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 ( ) ) ;
136
71
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 ) ) )
144
75
{
145
- ProcessStartInfo psi = new ProcessStartInfo
76
+ if ( isDownloading )
146
77
{
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
155
81
{
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 , ( ) =>
163
85
{
164
- model . isDownloaded = downloadedModels . Contains ( model . name ) ;
165
- }
86
+ isDownloading = false ;
87
+ ollama . CheckInstalledModels ( ) ;
88
+ } ) ;
166
89
}
167
90
}
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 ) ) )
182
95
{
183
- process . Start ( ) ;
184
- string output = process . StandardOutput . ReadToEnd ( ) ;
185
- process . WaitForExit ( ) ;
186
-
187
- Debug . Log ( output ) ;
96
+ ollama . DeleteModel ( selectedModelIndex ) ;
97
+ ollama . CheckInstalledModels ( ) ;
188
98
}
99
+ GUI . enabled = true ;
100
+
101
+ EditorGUILayout . EndVertical ( ) ;
189
102
}
190
103
191
- private List < string > ExtractModelNames ( string rawText )
104
+ private void UpdateProgressBar ( )
192
105
{
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 )
197
107
{
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 ) )
200
109
{
201
- modelNames . Add ( columns [ 0 ] ) ;
110
+ isDownloading = false ;
111
+ ollama . CancelRequest ( ) ;
202
112
}
203
113
}
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
225
115
{
226
- downloadUrl = downloadUrlMac ;
227
- platformName = "Mac OS" ;
228
- return ;
116
+ EditorUtility . ClearProgressBar ( ) ;
229
117
}
230
-
231
- isPlatformSupported = false ;
232
- platformName = "Unknown" ;
233
118
}
234
119
}
235
-
0 commit comments