This repository was archived by the owner on Jan 22, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathHuggingFaceAPIWizard.cs
177 lines (145 loc) · 7.45 KB
/
HuggingFaceAPIWizard.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
usingSystem.IO;
usingUnityEditor;
usingUnityEngine;
namespaceHuggingFace.API.Editor{
[InitializeOnLoad]
publicclassHuggingFaceAPIWizard:EditorWindow{
privatestringstatusMessage=string.Empty;
privatestaticAPIConfigconfig;
privatestaticstringsourcePath;
privatestaticstringdestinationPath;
privatevoidOnEnable(){
sourcePath=Path.GetFullPath("Packages/com.huggingface.api/Examples");
destinationPath=Path.Combine(Application.dataPath,"HuggingFaceAPI/Examples");
}
staticHuggingFaceAPIWizard(){
EditorApplication.update+=CheckConfig;
}
privatestaticvoidCheckConfig(){
EditorApplication.update-=CheckConfig;
LoadOrCreateConfig();
if(string.IsNullOrEmpty(config.apiKey)){
ShowWindow();
}
}
privatestaticvoidLoadOrCreateConfig(){
stringresourcesPath="Assets/Resources";
if(!AssetDatabase.IsValidFolder(resourcesPath)){
AssetDatabase.CreateFolder("Assets","Resources");
}
stringconfigPath=$"{resourcesPath}/HuggingFaceAPIConfig.asset";
config=AssetDatabase.LoadAssetAtPath<APIConfig>(configPath);
if(config==null){
config=ScriptableObject.CreateInstance<APIConfig>();
AssetDatabase.CreateAsset(config,configPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
[MenuItem("Window/Hugging Face API Wizard")]
publicstaticvoidShowWindow(){
GetWindow<HuggingFaceAPIWizard>("Hugging Face API Wizard");
}
privatevoidOnGUI(){
EditorGUILayout.Space();
EditorGUILayout.LabelField("Instructions:",EditorStyles.boldLabel);
EditorGUILayout.HelpBox("1. Enter your API key. Generate keys at: https://huggingface.co/settings/profile\n2. Test the API key.\n3. Optionally, update the endpoints to use different models.",MessageType.Info);
EditorGUILayout.Space();
EditorGUILayout.LabelField("Hugging Face API Setup",EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
stringapiKey=EditorGUILayout.TextField("API Key",config.apiKey);
if(EditorGUI.EndChangeCheck()){
Undo.RecordObject(config,"Change API Key");
config.SetAPIKey(apiKey);
EditorUtility.SetDirty(config);
}
if(GUILayout.Button("Test API Key")){
statusMessage="<color=white>Waiting for API response...</color>";
Repaint();
HuggingFaceAPI.TestAPIKey(apiKey,OnSuccess,OnError);
}
EditorGUILayout.LabelField("Status:",EditorStyles.boldLabel);
EditorGUILayout.LabelField(statusMessage,newGUIStyle());
GUILayout.Space(10);
EditorGUILayout.LabelField("Task Endpoints",EditorStyles.boldLabel);
EditorGUI.indentLevel++;
for(inti=0;i<config.taskEndpoints.Count;i++){
TaskEndpointendpoint=config.taskEndpoints[i];
EditorGUI.BeginChangeCheck();
stringnewEndpoint=EditorGUILayout.TextField(endpoint.taskName,endpoint.endpoint);
if(EditorGUI.EndChangeCheck()){
Undo.RecordObject(config,"Change Task Endpoint");
config.taskEndpoints[i]=newTaskEndpoint(endpoint.taskName,newEndpoint);
EditorUtility.SetDirty(config);
}
}
EditorGUI.indentLevel--;
if(GUILayout.Button("Reset to Defaults")){
Undo.RecordObject(config,"Reset Task Endpoints");
config.InitializeTaskEndpoints();
EditorUtility.SetDirty(config);
}
EditorGUILayout.Space(10);
EditorGUILayout.LabelField("Advanced Settings",EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
varcontent=newGUIContent("Use Backup Endpoints","If the primary endpoint fails, the API will try to use the backup endpoints.");
booluseBackupEndpoints=EditorGUILayout.Toggle(content,config.useBackupEndpoints);
if(EditorGUI.EndChangeCheck()){
Undo.RecordObject(config,"Change Use Backup Endpoints");
config.SetUseBackupEndpoints(useBackupEndpoints);
EditorUtility.SetDirty(config);
}
EditorGUI.BeginChangeCheck();
content=newGUIContent("Wait for Model","If true, the API will wait for the model to load if necessary. If false, the API will send the request immediately and return an error if the model is not loaded.");
boolwaitForModel=EditorGUILayout.Toggle(content,config.waitForModel);
if(EditorGUI.EndChangeCheck()){
Undo.RecordObject(config,"Change Wait for Model");
config.SetWaitForModel(waitForModel);
EditorUtility.SetDirty(config);
}
EditorGUI.BeginChangeCheck();
content=newGUIContent("Max Timeout","The maximum time to wait for a response from the API. If the API does not respond within this time, the request will be cancelled. Set to 0 to disable timeout.");
floatmaxTimeout=EditorGUILayout.FloatField(content,config.maxTimeout);
if(EditorGUI.EndChangeCheck()){
Undo.RecordObject(config,"Change Max Timeout");
config.SetMaxTimeout(maxTimeout);
EditorUtility.SetDirty(config);
}
GUILayout.Space(10);
EditorGUILayout.LabelField("Examples",EditorStyles.boldLabel);
boolexamplesInstalled=Directory.Exists(destinationPath);
EditorGUI.BeginDisabledGroup(examplesInstalled);
if(GUILayout.Button("Install Examples")){
InstallExamples();
}
EditorGUI.EndDisabledGroup();
if(examplesInstalled){
EditorGUILayout.HelpBox("Examples are installed. You can find them in the HuggingFaceAPI/Examples folder.",MessageType.Info);
}
}
privatestaticvoidInstallExamples(){
if(!Directory.Exists(sourcePath)){
Debug.LogError($"Examples not found at {sourcePath}");
return;
}
if(!Directory.Exists(destinationPath)){
Directory.CreateDirectory(destinationPath);
}
CopyFilesRecursively(newDirectoryInfo(sourcePath),newDirectoryInfo(destinationPath));
AssetDatabase.Refresh();
Debug.Log($"Examples installed to {destinationPath}");
}
privatestaticvoidCopyFilesRecursively(DirectoryInfosource,DirectoryInfotarget){
foreach(DirectoryInfodirinsource.GetDirectories())
CopyFilesRecursively(dir,target.CreateSubdirectory(dir.Name));
foreach(FileInfofileinsource.GetFiles())
file.CopyTo(Path.Combine(target.FullName,file.Name),true);
}
privatevoidOnSuccess(stringresponse){
statusMessage="<color=#5cb85c>API key is valid!</color>";
}
privatevoidOnError(stringerror){
statusMessage=$"<color=#d9534f>{error}</color>";
}
}
}