-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathModelUrl.cs
264 lines (220 loc) · 7.41 KB
/
ModelUrl.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
namespace AIDevGallery.Utils;
/// <summary>
/// ModelUrl class
/// </summary>
public abstract class ModelUrl
{
/// <summary>
/// Gets the FullUrl property
/// </summary>
public abstract string FullUrl { get; private protected init; }
internal string? PartialUrl { get; private protected init; }
/// <summary>
/// Gets the Repo property
/// </summary>
public abstract string Repo { get; private protected init; }
/// <summary>
/// Gets the Organization property
/// </summary>
public abstract string Organization { get; private protected init; }
/// <summary>
/// Gets the file path if exists
/// </summary>
public string? Path { get; private protected init; }
/// <summary>
/// Gets the Ref property
/// </summary>
public string Ref { get; private protected init; } = "main";
/// <summary>
/// Gets a value indicating whether the url references a file
/// </summary>
public abstract bool IsFile { get; private protected init; }
/// <summary>
/// Gets the local path with the given cache folder
/// </summary>
/// <param name="cacheRoot">The root directory of the cache</param>
/// <returns>The local path constructed from the cache root, organization, repository, and reference</returns>
public string GetLocalPath(string cacheRoot)
{
var localFolderPath = $"{cacheRoot}\\{Organization}--{Repo}\\{Ref}";
var path = Path;
if (!string.IsNullOrEmpty(path))
{
if (IsFile)
{
var pathComponents = path!.Split('/');
pathComponents = pathComponents.Take(pathComponents.Length - 1).ToArray();
localFolderPath = $"{localFolderPath}\\{string.Join("\\", pathComponents)}";
}
else
{
localFolderPath = $"{localFolderPath}\\{path!.Replace("/", "\\")}";
}
}
return localFolderPath;
}
}
/// <summary>
/// HuggingFaceUrl class
/// </summary>
public class HuggingFaceUrl : ModelUrl
{
/// <inheritdoc/>
public override string FullUrl
{
get { return $"https://huggingface.co/{PartialUrl}"; }
private protected init { }
}
/// <inheritdoc/>
public override string Repo { get; private protected init; }
/// <inheritdoc/>
public override string Organization { get; private protected init; }
/// <inheritdoc/>
public override bool IsFile { get; private protected init; }
/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceUrl"/> class.
/// </summary>
/// <param name="modelNameOrUrl">The model name or URL to initialize the HuggingFaceUrl instance.</param>
public HuggingFaceUrl(string modelNameOrUrl)
{
if (string.IsNullOrEmpty(modelNameOrUrl))
{
throw new ArgumentException("Model name or URL cannot be null or empty", nameof(modelNameOrUrl));
}
modelNameOrUrl = modelNameOrUrl.Trim();
if (modelNameOrUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
if (!modelNameOrUrl.StartsWith("https://huggingface.co", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Invalid URL", nameof(modelNameOrUrl));
}
modelNameOrUrl = modelNameOrUrl[23..];
}
string[] urlComponents = modelNameOrUrl.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
if (urlComponents.Length < 2)
{
throw new ArgumentException("Invalid URL", nameof(modelNameOrUrl));
}
Organization = urlComponents[0];
Repo = urlComponents[1];
if (urlComponents.Length < 4)
{
PartialUrl = $"{Organization}/{Repo}/tree/{Ref}";
return;
}
Ref = urlComponents[3];
if (urlComponents[2].Equals("blob", StringComparison.OrdinalIgnoreCase))
{
IsFile = true;
}
Path = string.Join("/", urlComponents.Skip(4));
PartialUrl = $"{Organization}/{Repo}/{urlComponents[2]}/{Ref}";
if (!string.IsNullOrEmpty(Path))
{
PartialUrl = $"{PartialUrl}/{Path}";
}
}
/// <summary>
/// Gets the URL root
/// </summary>
/// <returns>The root URL of the HuggingFace repository</returns>
public string GetUrlRoot()
{
return $"https://huggingface.co/{Organization}/{Repo}";
}
}
/// <summary>
/// Represents a URL for a GitHub repository.
/// </summary>
public class GitHubUrl : ModelUrl
{
/// <summary>
/// Initializes a new instance of the <see cref="GitHubUrl"/> class.
/// </summary>
/// <param name="url">The GitHub repository URL.</param>
public GitHubUrl(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException("url cannot be null or empty", nameof(url));
}
url = url.Trim();
FullUrl = url;
if (!url.StartsWith("https://github.com/", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Invalid URL", nameof(url));
}
url = url[19..];
string[] urlComponents = url.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
if (urlComponents.Length < 2)
{
throw new ArgumentException("Invalid URL", nameof(url));
}
Organization = urlComponents[0];
Repo = urlComponents[1];
if (urlComponents.Length < 4)
{
return;
}
Ref = urlComponents[3];
if (urlComponents[2].Equals("blob", StringComparison.OrdinalIgnoreCase))
{
IsFile = true;
}
Path = string.Join("/", urlComponents.Skip(4));
}
/// <summary>
/// Gets the full URL of the GitHub repository.
/// </summary>
public override string FullUrl { get; private protected init; }
/// <summary>
/// Gets the repository name.
/// </summary>
public override string Repo { get; private protected init; }
/// <summary>
/// Gets the organization name.
/// </summary>
public override string Organization { get; private protected init; }
/// <summary>
/// Gets a value indicating whether the URL references a file.
/// </summary>
public override bool IsFile { get; private protected init; }
/// <summary>
/// Gets the URL root of the GitHub repository.
/// </summary>
/// <returns>The root URL of the GitHub repository.</returns>
public string GetUrlRoot()
{
return $"https://github.com/{Organization}/{Repo}";
}
}
/// <summary>
/// Provides helper methods for URL manipulation.
/// </summary>
public static class UrlHelpers
{
/// <summary>
/// Gets the full URL for a given partial URL.
/// </summary>
/// <param name="url">The partial URL to be converted to a full URL.</param>
/// <returns>The full URL as a string.</returns>
public static string GetFullUrl(string url)
{
if (url.StartsWith("https://github.com", StringComparison.OrdinalIgnoreCase))
{
return url;
}
else if (url.StartsWith("local", StringComparison.OrdinalIgnoreCase))
{
return url;
}
else
{
return new HuggingFaceUrl(url).FullUrl;
}
}
}