Skip to content

Commit 26d4283

Browse files
committed
Change line feed code to LF and add dependencies to package.json
1 parent 80066ad commit 26d4283

File tree

13 files changed

+166
-183
lines changed

13 files changed

+166
-183
lines changed
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "Mochineko.WhisperAPI.Samples",
3-
"rootNamespace": "",
4-
"references": [
5-
"GUID:12e7ae99596e14eef8970a67eb588899",
6-
"GUID:2620e54ab6edb48f48d55966fd5662fb",
7-
"GUID:51d11816546934d4a937e4a420478ce5",
8-
"GUID:f51ebe6a0ceec4240a699833d6309b23",
9-
"GUID:fd228c28e14f1a34cbe508676c914dd7",
10-
"GUID:2665a8d13d1b3f18800f46e256720795",
11-
"GUID:e0cd26848372d4e5c891c569017e11f1"
12-
],
13-
"includePlatforms": [],
14-
"excludePlatforms": [],
15-
"allowUnsafeCode": false,
16-
"overrideReferences": true,
17-
"precompiledReferences": [],
18-
"autoReferenced": false,
19-
"defineConstraints": [],
20-
"versionDefines": [],
21-
"noEngineReferences": false
2+
"name": "Mochineko.WhisperAPI.Samples",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:12e7ae99596e14eef8970a67eb588899",
6+
"GUID:2620e54ab6edb48f48d55966fd5662fb",
7+
"GUID:51d11816546934d4a937e4a420478ce5",
8+
"GUID:f51ebe6a0ceec4240a699833d6309b23",
9+
"GUID:fd228c28e14f1a34cbe508676c914dd7",
10+
"GUID:2665a8d13d1b3f18800f46e256720795",
11+
"GUID:e0cd26848372d4e5c891c569017e11f1"
12+
],
13+
"includePlatforms": [],
14+
"excludePlatforms": [],
15+
"allowUnsafeCode": false,
16+
"overrideReferences": true,
17+
"precompiledReferences": [],
18+
"autoReferenced": false,
19+
"defineConstraints": [],
20+
"versionDefines": [],
21+
"noEngineReferences": false
2222
}

Assets/Mochineko/WhisperAPI.Samples/PolicyFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ internal static class PolicyFactory
1919
public static IPolicy<string> Build()
2020
{
2121
var totalTimeout = TimeoutFactory.Timeout<string>(
22-
timeout: TimeSpan.FromSeconds(TotalTimeoutSeconds));
22+
TimeSpan.FromSeconds(TotalTimeoutSeconds));
2323

2424
var retry = RetryFactory.RetryWithExponentialBackoff<string>(
2525
MaxRetryCount,
26-
factor: ExponentialBackoffFactor,
27-
baseNumber: ExponentialBackoffBaseNumber);
26+
ExponentialBackoffFactor,
27+
ExponentialBackoffBaseNumber);
2828

2929
var bulkheadPolicy = BulkheadFactory.Bulkhead<string>(
3030
MaxParallelization);

Assets/Mochineko/WhisperAPI.Samples/TranscriptionSample.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,18 @@ namespace Mochineko.WhisperAPI.Samples
1717
/// </summary>
1818
public sealed class TranscriptionSample : MonoBehaviour
1919
{
20+
private static readonly HttpClient httpClient = new();
21+
2022
/// <summary>
2123
/// File path of speech audio.
2224
/// </summary>
2325
[SerializeField] private string filePath = string.Empty;
2426

25-
private static readonly HttpClient httpClient = new();
27+
private readonly IPolicy<string> policy = PolicyFactory.Build();
2628

2729
private readonly TranscriptionRequestParameters requestParameters = new(
28-
file: string.Empty,
29-
Model.Whisper1,
30-
prompt: null,
31-
responseFormat: null,
32-
temperature: null,
33-
language: null);
34-
35-
private readonly IPolicy<string> policy = PolicyFactory.Build();
30+
string.Empty,
31+
Model.Whisper1);
3632

3733
[ContextMenu(nameof(Transcribe))]
3834
public void Transcribe()
@@ -44,10 +40,7 @@ public void Transcribe()
4440
private async UniTask TranscribeAsync(CancellationToken cancellationToken)
4541
{
4642
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
47-
if (string.IsNullOrEmpty(apiKey))
48-
{
49-
throw new NullReferenceException(nameof(apiKey));
50-
}
43+
if (string.IsNullOrEmpty(apiKey)) throw new NullReferenceException(nameof(apiKey));
5144

5245
var absoluteFilePath = Path.Combine(
5346
Application.dataPath,
@@ -56,7 +49,7 @@ private async UniTask TranscribeAsync(CancellationToken cancellationToken)
5649

5750
requestParameters.File = filePath;
5851

59-
Log.Debug("[Whisper_API.Samples] Begin to transcribe.");
52+
Log.Debug("[WhisperAPI.Samples] Begin to transcribe.");
6053

6154
// Transcribe speech into text by Whisper transcription API.
6255
var result = await policy
@@ -68,7 +61,7 @@ private async UniTask TranscribeAsync(CancellationToken cancellationToken)
6861
absoluteFilePath,
6962
requestParameters,
7063
innerCancellationToken,
71-
debug: true),
64+
true),
7265
cancellationToken);
7366

7467
switch (result)
@@ -78,19 +71,19 @@ private async UniTask TranscribeAsync(CancellationToken cancellationToken)
7871
{
7972
// Default text response format is JSON.
8073
var text = TranscriptionResponseBody.FromJson(success.Result)?.Text;
81-
Log.Debug("[Whisper_API.Samples] Succeeded to transcribe into: {0}.", text);
74+
Log.Debug("[WhisperAPI.Samples] Succeeded to transcribe into: {0}.", text);
8275
break;
8376
}
8477
// Retryable failure
8578
case IUncertainRetryableResult<string> retryable:
8679
{
87-
Log.Error("[Whisper_API.Samples] Failed to transcribe because -> {0}.", retryable.Message);
80+
Log.Error("[WhisperAPI.Samples] Retryable failed to transcribe because -> {0}.", retryable.Message);
8881
break;
8982
}
9083
// Failure
9184
case IUncertainFailureResult<string> failure:
9285
{
93-
Log.Error("[Whisper_API.Samples] Failed to transcribe because -> {0}.", failure.Message);
86+
Log.Error("[WhisperAPI.Samples] Failed to transcribe because -> {0}.", failure.Message);
9487
break;
9588
}
9689
default:

Assets/Mochineko/WhisperAPI.Samples/TranslationSample.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#nullable enable
22
using System;
3+
using System.IO;
34
using System.Net.Http;
45
using System.Threading;
56
using Assets.Mochineko.WhisperAPI;
67
using Cysharp.Threading.Tasks;
78
using Mochineko.Relent.Resilience;
89
using Mochineko.Relent.UncertainResult;
10+
using Unity.Logging;
911
using UnityEngine;
1012

1113
namespace Mochineko.WhisperAPI.Samples
@@ -15,20 +17,19 @@ namespace Mochineko.WhisperAPI.Samples
1517
/// </summary>
1618
public sealed class TranslationSample : MonoBehaviour
1719
{
20+
private static readonly HttpClient httpClient = new();
21+
1822
/// <summary>
1923
/// File path of speech audio.
2024
/// </summary>
21-
[SerializeField]
22-
private string filePath = string.Empty;
23-
24-
private static readonly HttpClient httpClient = new();
25+
[SerializeField] private string filePath = string.Empty;
26+
27+
private readonly IPolicy<string> policy = PolicyFactory.Build();
2528

2629
private readonly TranslationRequestParameters requestParameters = new(
27-
file: string.Empty,
30+
string.Empty,
2831
Model.Whisper1);
2932

30-
private readonly IPolicy<string> policy = PolicyFactory.Build();
31-
3233
[ContextMenu(nameof(Translate))]
3334
public void Translate()
3435
{
@@ -39,14 +40,16 @@ public void Translate()
3940
private async UniTask TranslateAsync(CancellationToken cancellationToken)
4041
{
4142
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
42-
if (string.IsNullOrEmpty(apiKey))
43-
{
44-
throw new NullReferenceException(nameof(apiKey));
45-
}
46-
43+
if (string.IsNullOrEmpty(apiKey)) throw new NullReferenceException(nameof(apiKey));
44+
45+
var absoluteFilePath = Path.Combine(
46+
Application.dataPath,
47+
"..",
48+
filePath);
49+
4750
requestParameters.File = filePath;
4851

49-
Debug.Log($"[Whisper_API.Samples] Begin to translate.");
52+
Log.Debug("[WhisperAPI.Samples] Begin to translate.");
5053

5154
// Translate speech into English text by Whisper transcription API.
5255
var result = await policy
@@ -55,10 +58,10 @@ private async UniTask TranslateAsync(CancellationToken cancellationToken)
5558
.TranslateFileAsync(
5659
apiKey,
5760
httpClient,
58-
filePath,
61+
absoluteFilePath,
5962
requestParameters,
6063
innerCancellationToken,
61-
debug: true),
64+
true),
6265
cancellationToken);
6366

6467
switch (result)
@@ -68,20 +71,19 @@ private async UniTask TranslateAsync(CancellationToken cancellationToken)
6871
{
6972
// Default text response format is JSON.
7073
var text = TranslationResponseBody.FromJson(success.Result)?.Text;
71-
// Log text result.
72-
Debug.Log($"[Whisper_API.Samples] Succeeded to translate into: {text}.");
74+
Log.Debug("[WhisperAPI.Samples] Succeeded to translate into: {0}.", text);
7375
break;
7476
}
7577
// Retryable failure
7678
case IUncertainRetryableResult<string> retryable:
7779
{
78-
Debug.LogError($"[Whisper_API.Samples] Failed to translate because -> {retryable.Message}.");
80+
Log.Error("[WhisperAPI.Samples] Retryable failed to translate because -> {0}.", retryable.Message);
7981
break;
8082
}
8183
// Failure
8284
case IUncertainFailureResult<string> failure:
8385
{
84-
Debug.LogError($"[Whisper_API.Samples] Failed to translate because -> {failure.Message}.");
86+
Log.Error("[WhisperAPI.Samples] Failed to translate because -> {0}.", failure.Message);
8587
break;
8688
}
8789
default:
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
2-
"name": "Mochineko.WhisperAPI.Tests",
3-
"rootNamespace": "",
4-
"references": [
5-
"GUID:27619889b8ba8c24980f49ee34dbb44a",
6-
"GUID:0acc523941302664db1f4e527237feb3",
7-
"GUID:e372c541aba5148868e12aa078ca7c20",
8-
"GUID:12e7ae99596e14eef8970a67eb588899",
9-
"GUID:f51ebe6a0ceec4240a699833d6309b23",
10-
"GUID:2620e54ab6edb48f48d55966fd5662fb",
11-
"GUID:fd228c28e14f1a34cbe508676c914dd7",
12-
"GUID:2665a8d13d1b3f18800f46e256720795",
13-
"GUID:e0cd26848372d4e5c891c569017e11f1"
14-
],
15-
"includePlatforms": [
16-
"Editor"
17-
],
18-
"excludePlatforms": [],
19-
"allowUnsafeCode": false,
20-
"overrideReferences": true,
21-
"precompiledReferences": [
22-
"nunit.framework.dll"
23-
],
24-
"autoReferenced": false,
25-
"defineConstraints": [
26-
"UNITY_INCLUDE_TESTS"
27-
],
28-
"versionDefines": [],
29-
"noEngineReferences": false
2+
"name": "Mochineko.WhisperAPI.Tests",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:27619889b8ba8c24980f49ee34dbb44a",
6+
"GUID:0acc523941302664db1f4e527237feb3",
7+
"GUID:e372c541aba5148868e12aa078ca7c20",
8+
"GUID:12e7ae99596e14eef8970a67eb588899",
9+
"GUID:f51ebe6a0ceec4240a699833d6309b23",
10+
"GUID:2620e54ab6edb48f48d55966fd5662fb",
11+
"GUID:fd228c28e14f1a34cbe508676c914dd7",
12+
"GUID:2665a8d13d1b3f18800f46e256720795",
13+
"GUID:e0cd26848372d4e5c891c569017e11f1"
14+
],
15+
"includePlatforms": [
16+
"Editor"
17+
],
18+
"excludePlatforms": [],
19+
"allowUnsafeCode": false,
20+
"overrideReferences": true,
21+
"precompiledReferences": [
22+
"nunit.framework.dll"
23+
],
24+
"autoReferenced": false,
25+
"defineConstraints": [
26+
"UNITY_INCLUDE_TESTS"
27+
],
28+
"versionDefines": [],
29+
"noEngineReferences": false
3030
}

Assets/Mochineko/WhisperAPI.Tests/TranscriptionTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable enable
22
using System;
33
using System.IO;
4+
using System.Net.Http;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Assets.Mochineko.WhisperAPI;
@@ -29,15 +30,15 @@ public async Task Transcribe()
2930
Application.dataPath,
3031
"Mochineko/WhisperAPI.Tests/test.wav");
3132

32-
using var httpClient = new System.Net.Http.HttpClient();
33+
using var httpClient = new HttpClient();
3334

3435
var apiResult = await TranscriptionAPI
3536
.TranscribeFileAsync(
3637
apiKey,
3738
httpClient,
3839
filePath,
3940
new TranscriptionRequestParameters(
40-
file: filePath,
41+
filePath,
4142
Model.Whisper1,
4243
temperature: 0f),
4344
CancellationToken.None,

Assets/Mochineko/WhisperAPI.Tests/TranslationTest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable enable
22
using System;
33
using System.IO;
4+
using System.Net.Http;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Assets.Mochineko.WhisperAPI;
@@ -20,28 +21,25 @@ internal sealed class TranslationTest
2021
public async Task Translate()
2122
{
2223
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
23-
if (string.IsNullOrEmpty(apiKey))
24-
{
25-
throw new NullReferenceException(nameof(apiKey));
26-
}
24+
if (string.IsNullOrEmpty(apiKey)) throw new NullReferenceException(nameof(apiKey));
2725

2826
var filePath = Path.Combine(
2927
Application.dataPath,
3028
"Mochineko/WhisperAPI.Tests/test.wav");
3129

32-
using var httpClient = new System.Net.Http.HttpClient();
30+
using var httpClient = new HttpClient();
3331

3432
var apiResult = await TranslationAPI
3533
.TranslateFileAsync(
3634
apiKey,
3735
httpClient,
3836
filePath,
3937
new TranslationRequestParameters(
40-
file: filePath,
38+
filePath,
4139
Model.Whisper1,
4240
temperature: 0f),
4341
CancellationToken.None,
44-
debug: true);
42+
true);
4543

4644
var result = TranscriptionResponseBody.FromJson(apiResult.Unwrap())?.Text;
4745
result?.Should().Be("Please clean up the store. Please clean up the store.");

0 commit comments

Comments
 (0)