Skip to content

Commit

Permalink
com.rest.huggingface 1.0.0-preview.11 (#9)
Browse files Browse the repository at this point in the history
- added support for custom inference endpoints
- added TextToImageB64Response
- changed how we name cached items
  • Loading branch information
StephenHodgson authored Aug 22, 2023
1 parent fad50a2 commit 87d6b06
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Security.Authentication;
using UnityEngine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.IO;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ namespace HuggingFace
{
public abstract class HuggingFaceBaseEndpoint : BaseEndPoint<HuggingFaceClient, HuggingFaceAuthentication, HuggingFaceSettings>
{
private const string HttpPrefix = "https://";

protected HuggingFaceBaseEndpoint(HuggingFaceClient client) : base(client) { }

protected string GetInferenceUrl(string endpoint)
=> string.Format(client.Settings.InferenceRequestUrlFormat, $"{Root}/{endpoint}");
=> endpoint.Contains(HttpPrefix)
? endpoint
: string.Format(client.Settings.InferenceRequestUrlFormat, $"{Root}/{endpoint}");
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using HuggingFace.Hub;
using HuggingFace.Inference;
using Newtonsoft.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Linq;
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using Utilities.WebRequestRest.Interfaces;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
using Utilities.WebRequestRest;

Expand All @@ -31,13 +31,34 @@ private static async Task DecodeAudioAsync(AudioToAudioResult result, Cancellati
{
await Rest.ValidateCacheDirectoryAsync();

if (!Rest.TryGetDownloadCacheItem(result.Blob, out var localFilePath))
Rest.TryGetDownloadCacheItem(result.Blob, out var guid);
var localFilePath = Path.Combine(Rest.DownloadCacheDirectory, $"{DateTime.UtcNow:yyyy-MM-ddTHH-mm-ssffff}-{guid}.jpg");
var fileStream = new FileStream(localFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);

try
{
await using var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.ReadWrite);
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();
}

result.AudioClip = await Rest.DownloadAudioClipAsync(localFilePath, AudioType.WAV, parameters: null, cancellationToken: cancellationToken);
result.AudioClip = await Rest.DownloadAudioClipAsync($"file://{localFilePath}", AudioType.WAV, parameters: null, cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override async Task DecodeAsync(Stream stream, CancellationToken cancella
var filePath = Path.Combine(Rest.DownloadCacheDirectory, $"{DateTime.UtcNow:yyyy-MM-ddTHH-mm-ssffff}.wav");
Debug.Log(filePath);
CachedPath = filePath;
var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);

try
{
Expand All @@ -37,7 +37,7 @@ public override async Task DecodeAsync(Stream stream, CancellationToken cancella
throw;
default:
Debug.LogError(e);
break;
throw;
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace HuggingFace.Inference
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
using Utilities.WebRequestRest;

namespace HuggingFace.Inference.ComputerVision.ImageSegmentation
Expand All @@ -26,14 +27,34 @@ public override async Task DecodeAsync(CancellationToken cancellationToken = def
private static async Task DecodeImageAsync(ImageSegmentationResult result, CancellationToken cancellationToken)
{
await Rest.ValidateCacheDirectoryAsync();
Rest.TryGetDownloadCacheItem(result.Blob, out var guid);
var localFilePath = Path.Combine(Rest.DownloadCacheDirectory, $"{DateTime.UtcNow:yyyy-MM-ddTHH-mm-ssffff}-{guid}.jpg");
var fileStream = new FileStream(localFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);

if (!Rest.TryGetDownloadCacheItem(result.Blob, out var localFilePath))
try
{
await using var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.ReadWrite);
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();
}

result.Mask = await Rest.DownloadTextureAsync(localFilePath, parameters: null, cancellationToken: cancellationToken);
result.Mask = await Rest.DownloadTextureAsync($"file://{localFilePath}", parameters: null, cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override async Task DecodeAsync(Stream stream, CancellationToken cancella
var filePath = Path.Combine(Rest.DownloadCacheDirectory, $"{DateTime.UtcNow:yyyy-MM-ddTHH-mm-ssffff}.jpg");
Debug.Log(filePath);
CachedPath = filePath;
var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);

try
{
Expand All @@ -37,7 +37,7 @@ public override async Task DecodeAsync(Stream stream, CancellationToken cancella
throw;
default:
Debug.LogError(e);
break;
throw;
}
}
finally
Expand Down
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.

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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace HuggingFace.Inference.Multimodal.TextToImage
{
public sealed class TextToImageResponse : BinaryInferenceTaskResponse
public sealed class TextToImageBinaryResponse : BinaryInferenceTaskResponse
{
public string CachedPath { get; private set; }

Expand All @@ -18,10 +18,9 @@ public sealed class TextToImageResponse : BinaryInferenceTaskResponse
public override async Task DecodeAsync(Stream stream, CancellationToken cancellationToken = default)
{
await Rest.ValidateCacheDirectoryAsync();
var filePath = Path.Combine(Rest.DownloadCacheDirectory, $"{DateTime.UtcNow:yyyy-MM-ddTHH-mm-ssffff}.jpg");
Debug.Log(filePath);
CachedPath = filePath;
var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
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
{
Expand All @@ -37,7 +36,7 @@ public override async Task DecodeAsync(Stream stream, CancellationToken cancella
throw;
default:
Debug.LogError(e);
break;
throw;
}
}
finally
Expand All @@ -46,7 +45,7 @@ public override async Task DecodeAsync(Stream stream, CancellationToken cancella
await fileStream.DisposeAsync();
}

Image = await Rest.DownloadTextureAsync($"file://{filePath}", parameters: null, cancellationToken: cancellationToken);
Image = await Rest.DownloadTextureAsync($"file://{localFilePath}", parameters: null, cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public async Task Test_01_TextToImage()
Width = 1024,
};
var task = new TextToImageTask(inputs);
var response = await api.InferenceEndpoint.RunInferenceTaskAsync<TextToImageTask, TextToImageResponse>(task);
var response = await api.InferenceEndpoint.RunInferenceTaskAsync<TextToImageTask, TextToImageBinaryResponse>(task);
Assert.IsNotNull(response);
Debug.Log(response.CachedPath);
Assert.IsNotNull(response.Image);
}

Expand Down
2 changes: 1 addition & 1 deletion HuggingFace/Packages/com.rest.huggingface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "HuggingFace",
"description": "A Non-Official HuggingFace Rest Client for Unity (UPM)",
"keywords": [],
"version": "1.0.0-preview.10",
"version": "1.0.0-preview.11",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.rest.huggingface#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.rest.huggingface/releases",
Expand Down
2 changes: 1 addition & 1 deletion HuggingFace/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"com.unity.ide.rider": "3.0.24",
"com.unity.ide.visualstudio": "2.0.18",
"com.unity.ide.visualstudio": "2.0.20",
"com.unity.test-framework": "1.1.33",
"com.utilities.buildpipeline": "1.1.7"
},
Expand Down
4 changes: 2 additions & 2 deletions HuggingFace/ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2021.3.24f1
m_EditorVersionWithRevision: 2021.3.24f1 (cf10dcf7010d)
m_EditorVersion: 2022.3.7f1
m_EditorVersionWithRevision: 2022.3.7f1 (b16b3b16c7a0)
Empty file.

0 comments on commit 87d6b06

Please sign in to comment.