forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUncompressedFileResolver.cs
More file actions
65 lines (52 loc) · 1.93 KB
/
Copy pathHttpUncompressedFileResolver.cs
File metadata and controls
65 lines (52 loc) · 1.93 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
using System;
using System.Net;
namespace Tensorflow.Hub
{
public class HttpUncompressedFileResolver : HttpResolverBase
{
private readonly PathResolver _pathResolver;
public HttpUncompressedFileResolver()
{
_pathResolver = new PathResolver();
}
public override string Call(string handle)
{
handle = AppendUncompressedFormatQuery(handle);
var gsLocation = RequestGcsLocation(handle);
return _pathResolver.Call(gsLocation);
}
public override bool IsSupported(string handle)
{
if (!is_http_protocol(handle))
{
return false;
}
var load_format = resolver.model_load_format();
return load_format == Enum.GetName(typeof(resolver.ModelLoadFormat), resolver.ModelLoadFormat.UNCOMPRESSED);
}
protected virtual string AppendUncompressedFormatQuery(string handle)
{
return append_format_query(handle, ("tf-hub-format", "uncompressed"));
}
protected virtual string RequestGcsLocation(string handleWithParams)
{
var request = WebRequest.Create(handleWithParams);
var response = request.GetResponse() as HttpWebResponse;
if (response == null)
{
throw new Exception("Failed to get a response from the server.");
}
var statusCode = (int)response.StatusCode;
if (statusCode != 303)
{
throw new Exception($"Expected 303 for GCS location lookup but got HTTP {statusCode} {response.StatusDescription}");
}
var location = response.Headers["Location"];
if (!location.StartsWith("gs://"))
{
throw new Exception($"Expected Location:GS path but received {location}");
}
return location;
}
}
}