Skip to content

Commit 71b66fb

Browse files
committed
Refactor to static
1 parent 53471c9 commit 71b66fb

File tree

7 files changed

+108
-163
lines changed

7 files changed

+108
-163
lines changed

Assets/Mochineko/Whisper_API.Samples/Mochineko.Whisper_API.Samples.asmdef

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"includePlatforms": [],
99
"excludePlatforms": [],
1010
"allowUnsafeCode": false,
11-
"overrideReferences": false,
11+
"overrideReferences": true,
1212
"precompiledReferences": [],
1313
"autoReferenced": false,
1414
"defineConstraints": [],

Assets/Mochineko/Whisper_API.Samples/TranscriptionSample.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
22
using System;
3+
using System.Net.Http;
34
using Cysharp.Threading.Tasks;
45
using UnityEngine;
56
using UnityEngine.Assertions;
@@ -21,16 +22,13 @@ public sealed class TranscriptionSample : MonoBehaviour
2122
/// </summary>
2223
[SerializeField] private string filePath = string.Empty;
2324

24-
private Transcription? connection;
25-
25+
private static readonly HttpClient httpClient = new();
26+
2627
private void Start()
2728
{
2829
// API Key must be set.
2930
Assert.IsNotNull(apiKey);
3031

31-
// Create instance of WhisperTranscriptionConnection.
32-
connection = new Transcription(apiKey);
33-
3432
// If you want to specify response format, language, etc..., please use other initialization:
3533
// connection = new WhisperTranscriptionConnection(apiKey, new APIRequestBody(
3634
// file: "",
@@ -45,12 +43,6 @@ private void Start()
4543
public async void Transcribe()
4644
{
4745
// Validations
48-
if (connection == null)
49-
{
50-
Debug.LogError($"[Whisper_API.Transcription.Samples] Connection is null.");
51-
return;
52-
}
53-
5446
if (string.IsNullOrEmpty(filePath))
5547
{
5648
Debug.LogError($"[Whisper_API.Transcription.Samples] File path is null or empty.");
@@ -61,8 +53,13 @@ public async void Transcribe()
6153
try
6254
{
6355
// Transcribe speech into text by Whisper transcription API.
64-
result = await connection
65-
.TranscribeFromFileAsync(filePath, this.GetCancellationTokenOnDestroy());
56+
result = await Transcription
57+
.TranscribeFromFileAsync(
58+
apiKey,
59+
httpClient,
60+
filePath,
61+
Model.Whisper1,
62+
this.GetCancellationTokenOnDestroy());
6663
}
6764
catch (Exception e)
6865
{

Assets/Mochineko/Whisper_API.Tests/Mochineko.Whisper_API.Tests.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"GUID:27619889b8ba8c24980f49ee34dbb44a",
66
"GUID:0acc523941302664db1f4e527237feb3",
77
"GUID:e372c541aba5148868e12aa078ca7c20",
8-
"GUID:12e7ae99596e14eef8970a67eb588899"
8+
"GUID:12e7ae99596e14eef8970a67eb588899",
9+
"GUID:f51ebe6a0ceec4240a699833d6309b23"
910
],
1011
"includePlatforms": [
1112
"Editor"

Assets/Mochineko/Whisper_API.Tests/TranscriptionTest.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ public async Task Transcribe()
2727
Application.dataPath,
2828
"Mochineko/Whisper_API.Tests/test.wav");
2929

30-
var connection = new Transcription(apiKey, Model.Whisper1);
31-
32-
var result = await connection.TranscribeFromFileAsync(filePath, CancellationToken.None);
30+
using var httpClient = new System.Net.Http.HttpClient();
31+
32+
var result = await Transcription
33+
.TranscribeFromFileAsync(
34+
apiKey,
35+
httpClient,
36+
filePath,
37+
Model.Whisper1,
38+
CancellationToken.None);
3339

3440
string.IsNullOrEmpty(result).Should().BeFalse();
3541

Assets/Mochineko/Whisper_API/Mochineko.Whisper_API.asmdef

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
{
22
"name": "Mochineko.Whisper_API",
33
"rootNamespace": "",
4-
"references": [],
4+
"references": [
5+
"GUID:f51ebe6a0ceec4240a699833d6309b23"
6+
],
57
"includePlatforms": [],
68
"excludePlatforms": [],
79
"allowUnsafeCode": false,
8-
"overrideReferences": false,
9-
"precompiledReferences": [],
10-
"autoReferenced": true,
10+
"overrideReferences": true,
11+
"precompiledReferences": [
12+
"Newtonsoft.Json.dll"
13+
],
14+
"autoReferenced": false,
1115
"defineConstraints": [],
1216
"versionDefines": [],
1317
"noEngineReferences": true
Lines changed: 76 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,69 @@
11
#nullable enable
22
using System;
3-
using System.Collections.Generic;
43
using System.IO;
54
using System.Net.Http;
65
using System.Threading;
7-
using System.Threading.Tasks;
8-
using Newtonsoft.Json;
6+
using Cysharp.Threading.Tasks;
97

108
namespace Mochineko.Whisper_API
119
{
1210
/// <summary>
1311
/// OpenAI Whisper transcription API.
1412
/// https://platform.openai.com/docs/api-reference/audio/create
1513
/// </summary>
16-
public sealed class Transcription
14+
public static class Transcription
1715
{
1816
private const string EndPoint = "https://api.openai.com/v1/audio/transcriptions";
1917

20-
private readonly IReadOnlyDictionary<string, string> headers;
21-
private readonly TranscriptionRequestBody requestBody;
22-
23-
private static HttpClient HttpClient
24-
=> HttpClientPool.PooledClient;
25-
2618
/// <summary>
27-
/// Create an instance of Whisper transcription API connection.
19+
/// Transcribes speech audio into text by Whisper transcription API.
2820
/// </summary>
29-
/// <param name="apiKey">API key generated by OpenAI</param>
30-
/// <param name="model">Speech recognition model</param>
31-
/// <exception cref="ArgumentNullException">API Key must be set</exception>
32-
public Transcription(string apiKey, Model model = Model.Whisper1)
21+
public static async UniTask<string> TranscribeAsync(
22+
string apiKey,
23+
HttpClient httpClient,
24+
Stream fileStream,
25+
string fileName,
26+
Model model,
27+
CancellationToken cancellationToken,
28+
string? prompt = null,
29+
string? responseFormat = null,
30+
float? temperature = null,
31+
string? language = null
32+
)
3333
{
34-
if (string.IsNullOrEmpty(apiKey))
35-
{
36-
throw new ArgumentNullException(nameof(apiKey));
37-
}
34+
cancellationToken.ThrowIfCancellationRequested();
3835

39-
this.headers = CreateHeader(apiKey);
40-
this.requestBody = new TranscriptionRequestBody(string.Empty, model);
41-
}
42-
43-
/// <summary>
44-
/// Create an instance of Whisper transcription API connection.
45-
/// </summary>
46-
/// <param name="apiKey">API key generated by OpenAI</param>
47-
/// <param name="requestBody">Request parameters</param>
48-
/// <exception cref="ArgumentNullException">API Key must be set</exception>
49-
public Transcription(string apiKey, TranscriptionRequestBody requestBody)
50-
{
5136
if (string.IsNullOrEmpty(apiKey))
5237
{
5338
throw new ArgumentNullException(nameof(apiKey));
5439
}
5540

56-
this.headers = CreateHeader(apiKey);
57-
this.requestBody = requestBody;
58-
}
59-
60-
private static IReadOnlyDictionary<string, string> CreateHeader(string apiKey)
61-
=> new Dictionary<string, string>
41+
if (string.IsNullOrEmpty(fileName))
6242
{
63-
["Authorization"] = $"Bearer {apiKey}",
64-
};
43+
throw new ArgumentNullException(nameof(fileName));
44+
}
6545

66-
private static HttpRequestMessage CreateRequestMessage(
67-
IReadOnlyDictionary<string, string> headers,
68-
TranscriptionRequestBody requestBody,
69-
Stream fileStream)
70-
{
71-
var requestMessage = new HttpRequestMessage(HttpMethod.Post, EndPoint);
72-
foreach (var header in headers)
46+
if (!TranscriptionRequestBody.IsAvailableFormat(fileName))
7347
{
74-
requestMessage.Headers.Add(header.Key, header.Value);
48+
throw new InvalidDataException(fileName);
7549
}
50+
51+
var requestBody = new TranscriptionRequestBody(
52+
fileName,
53+
model,
54+
prompt,
55+
responseFormat,
56+
temperature,
57+
language);
58+
59+
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, EndPoint);
60+
requestMessage.Headers.Add("Authorization", $"Bearer {apiKey}");
7661

7762
var requestContent = new MultipartFormDataContent();
7863

7964
requestContent.Add(
8065
content: new StringContent(
81-
content: $"{requestBody.Model}",
66+
content: requestBody.Model,
8267
encoding: System.Text.Encoding.UTF8),
8368
name: "model");
8469

@@ -89,92 +74,9 @@ private static HttpRequestMessage CreateRequestMessage(
8974

9075
requestMessage.Content = requestContent;
9176

92-
return requestMessage;
93-
}
94-
95-
/// <summary>
96-
/// Transcribes speech audio into text by Whisper transcription API.
97-
/// </summary>
98-
/// <param name="filePath"></param>
99-
/// <param name="cancellationToken"></param>
100-
/// <returns>Response text</returns>
101-
/// <exception cref="Exception">System exceptions</exception>
102-
/// <exception cref="APIErrorException">API error response</exception>
103-
/// <exception cref="HttpRequestException">Network error</exception>
104-
/// <exception cref="TaskCanceledException">Cancellation or timeout</exception>
105-
/// <exception cref="JsonSerializationException">JSON error</exception>
106-
/// <exception cref="ArgumentNullException">File path is null or empty</exception>
107-
/// <exception cref="FileNotFoundException">Audio file not found</exception>
108-
/// <exception cref="InvalidDataException">Not available audio format</exception>
109-
/// <exception cref="OperationCanceledException">Cancelled on called</exception>
110-
public async Task<string> TranscribeFromFileAsync(string filePath, CancellationToken cancellationToken)
111-
{
112-
cancellationToken.ThrowIfCancellationRequested();
113-
114-
if (string.IsNullOrEmpty(filePath))
115-
{
116-
throw new ArgumentNullException(nameof(filePath));
117-
}
118-
119-
if (!File.Exists(filePath))
120-
{
121-
throw new FileNotFoundException(filePath);
122-
}
123-
124-
if (!TranscriptionRequestBody.IsAvailableFormat(filePath))
125-
{
126-
throw new InvalidDataException(filePath);
127-
}
128-
129-
await using var stream = File.OpenRead(filePath);
130-
131-
return await TranscribeAsync(
132-
fileStream: stream,
133-
fileName: Path.GetFileName(filePath),
134-
cancellationToken);
135-
}
136-
137-
/// <summary>
138-
/// Transcribes speech audio into text by Whisper transcription API.
139-
/// </summary>
140-
/// <param name="fileStream">File data stream of speech audio</param>
141-
/// <param name="fileName">File name of speech audio</param>
142-
/// <param name="cancellationToken">Cancellation token</param>
143-
/// <returns>Response text.</returns>
144-
/// <exception cref="Exception">System exceptions</exception>
145-
/// <exception cref="APIErrorException">API error response</exception>
146-
/// <exception cref="HttpRequestException">Network error</exception>
147-
/// <exception cref="TaskCanceledException">Cancellation or timeout</exception>
148-
/// <exception cref="JsonSerializationException">JSON error</exception>
149-
/// <exception cref="ArgumentNullException">Argument is null or empty</exception>
150-
/// <exception cref="InvalidDataException">Not available audio format</exception>
151-
/// <exception cref="OperationCanceledException">Cancelled on called</exception>
152-
public async Task<string> TranscribeAsync(
153-
Stream fileStream,
154-
string fileName,
155-
CancellationToken cancellationToken)
156-
{
157-
cancellationToken.ThrowIfCancellationRequested();
158-
159-
if (string.IsNullOrEmpty(fileName))
160-
{
161-
throw new ArgumentNullException(nameof(fileName));
162-
}
163-
164-
if (!TranscriptionRequestBody.IsAvailableFormat(fileName))
165-
{
166-
throw new InvalidDataException(fileName);
167-
}
168-
169-
requestBody.File = fileName;
170-
171-
using var requestMessage = CreateRequestMessage(
172-
headers,
173-
requestBody,
174-
fileStream);
175-
17677
// Post request and receive response
177-
using var responseMessage = await HttpClient.SendAsync(requestMessage, cancellationToken);
78+
using var responseMessage = await httpClient
79+
.SendAsync(requestMessage, cancellationToken);
17880
if (responseMessage == null)
17981
{
18082
throw new Exception($"[Whisper_API.Transcription] HttpResponseMessage is null.");
@@ -209,5 +111,46 @@ public async Task<string> TranscribeAsync(
209111
}
210112
}
211113
}
114+
115+
/// <summary>
116+
/// Transcribes speech audio into text by Whisper transcription API.
117+
/// </summary>
118+
public static async UniTask<string> TranscribeFromFileAsync(
119+
string apiKey,
120+
HttpClient httpClient,
121+
string filePath,
122+
Model model,
123+
CancellationToken cancellationToken,
124+
string? prompt = null,
125+
string? responseFormat = null,
126+
float? temperature = null,
127+
string? language = null)
128+
{
129+
cancellationToken.ThrowIfCancellationRequested();
130+
131+
if (string.IsNullOrEmpty(filePath))
132+
{
133+
throw new ArgumentNullException(nameof(filePath));
134+
}
135+
136+
if (!File.Exists(filePath))
137+
{
138+
throw new FileNotFoundException(filePath);
139+
}
140+
141+
await using var stream = File.OpenRead(filePath);
142+
143+
return await TranscribeAsync(
144+
apiKey,
145+
httpClient,
146+
fileStream: stream,
147+
fileName: Path.GetFileName(filePath),
148+
model,
149+
cancellationToken,
150+
prompt,
151+
responseFormat,
152+
temperature,
153+
language);
154+
}
212155
}
213156
}

0 commit comments

Comments
 (0)