Skip to content

Commit bd0eb4f

Browse files
committed
WIP Improve logging
1 parent 8e76533 commit bd0eb4f

File tree

10 files changed

+282
-149
lines changed

10 files changed

+282
-149
lines changed

Assets/Mochineko/WhisperAPI.Samples/Mochineko.WhisperAPI.Samples.asmdef

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"GUID:12e7ae99596e14eef8970a67eb588899",
66
"GUID:2620e54ab6edb48f48d55966fd5662fb",
77
"GUID:51d11816546934d4a937e4a420478ce5",
8-
"GUID:f51ebe6a0ceec4240a699833d6309b23"
8+
"GUID:f51ebe6a0ceec4240a699833d6309b23",
9+
"GUID:fd228c28e14f1a34cbe508676c914dd7",
10+
"GUID:2665a8d13d1b3f18800f46e256720795",
11+
"GUID:e0cd26848372d4e5c891c569017e11f1"
912
],
1013
"includePlatforms": [],
1114
"excludePlatforms": [],

Assets/Mochineko/WhisperAPI.Samples/TranscriptionSample.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#nullable enable
2+
using System;
23
using System.Net.Http;
34
using System.Threading;
45
using Assets.Mochineko.WhisperAPI;
56
using Cysharp.Threading.Tasks;
67
using Mochineko.Relent.Resilience;
78
using Mochineko.Relent.UncertainResult;
9+
using Unity.Logging;
810
using UnityEngine;
911

1012
namespace Mochineko.WhisperAPI.Samples
@@ -14,12 +16,6 @@ namespace Mochineko.WhisperAPI.Samples
1416
/// </summary>
1517
public sealed class TranscriptionSample : MonoBehaviour
1618
{
17-
/// <summary>
18-
/// API key generated by OpenAPI.
19-
/// </summary>
20-
[SerializeField]
21-
private string apiKey = string.Empty;
22-
2319
/// <summary>
2420
/// File path of speech audio.
2521
/// </summary>
@@ -30,7 +26,11 @@ public sealed class TranscriptionSample : MonoBehaviour
3026

3127
private readonly TranscriptionRequestParameters requestParameters = new(
3228
file: string.Empty,
33-
Model.Whisper1);
29+
Model.Whisper1,
30+
prompt: null,
31+
responseFormat: null,
32+
temperature: null,
33+
language: null);
3434

3535
private readonly IPolicy<string> policy = PolicyFactory.Build();
3636

@@ -43,11 +43,11 @@ public void Transcribe()
4343

4444
private async UniTask TranscribeAsync(CancellationToken cancellationToken)
4545
{
46+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
47+
4648
requestParameters.File = filePath;
4749

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

5252
// Transcribe speech into text by Whisper transcription API.
5353
var result = await policy
@@ -58,32 +58,30 @@ private async UniTask TranscribeAsync(CancellationToken cancellationToken)
5858
httpClient,
5959
filePath,
6060
requestParameters,
61-
innerCancellationToken),
61+
innerCancellationToken,
62+
debug: true),
6263
cancellationToken);
6364

64-
await UniTask.SwitchToMainThread(cancellationToken);
65-
6665
switch (result)
6766
{
6867
// Success
6968
case IUncertainSuccessResult<string> success:
7069
{
7170
// Default text response format is JSON.
7271
var text = TranscriptionResponseBody.FromJson(success.Result)?.Text;
73-
// Log text result.
74-
Debug.Log($"[Whisper_API.Samples] Succeeded to transcribe into: {text}.");
72+
Log.Debug("[Whisper_API.Samples] Succeeded to transcribe into: {0}.", text);
7573
break;
7674
}
7775
// Retryable failure
7876
case IUncertainRetryableResult<string> retryable:
7977
{
80-
Debug.LogError($"[Whisper_API.Samples] Failed to transcribe because -> {retryable.Message}.");
78+
Log.Error("[Whisper_API.Samples] Failed to transcribe because -> {0}.", retryable.Message);
8179
break;
8280
}
8381
// Failure
8482
case IUncertainFailureResult<string> failure:
8583
{
86-
Debug.LogError($"[Whisper_API.Samples] Failed to transcribe because -> {failure.Message}.");
84+
Log.Error("[Whisper_API.Samples] Failed to transcribe because -> {0}.", failure.Message);
8785
break;
8886
}
8987
default:

Assets/Mochineko/WhisperAPI.Samples/TranslationSample.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable enable
2+
using System;
23
using System.Net.Http;
34
using System.Threading;
45
using Assets.Mochineko.WhisperAPI;
@@ -14,12 +15,6 @@ namespace Mochineko.WhisperAPI.Samples
1415
/// </summary>
1516
public sealed class TranslationSample : MonoBehaviour
1617
{
17-
/// <summary>
18-
/// API key generated by OpenAPI.
19-
/// </summary>
20-
[SerializeField]
21-
private string apiKey = string.Empty;
22-
2318
/// <summary>
2419
/// File path of speech audio.
2520
/// </summary>
@@ -43,6 +38,8 @@ public void Translate()
4338

4439
private async UniTask TranslateAsync(CancellationToken cancellationToken)
4540
{
41+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
42+
4643
requestParameters.File = filePath;
4744

4845
await UniTask.SwitchToThreadPool();
@@ -58,7 +55,8 @@ private async UniTask TranslateAsync(CancellationToken cancellationToken)
5855
httpClient,
5956
filePath,
6057
requestParameters,
61-
innerCancellationToken),
58+
innerCancellationToken,
59+
debug: true),
6260
cancellationToken);
6361

6462
await UniTask.SwitchToMainThread(cancellationToken);
Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable enable
2+
using System;
23
using System.IO;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -15,19 +16,14 @@ namespace Mochineko.WhisperAPI.Tests
1516
internal sealed class TranscriptionTest
1617
{
1718
[Test]
18-
[RequiresPlayMode(false)]
19+
[RequiresPlayMode(true)]
1920
public async Task Transcribe()
2021
{
21-
// This file is a target of .gitignore.
22-
var apiKeyPath = Path.Combine(
23-
Application.dataPath,
24-
"Mochineko/Whisper_API.Tests/OpenAI_API_Key.txt");
25-
26-
var apiKey = await File.ReadAllTextAsync(apiKeyPath);
22+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
2723

2824
var filePath = Path.Combine(
2925
Application.dataPath,
30-
"Mochineko/Whisper_API.Tests/test.wav");
26+
"Mochineko/WhisperAPI.Tests/test.wav");
3127

3228
using var httpClient = new System.Net.Http.HttpClient();
3329

@@ -40,26 +36,11 @@ public async Task Transcribe()
4036
file: filePath,
4137
Model.Whisper1,
4238
temperature: 0f),
43-
CancellationToken.None);
44-
switch (apiResult)
45-
{
46-
case IUncertainSuccessResult<string> success:
47-
var result = TranscriptionResponseBody.FromJson(success.Result)?.Text;
48-
Debug.Log($"[Whisper_API.Tests] Result: {result}.");
49-
result?.Should().Be("とりあえず店の前、掃除しといてくれ。 内水も頼む。");
50-
break;
51-
52-
case IUncertainRetryableResult<string> retryable:
53-
Debug.LogError($"Retryable error -> {retryable.Message}");
54-
break;
55-
56-
case IUncertainFailureResult<string> failure:
57-
Debug.LogError($"Failure error -> {failure.Message}");
58-
break;
59-
60-
default:
61-
throw new UncertainResultPatternMatchException(nameof(apiResult));
62-
}
39+
CancellationToken.None,
40+
debug: true);
41+
42+
var result = TranscriptionResponseBody.FromJson(apiResult.Unwrap())?.Text;
43+
result?.Should().Be("とりあえず店の前、掃除しといてくれ。 内水も頼む。");
6344
}
6445
}
6546
}
Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable enable
2+
using System;
23
using System.IO;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -15,19 +16,14 @@ namespace Mochineko.WhisperAPI.Tests
1516
internal sealed class TranslationTest
1617
{
1718
[Test]
18-
[RequiresPlayMode(false)]
19+
[RequiresPlayMode(true)]
1920
public async Task Translate()
2021
{
21-
// This file is a target of .gitignore.
22-
var apiKeyPath = Path.Combine(
23-
Application.dataPath,
24-
"Mochineko/Whisper_API.Tests/OpenAI_API_Key.txt");
25-
26-
var apiKey = await File.ReadAllTextAsync(apiKeyPath);
22+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
2723

2824
var filePath = Path.Combine(
2925
Application.dataPath,
30-
"Mochineko/Whisper_API.Tests/test.wav");
26+
"Mochineko/WhisperAPI.Tests/test.wav");
3127

3228
using var httpClient = new System.Net.Http.HttpClient();
3329

@@ -40,26 +36,11 @@ public async Task Translate()
4036
file: filePath,
4137
Model.Whisper1,
4238
temperature: 0f),
43-
CancellationToken.None);
44-
switch (apiResult)
45-
{
46-
case IUncertainSuccessResult<string> success:
47-
var result = TranscriptionResponseBody.FromJson(success.Result)?.Text;
48-
Debug.Log($"[Whisper_API.Tests] Result: {result}.");
49-
result?.Should().Be("Please clean up the store. Please clean up the store.");
50-
break;
51-
52-
case IUncertainRetryableResult<string> retryable:
53-
Debug.LogError($"Retryable error -> {retryable.Message}");
54-
break;
55-
56-
case IUncertainFailureResult<string> failure:
57-
Debug.LogError($"Failure error -> {failure.Message}");
58-
break;
59-
60-
default:
61-
throw new UncertainResultPatternMatchException(nameof(apiResult));
62-
}
39+
CancellationToken.None,
40+
debug: true);
41+
42+
var result = TranscriptionResponseBody.FromJson(apiResult.Unwrap())?.Text;
43+
result?.Should().Be("Please clean up the store. Please clean up the store.");
6344
}
6445
}
6546
}

Assets/Mochineko/WhisperAPI/Mochineko.WhisperAPI.asmdef

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"rootNamespace": "",
44
"references": [
55
"GUID:2620e54ab6edb48f48d55966fd5662fb",
6-
"GUID:f51ebe6a0ceec4240a699833d6309b23"
6+
"GUID:f51ebe6a0ceec4240a699833d6309b23",
7+
"GUID:fd228c28e14f1a34cbe508676c914dd7",
8+
"GUID:2665a8d13d1b3f18800f46e256720795",
9+
"GUID:e0cd26848372d4e5c891c569017e11f1"
710
],
811
"includePlatforms": [],
912
"excludePlatforms": [],
@@ -15,5 +18,5 @@
1518
"autoReferenced": false,
1619
"defineConstraints": [],
1720
"versionDefines": [],
18-
"noEngineReferences": true
21+
"noEngineReferences": false
1922
}

0 commit comments

Comments
 (0)