Skip to content

Commit

Permalink
com.rest.huggingface 1.0.0-preview.7 (#5)
Browse files Browse the repository at this point in the history
- added image segmentation task
  • Loading branch information
StephenHodgson authored Jun 18, 2023
1 parent 3defe14 commit 5161f27
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ I am not affiliated with HuggingFace and an account with api access is required.

Requires Unity 2021.3 LTS or higher.

The recommended installation method is though the unity package manager and [OpenUPM](https://openupm.com/packages/com.openai.unity).
The recommended installation method is though the unity package manager and [OpenUPM](https://openupm.com/packages/com.rest.huggingface).

### Via Unity Package Manager and OpenUPM

Expand Down
12 changes: 7 additions & 5 deletions Runtime/Inference/Audio/AudioToAudio/AudioToAudioResponse.cs
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.Collections.Generic;
using System.IO;
Expand All @@ -23,19 +25,19 @@ public AudioToAudioResponse(string content, JsonSerializerSettings settings)

/// <inheritdoc />
public override async Task DecodeAsync(CancellationToken cancellationToken = default)
=> await Task.WhenAll(Results.Select(audioInfo => DecodeAudioAsync(audioInfo, cancellationToken)).ToList());
=> await Task.WhenAll(Results.Select(result => DecodeAudioAsync(result, cancellationToken)).ToList());

private static async Task DecodeAudioAsync(AudioToAudioResult audioToAudioResult, CancellationToken cancellationToken)
private static async Task DecodeAudioAsync(AudioToAudioResult result, CancellationToken cancellationToken)
{
await Rest.ValidateCacheDirectoryAsync();

if (!Rest.TryGetDownloadCacheItem(audioToAudioResult.Blob, out var localFilePath))
if (!Rest.TryGetDownloadCacheItem(result.Blob, out var localFilePath))
{
await using var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.ReadWrite);
await fileStream.WriteAsync(Convert.FromBase64String(audioToAudioResult.Blob), cancellationToken);
await fileStream.WriteAsync(Convert.FromBase64String(result.Blob), cancellationToken);
}

audioToAudioResult.AudioClip = await Rest.DownloadAudioClipAsync(localFilePath, AudioType.WAV, parameters: null, cancellationToken: cancellationToken);
result.AudioClip = await Rest.DownloadAudioClipAsync(localFilePath, AudioType.WAV, parameters: null, cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

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

namespace HuggingFace.Inference.ComputerVision.ImageSegmentation
{
public sealed class ImageSegmentationResponse : B64JsonInferenceTaskResponse
{
public ImageSegmentationResponse(string content, JsonSerializerSettings settings) : base(content, settings)
{
Results = JsonConvert.DeserializeObject<IReadOnlyList<ImageSegmentationResult>>(content, settings);
}

public IReadOnlyList<ImageSegmentationResult> Results { get; }

public override async Task DecodeAsync(CancellationToken cancellationToken = default)
=> await Task.WhenAll(Results.Select(result => DecodeImageAsync(result, cancellationToken)).ToList());

private static async Task DecodeImageAsync(ImageSegmentationResult result, CancellationToken cancellationToken)
{
await Rest.ValidateCacheDirectoryAsync();

if (!Rest.TryGetDownloadCacheItem(result.Blob, out var localFilePath))
{
await using var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.ReadWrite);
await fileStream.WriteAsync(Convert.FromBase64String(result.Blob), cancellationToken);
}

result.Mask = await Rest.DownloadTextureAsync(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
@@ -0,0 +1,33 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine;

namespace HuggingFace.Inference.ComputerVision.ImageSegmentation
{
public sealed class ImageSegmentationResult
{
[JsonConstructor]
public ImageSegmentationResult(
[JsonProperty("score")] double score,
[JsonProperty("label")] string label,
[JsonProperty("mask")] string blob)
{
Score = score;
Label = label;
Blob = blob;
}

[JsonProperty("score")]
public double Score { get; }

[JsonProperty("label")]
public string Label { get; }

[JsonProperty("mask")]
public string Blob { get; }

[JsonIgnore]
public Texture2D Mask { get; internal set; }
}
}

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
@@ -1,11 +1,13 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using HuggingFace.Hub;

namespace HuggingFace.Inference.ComputerVision.ImageSegmentation
{
public sealed class ImageSegmentationTask : InferenceTask
public sealed class ImageSegmentationTask : BaseImageInferenceTask
{
public ImageSegmentationTask(ModelInfo model, InferenceOptions options)
: base(model, options)
public ImageSegmentationTask(SingleSourceImageInput input, ModelInfo model = null, InferenceOptions options = null)
: base(input, model, options)
{
}

Expand Down
12 changes: 11 additions & 1 deletion Runtime/Inference/InferenceEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@ async Task<Response> CallEndpointAsync()
throw;
}

var error = JsonConvert.DeserializeObject<HuggingFaceError>(restEx.Response.Error);
HuggingFaceError error;

try
{
error = JsonConvert.DeserializeObject<HuggingFaceError>(restEx.Response.Error);
}
catch (Exception)
{
throw restEx;
}

await new WaitForSeconds((float)error.EstimatedTime);
response = await CallEndpointAsync();
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/TestFixture_05_Inference_ComputerVision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using HuggingFace.Inference.ComputerVision;
using HuggingFace.Inference.ComputerVision.ImageClassification;
using HuggingFace.Inference.ComputerVision.ImageSegmentation;
using HuggingFace.Inference.ComputerVision.ObjectDetection;
using NUnit.Framework;
using System.Threading.Tasks;
Expand Down Expand Up @@ -47,5 +48,24 @@ public async Task Test_02_ObjectDetection()
Debug.Log($"{result.Label}: {result.Score}");
}
}

[Test]
public async Task Test_03_ImageSegmentation()
{
var api = new HuggingFaceClient();
Assert.IsNotNull(api.InferenceEndpoint);
var imagePath = AssetDatabase.GUIDToAssetPath("32291e451b73587408924bd1f44e60ed");
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(imagePath);
using var input = new SingleSourceImageInput(texture);
var task = new ImageSegmentationTask(input);
var response = await api.InferenceEndpoint.RunInferenceTaskAsync<ImageSegmentationTask, ImageSegmentationResponse>(task);
Assert.IsNotNull(response);

foreach (var result in response.Results)
{
Debug.Log($"{result.Label}: {result.Score}");
Assert.IsNotNull(result.Mask);
}
}
}
}
2 changes: 1 addition & 1 deletion 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.6",
"version": "1.0.0-preview.7",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.rest.huggingface#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.rest.huggingface/releases",
Expand Down

0 comments on commit 5161f27

Please sign in to comment.