Skip to content

Commit 5d692cf

Browse files
committed
Renewal translation API implementation
1 parent a5105e5 commit 5d692cf

File tree

11 files changed

+296
-187
lines changed

11 files changed

+296
-187
lines changed

Assets/Mochineko/Whisper_API.Samples/TranscriptionSample.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ private async UniTask TranscribeAsync(CancellationToken cancellationToken)
4545
requestParameters.File = filePath;
4646

4747
await UniTask.SwitchToThreadPool();
48+
49+
Debug.Log($"[Whisper_API.Samples] Begin to transcribe.");
4850

4951
// Transcribe speech into text by Whisper transcription API.
5052
var result = await policy
@@ -68,19 +70,19 @@ private async UniTask TranscribeAsync(CancellationToken cancellationToken)
6870
// Default text response format is JSON.
6971
var text = TranscriptionResponseBody.FromJson(success.Result)?.Text;
7072
// Log text result.
71-
Debug.Log($"[Whisper_API.Transcription.Samples] Succeeded to transcribe into: {text}.");
73+
Debug.Log($"[Whisper_API.Samples] Succeeded to transcribe into: {text}.");
7274
break;
7375
}
7476
// Retryable failure
7577
case IUncertainRetryableResult<string> retryable:
7678
{
77-
Debug.LogError($"[Whisper_API.Transcription.Samples] Failed to transcribe because -> {retryable.Message}.");
79+
Debug.LogError($"[Whisper_API.Samples] Failed to transcribe because -> {retryable.Message}.");
7880
break;
7981
}
8082
// Failure
8183
case IUncertainFailureResult<string> failure:
8284
{
83-
Debug.LogError($"[Whisper_API.Transcription.Samples] Failed to transcribe because -> {failure.Message}.");
85+
Debug.LogError($"[Whisper_API.Samples] Failed to transcribe because -> {failure.Message}.");
8486
break;
8587
}
8688
default:
Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#nullable enable
2-
using System;
2+
using System.Net.Http;
3+
using System.Threading;
34
using Cysharp.Threading.Tasks;
5+
using Mochineko.Relent.Resilience;
6+
using Mochineko.Relent.UncertainResult;
47
using UnityEngine;
5-
using UnityEngine.Assertions;
68

79
namespace Mochineko.Whisper_API.Samples
810
{
@@ -14,67 +16,78 @@ public sealed class TranslationSample : MonoBehaviour
1416
/// <summary>
1517
/// API key generated by OpenAPI.
1618
/// </summary>
17-
[SerializeField] private string apiKey = string.Empty;
19+
[SerializeField]
20+
private string apiKey = string.Empty;
1821

1922
/// <summary>
2023
/// File path of speech audio.
2124
/// </summary>
22-
[SerializeField] private string filePath = string.Empty;
25+
[SerializeField]
26+
private string filePath = string.Empty;
27+
28+
private static readonly HttpClient httpClient = new();
2329

24-
private TranslationAPI? connection;
30+
private readonly TranslationRequestParameters requestParameters = new(
31+
file: string.Empty,
32+
Model.Whisper1);
2533

26-
private void Start()
27-
{
28-
// API Key must be set.
29-
Assert.IsNotNull(apiKey);
34+
private readonly IPolicy<string> policy = PolicyFactory.Build();
3035

31-
// Create instance of WhisperTranscriptionConnection.
32-
connection = new TranslationAPI(apiKey);
33-
34-
// If you want to specify response format, etc..., please use other initialization:
35-
// connection = new WhisperTranslationConnection(apiKey, new APIRequestBody(
36-
// file: "",
37-
// model: "whisper-1",
38-
// prompt: "Some prompts",
39-
// responseFormat: "json",
40-
// temperature: 1f));
36+
[ContextMenu(nameof(Translate))]
37+
public void Translate()
38+
{
39+
TranslateAsync(this.GetCancellationTokenOnDestroy())
40+
.Forget();
4141
}
4242

43-
[ContextMenu(nameof(Translate))]
44-
public async void Translate()
43+
private async UniTask TranslateAsync(CancellationToken cancellationToken)
4544
{
46-
// Validations
47-
if (connection == null)
48-
{
49-
Debug.LogError($"[Whisper_API.Translation.Samples] Connection is null.");
50-
return;
51-
}
45+
requestParameters.File = filePath;
5246

53-
if (string.IsNullOrEmpty(filePath))
54-
{
55-
Debug.LogError($"[Whisper_API.Translation.Samples] File path is null or empty.");
56-
return;
57-
}
47+
await UniTask.SwitchToThreadPool();
48+
49+
Debug.Log($"[Whisper_API.Samples] Begin to translate.");
5850

59-
string result;
60-
try
61-
{
62-
// Translate speech into English text by Whisper translation API.
63-
result = await connection
64-
.TranslateFromFileAsync(filePath, this.GetCancellationTokenOnDestroy());
65-
}
66-
catch (Exception e)
51+
// Translate speech into English text by Whisper transcription API.
52+
var result = await policy
53+
.ExecuteAsync(async innerCancellationToken
54+
=> await TranslationAPI
55+
.TranslateFileAsync(
56+
apiKey,
57+
httpClient,
58+
filePath,
59+
requestParameters,
60+
innerCancellationToken),
61+
cancellationToken);
62+
63+
await UniTask.SwitchToMainThread(cancellationToken);
64+
65+
switch (result)
6766
{
68-
// Exceptions should be caught.
69-
Debug.LogException(e);
70-
return;
67+
// Success
68+
case IUncertainSuccessResult<string> success:
69+
{
70+
// Default text response format is JSON.
71+
var text = TranslationResponseBody.FromJson(success.Result)?.Text;
72+
// Log text result.
73+
Debug.Log($"[Whisper_API.Samples] Succeeded to translate into: {text}.");
74+
break;
75+
}
76+
// Retryable failure
77+
case IUncertainRetryableResult<string> retryable:
78+
{
79+
Debug.LogError($"[Whisper_API.Samples] Failed to translate because -> {retryable.Message}.");
80+
break;
81+
}
82+
// Failure
83+
case IUncertainFailureResult<string> failure:
84+
{
85+
Debug.LogError($"[Whisper_API.Samples] Failed to translate because -> {failure.Message}.");
86+
break;
87+
}
88+
default:
89+
throw new UncertainResultPatternMatchException(nameof(result));
7190
}
72-
73-
// Default text response format is JSON.
74-
var text = TranslationResponseBody.FromJson(result)?.Text;
75-
76-
// Log text result.
77-
Debug.Log($"[Whisper_API.Translation.Samples] Result:\n{text}");
7891
}
7992
}
8093
}

Assets/Mochineko/Whisper_API.Tests/TranscriptionTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public async Task Transcribe()
3737
filePath,
3838
new TranscriptionRequestParameters(
3939
file: filePath,
40-
Model.Whisper1),
40+
Model.Whisper1,
41+
temperature: 0f),
4142
CancellationToken.None);
4243
switch (apiResult)
4344
{

Assets/Mochineko/Whisper_API.Tests/TranslationTest.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using FluentAssertions;
6+
using Mochineko.Relent.UncertainResult;
67
using NUnit.Framework;
78
using UnityEngine;
89
using UnityEngine.TestTools;
@@ -27,15 +28,37 @@ public async Task Translate()
2728
Application.dataPath,
2829
"Mochineko/Whisper_API.Tests/test.wav");
2930

30-
var connection = new TranslationAPI(apiKey, Model.Whisper1);
31+
using var httpClient = new System.Net.Http.HttpClient();
3132

32-
var result = await connection.TranslateFromFileAsync(filePath, CancellationToken.None);
33-
34-
string.IsNullOrEmpty(result).Should().BeFalse();
35-
36-
var json = TranslationResponseBody.FromJson(result);
37-
38-
Debug.Log($"Result:\n{json?.Text}");
33+
var apiResult = await TranslationAPI
34+
.TranslateFileAsync(
35+
apiKey,
36+
httpClient,
37+
filePath,
38+
new TranslationRequestParameters(
39+
file: filePath,
40+
Model.Whisper1,
41+
temperature: 0f),
42+
CancellationToken.None);
43+
switch (apiResult)
44+
{
45+
case IUncertainSuccessResult<string> success:
46+
var result = TranscriptionResponseBody.FromJson(success.Result)?.Text;
47+
Debug.Log($"[Whisper_API.Tests] Result: {result}.");
48+
result?.Should().Be("Please clean up the store. Please clean up the store.");
49+
break;
50+
51+
case IUncertainRetryableResult<string> retryable:
52+
Debug.LogError($"Retryable error -> {retryable.Message}");
53+
break;
54+
55+
case IUncertainFailureResult<string> failure:
56+
Debug.LogError($"Failure error -> {failure.Message}");
57+
break;
58+
59+
default:
60+
throw new UncertainResultPatternMatchException(nameof(apiResult));
61+
}
3962
}
4063
}
4164
}

Assets/Mochineko/Whisper_API/RateLimitExceededResult.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
namespace Mochineko.Whisper_API
55
{
6+
/// <summary>
7+
/// A result that indicates that the rate limit of API has been exceeded.
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
610
public sealed class RateLimitExceededResult<T>
7-
: IUncertainRetryableResult<T>
11+
: IUncertainFailureResult<T>
812
{
913
public bool Success => false;
1014
public bool Retryable => false;

Assets/Mochineko/Whisper_API/TranscriptionAPI.cs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,59 +56,13 @@ public static async UniTask<IUncertainResult<string>> TranscribeAsync(
5656

5757
// Create request
5858
var requestMessage = new HttpRequestMessage(HttpMethod.Post, EndPoint);
59+
5960
requestMessage
6061
.Headers
6162
.Add("Authorization", $"Bearer {apiKey}");
6263

6364
var requestContent = new MultipartFormDataContent();
64-
65-
requestContent.Add(
66-
content: new StringContent(
67-
content: parameters.Model,
68-
encoding: System.Text.Encoding.UTF8),
69-
name: "model");
70-
71-
requestContent.Add(
72-
content: new StreamContent(content: fileStream),
73-
name: "file",
74-
fileName: parameters.File);
75-
76-
if (parameters.Prompt != null)
77-
{
78-
requestContent.Add(
79-
content: new StringContent(
80-
content: parameters.Prompt,
81-
encoding: System.Text.Encoding.UTF8),
82-
name: "prompt");
83-
}
84-
85-
if (parameters.ResponseFormat != null)
86-
{
87-
requestContent.Add(
88-
content: new StringContent(
89-
content: parameters.ResponseFormat,
90-
encoding: System.Text.Encoding.UTF8),
91-
name: "response_format");
92-
}
93-
94-
if (parameters.Temperature != null)
95-
{
96-
requestContent.Add(
97-
content: new StringContent(
98-
content: parameters.Temperature.ToString(),
99-
encoding: System.Text.Encoding.UTF8),
100-
name: "temperature");
101-
}
102-
103-
if (parameters.Language != null)
104-
{
105-
requestContent.Add(
106-
content: new StringContent(
107-
content: parameters.Language,
108-
encoding: System.Text.Encoding.UTF8),
109-
name: "language");
110-
}
111-
65+
parameters.SetParameters(requestContent, fileStream);
11266
requestMessage.Content = requestContent;
11367

11468
// Send request

0 commit comments

Comments
 (0)