generated from RageAgainstThePixel/upm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
com.rest.huggingface 1.0.0-preview.11 (#9)
- added support for custom inference endpoints - added TextToImageB64Response - changed how we name cached items
- Loading branch information
1 parent
35a45a2
commit 7582258
Showing
19 changed files
with
180 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace HuggingFace.Inference.Multimodal | ||
{ | ||
public sealed class B64ImageResult | ||
{ | ||
[JsonConstructor] | ||
public B64ImageResult([JsonProperty("image")] string blob) | ||
{ | ||
Blob = blob; | ||
} | ||
|
||
[JsonProperty("image")] | ||
public string Blob { get; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
Runtime/Inference/Multimodal/TextToImage/TextToImageB64Response.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Newtonsoft.Json; | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
using Utilities.WebRequestRest; | ||
|
||
namespace HuggingFace.Inference.Multimodal.TextToImage | ||
{ | ||
public sealed class TextToImageB64Response : B64JsonInferenceTaskResponse | ||
{ | ||
public TextToImageB64Response(string content, JsonSerializerSettings settings) : base(content, settings) | ||
{ | ||
Result = JsonConvert.DeserializeObject<B64ImageResult>(content, settings); | ||
} | ||
|
||
public B64ImageResult Result { get; private set; } | ||
|
||
public string CachedPath { get; private set; } | ||
|
||
public Texture2D Image { get; private set; } | ||
|
||
public override async Task DecodeAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await Rest.ValidateCacheDirectoryAsync(); | ||
var localFilePath = Path.Combine(Rest.DownloadCacheDirectory, $"{DateTime.UtcNow:yyyy-MM-ddTHH-mm-ssffff}.jpg"); | ||
CachedPath = localFilePath; | ||
var fileStream = new FileStream(localFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); | ||
|
||
try | ||
{ | ||
await fileStream.WriteAsync(Convert.FromBase64String(Result.Blob), cancellationToken); | ||
await fileStream.FlushAsync(cancellationToken); | ||
} | ||
catch (Exception e) | ||
{ | ||
switch (e) | ||
{ | ||
case TaskCanceledException: | ||
case OperationCanceledException: | ||
throw; | ||
default: | ||
Debug.LogError(e); | ||
throw; | ||
} | ||
} | ||
finally | ||
{ | ||
fileStream.Close(); | ||
await fileStream.DisposeAsync(); | ||
} | ||
|
||
Image = await Rest.DownloadTextureAsync($"file://{localFilePath}", parameters: null, cancellationToken: cancellationToken); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Inference/Multimodal/TextToImage/TextToImageB64Response.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters