-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transcriber.py
More file actions
277 lines (215 loc) Β· 9.39 KB
/
test_transcriber.py
File metadata and controls
277 lines (215 loc) Β· 9.39 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
"""
Test script for transcriber.py functions
Tests both Groq and faster-whisper transcription methods
"""
import os
import sys
import json
import tempfile
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Add project root to path
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)
from services.transcriber import transcribe_with_faster_whisper, transcribe_with_groq, transcribe_video
def test_faster_whisper_transcription():
"""Test faster-whisper transcription method"""
print("\n" + "="*60)
print("π§ͺ Testing faster-whisper transcription")
print("="*60)
sample_video = os.path.join(project_root, "sample-files", "sample-video-2.mp4")
if not os.path.exists(sample_video):
print(f"β Sample video not found: {sample_video}")
return False
print(f"π Using sample video: {sample_video}")
print(f"π File size: {os.path.getsize(sample_video)} bytes")
try:
print("π€ Starting faster-whisper transcription...")
start_time = datetime.now()
transcript = transcribe_with_faster_whisper(sample_video)
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
print(f"β±οΈ Transcription completed in {duration:.2f} seconds")
print(f"π Generated {len(transcript)} transcript segments")
# Display transcript segments
print("\nπ Transcript segments:")
for i, segment in enumerate(transcript[:5]): # Show first 5 segments
print(f" {i+1}. [{segment['start']:.2f}s - {segment['end']:.2f}s]: {segment['text']}")
if len(transcript) > 5:
print(f" ... and {len(transcript) - 5} more segments")
# Validate transcript structure
for segment in transcript:
if not all(key in segment for key in ['text', 'start', 'end']):
print("β Invalid segment structure")
return False
if not isinstance(segment['start'], (int, float)) or not isinstance(segment['end'], (int, float)):
print("β Invalid timestamp types")
return False
print("β
faster-whisper transcription test passed!")
return True
except Exception as e:
print(f"β faster-whisper transcription failed: {e}")
import traceback
traceback.print_exc()
return False
def test_groq_transcription():
"""Test Groq API transcription method"""
print("\n" + "="*60)
print("π§ͺ Testing Groq API transcription")
print("="*60)
# Check if Groq API key is available
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
print("β οΈ GROQ_API_KEY not found in environment variables")
print("π‘ Skipping Groq test - set GROQ_API_KEY to test this method")
return True # Return True since this is expected behavior
sample_video = os.path.join(project_root, "sample-files", "sample-video.mp4")
if not os.path.exists(sample_video):
print(f"β Sample video not found: {sample_video}")
return False
print(f"π Using sample video: {sample_video}")
print(f"π Groq API key found (length: {len(api_key)} chars)")
try:
print("π€ Starting Groq API transcription...")
start_time = datetime.now()
transcript = transcribe_with_groq(sample_video)
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
print(f"β±οΈ Transcription completed in {duration:.2f} seconds")
print(f"π Generated {len(transcript)} transcript segments")
# Display transcript segments
print("\nπ Transcript segments:")
for i, segment in enumerate(transcript[:5]): # Show first 5 segments
print(f" {i+1}. [{segment['start']:.2f}s - {segment['end']:.2f}s]: {segment['text']}")
if len(transcript) > 5:
print(f" ... and {len(transcript) - 5} more segments")
# Validate transcript structure
for segment in transcript:
if not all(key in segment for key in ['text', 'start', 'end']):
print("β Invalid segment structure")
return False
print("β
Groq API transcription test passed!")
return True
except Exception as e:
print(f"β Groq API transcription failed: {e}")
import traceback
traceback.print_exc()
return False
def test_transcribe_video_function():
"""Test the main transcribe_video function with different methods"""
print("\n" + "="*60)
print("π§ͺ Testing transcribe_video function")
print("="*60)
sample_video = os.path.join(project_root, "sample-files", "sample-video.mp4")
if not os.path.exists(sample_video):
print(f"β Sample video not found: {sample_video}")
return False
# Test with faster-whisper method
try:
print("π€ Testing transcribe_video with faster-whisper method...")
transcript = transcribe_video(sample_video, method="faster-whisper")
print(f"π Generated {len(transcript)} segments with faster-whisper")
print("β
transcribe_video (faster-whisper) test passed!")
except Exception as e:
print(f"β transcribe_video (faster-whisper) failed: {e}")
return False
# Test with Groq method if API key is available
if os.getenv("GROQ_API_KEY"):
try:
print("π€ Testing transcribe_video with Groq method...")
transcript = transcribe_video(sample_video, method="groq")
print(f"π Generated {len(transcript)} segments with Groq")
print("β
transcribe_video (groq) test passed!")
except Exception as e:
print(f"β transcribe_video (groq) failed: {e}")
return False
else:
print("β οΈ Skipping Groq method test - GROQ_API_KEY not available")
# Test invalid method
try:
print("π§ͺ Testing invalid method handling...")
transcribe_video(sample_video, method="invalid_method")
print("β Should have raised ValueError for invalid method")
return False
except ValueError as e:
print(f"β
Correctly raised ValueError: {e}")
except Exception as e:
print(f"β Unexpected error: {e}")
return False
return True
def test_transcribe_video_with_upload():
"""Test transcribe_video function with R2 upload simulation"""
print("\n" + "="*60)
print("π§ͺ Testing transcribe_video with video_id (R2 upload)")
print("="*60)
sample_video = os.path.join(project_root, "sample-files", "sample-video.mp4")
if not os.path.exists(sample_video):
print(f"β Sample video not found: {sample_video}")
return False
try:
print("π€ Testing transcribe_video with video_id parameter...")
# Note: This will attempt R2 upload, which may fail if R2 credentials aren't configured
# That's expected behavior for testing
transcript = transcribe_video(
sample_video,
method="faster-whisper",
video_id="test_video_123"
)
print(f"π Generated {len(transcript)} segments")
print("β
transcribe_video with video_id test completed!")
print("π‘ Note: R2 upload may have failed if credentials not configured - this is expected")
return True
except Exception as e:
print(f"β transcribe_video with video_id failed: {e}")
return False
def run_all_tests():
"""Run all transcriber tests"""
print("π Starting transcriber.py test suite")
print(f"π Project root: {project_root}")
tests = [
("faster-whisper transcription", test_faster_whisper_transcription),
("Groq API transcription", test_groq_transcription),
("transcribe_video function", test_transcribe_video_function),
("transcribe_video with upload", test_transcribe_video_with_upload)
]
results = []
for test_name, test_func in tests:
try:
result = test_func()
results.append((test_name, result))
except Exception as e:
print(f"β Test '{test_name}' crashed: {e}")
results.append((test_name, False))
# Summary
print("\n" + "="*60)
print("π TEST RESULTS SUMMARY")
print("="*60)
passed = 0
total = len(results)
for test_name, result in results:
status = "β
PASSED" if result else "β FAILED"
print(f"{status}: {test_name}")
if result:
passed += 1
print(f"\nπ― Overall: {passed}/{total} tests passed")
if passed == total:
print("π All tests passed!")
return True
else:
print("π₯ Some tests failed!")
return False
if __name__ == "__main__":
print("=" * 80)
print("π§ͺ TRANSCRIBER.PY TEST SUITE")
print("=" * 80)
success = run_all_tests()
print("\n" + "=" * 80)
if success:
print("π Test suite completed successfully!")
else:
print("π₯ Test suite completed with failures!")
print("=" * 80)
sys.exit(0 if success else 1)