forked from argmaxinc/WhisperKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionalTests.swift
170 lines (138 loc) · 6.06 KB
/
FunctionalTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// For licensing see accompanying LICENSE.md file.
// Copyright © 2024 Argmax, Inc. All rights reserved.
import CoreML
@testable import WhisperKit
import XCTest
@available(macOS 13, iOS 16, watchOS 10, visionOS 1, *)
final class FunctionalTests: XCTestCase {
func testInitLarge() async throws {
try await XCTAssertNoThrowAsync(
await WhisperKit(modelFolder: largev3ModelPath(), logLevel: .error)
)
}
func testOutputAll() async throws {
let modelPaths = try allModelPaths()
for modelPath in modelPaths {
let modelName = modelPath.split(separator: "/").last!
print("[Integration] Testing model \(modelName)")
let audioFilePath = try XCTUnwrap(
Bundle.module.path(forResource: "jfk", ofType: "wav"),
"Audio file not found"
)
let whisperKit = try await WhisperKit(
modelFolder: modelPath,
verbose: true,
logLevel: .debug
)
let transcriptionResult = try await XCTUnwrapAsync(
await whisperKit.transcribe(audioPath: audioFilePath),
"Transcription failed"
)
print("[Integration] \(transcriptionResult.text)")
XCTAssertEqual(
transcriptionResult.text.normalized,
" And so my fellow Americans ask not what your country can do for you, ask what you can do for your country.".normalized,
"Transcription result does not match expected result for model \(modelName)"
)
}
}
func testRealTimeFactorTiny() async throws {
let modelPath = try tinyModelPath()
let metrics: [XCTMetric] = [XCTMemoryMetric(), XCTStorageMetric(), XCTClockMetric()]
let measureOptions = XCTMeasureOptions.default
measureOptions.iterationCount = 5
let audioFilePath = try XCTUnwrap(
Bundle.module.path(forResource: "jfk", ofType: "wav"),
"Audio file not found"
)
let whisperKit = try await WhisperKit(modelFolder: modelPath)
measure(metrics: metrics, options: measureOptions) {
let dispatchSemaphore = DispatchSemaphore(value: 0)
Task {
let transcriptionResult = try await XCTUnwrapAsync(
await whisperKit.transcribe(audioPath: audioFilePath),
"Transcription failed"
)
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
}
}
func testRealTimeFactorLarge() async throws {
let modelPath = try largev3ModelPath()
let metrics: [XCTMetric] = [XCTMemoryMetric(), XCTStorageMetric(), XCTClockMetric()]
let measureOptions = XCTMeasureOptions.default
measureOptions.iterationCount = 5
let audioFilePath = try XCTUnwrap(
Bundle.module.path(forResource: "jfk", ofType: "wav"),
"Audio file not found"
)
let whisperKit = try await WhisperKit(modelFolder: modelPath, verbose: false)
measure(metrics: metrics, options: measureOptions) {
let dispatchSemaphore = DispatchSemaphore(value: 0)
Task {
let transcriptionResult = try await XCTUnwrapAsync(
await whisperKit.transcribe(audioPath: audioFilePath),
"Transcription failed"
)
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
}
}
func testBaseImplementation() throws {
let audioFilePath = try XCTUnwrap(
Bundle.module.path(forResource: "jfk", ofType: "wav"),
"Audio file not found"
)
let dispatchSemaphore = DispatchSemaphore(value: 0)
Task {
let whisperKit = try await XCTUnwrapAsync(await WhisperKit(model: "large-v3"))
let transcriptionResult = try await XCTUnwrapAsync(
await whisperKit.transcribe(audioPath: audioFilePath),
"Transcription failed"
)
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
}
func testAsyncImplementation() async throws {
let audioFilePath = try XCTUnwrap(
Bundle.module.path(forResource: "jfk", ofType: "wav"),
"Audio file not found"
)
let whisperKit = try await WhisperKit(model: "large-v3")
let transcriptionResult = try await XCTUnwrapAsync(
await whisperKit.transcribe(audioPath: audioFilePath),
"Transcription failed"
)
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
}
func testModelSearchPathLarge() async throws {
guard let audioFilePath = Bundle.module.path(forResource: "jfk", ofType: "wav") else {
XCTFail("Audio file not found")
return
}
var pipe = try await WhisperKit(model: "large-v3", verbose: true, logLevel: .debug)
guard let transcriptionResult = try await pipe.transcribe(audioPath: audioFilePath) else {
XCTFail("Transcription failed")
return
}
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
pipe = try await WhisperKit(model: "distil*large-v3", verbose: true, logLevel: .debug)
guard let transcriptionResult = try await pipe.transcribe(audioPath: audioFilePath) else {
XCTFail("Transcription failed")
return
}
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
pipe = try await WhisperKit(model: "distil-whisper_distil-large-v3", verbose: true, logLevel: .debug)
guard let transcriptionResult = try await pipe.transcribe(audioPath: audioFilePath) else {
XCTFail("Transcription failed")
return
}
XCTAssertGreaterThan(transcriptionResult.text.count, 0)
}
}