File size: 10,514 Bytes
76f9cd2 |
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 |
"""
Test remote GPU transcription functionality
ζ΅θ―θΏη¨GPU转ε½εθ½
"""
import pytest
import asyncio
import os
import tempfile
from pathlib import Path
from typing import Dict, Any
from src.tools.transcription_tools import transcribe_audio_file_tool
from src.services.audio_processing_service import AudioProcessingService
class TestRemoteTranscription:
"""Test remote GPU transcription integration"""
def test_transcription_tools_initialization(self):
"""Test transcription tools initialization"""
print("\nπ§ Testing transcription tools initialization...")
# Test that the tool can be imported
assert transcribe_audio_file_tool is not None
print("β
Transcription tools initialized successfully")
@pytest.mark.asyncio
async def test_create_sample_audio_file(self, temp_dir: str):
"""Test creating a sample audio file for transcription testing"""
print("\nπ΅ Creating sample audio file for testing...")
import ffmpeg
# Create a short sample audio file
sample_file = os.path.join(temp_dir, "sample_audio.mp3")
try:
# Generate a short sine wave audio for testing
(
ffmpeg
.input('sine=frequency=440:duration=5', f='lavfi')
.output(sample_file, acodec='mp3', ar=16000)
.overwrite_output()
.run(quiet=True)
)
assert os.path.exists(sample_file)
assert os.path.getsize(sample_file) > 0
print(f"β
Sample audio file created: {sample_file}")
print(f" File size: {os.path.getsize(sample_file)} bytes")
return sample_file
except Exception as e:
print(f"β Failed to create sample audio file: {str(e)}")
pytest.skip(f"Failed to create sample audio file: {str(e)}")
@pytest.mark.asyncio
async def test_remote_transcription_endpoint_connectivity(self):
"""Test connectivity to remote transcription endpoint"""
print("\nπ Testing remote transcription endpoint connectivity...")
import aiohttp
import json
# Read endpoint config
try:
with open("endpoint_config.json", "r") as f:
endpoint_config = json.load(f)
endpoint_url = endpoint_config["transcribe_audio"]
async with aiohttp.ClientSession() as session:
# Test with a simple HEAD request to check if endpoint is reachable
async with session.head(endpoint_url, timeout=10) as response:
print(f"β
Endpoint connectivity test:")
print(f" URL: {endpoint_url}")
print(f" Status: {response.status}")
print(f" Headers: {dict(response.headers)}")
# We expect either 200 (OK) or 405 (Method Not Allowed) for HEAD requests
assert response.status in [200, 405, 404], f"Unexpected status: {response.status}"
except asyncio.TimeoutError:
print(f"β οΈ Endpoint connectivity timeout (expected if Modal is sleeping)")
pytest.skip("Endpoint connectivity timeout")
except Exception as e:
print(f"β οΈ Endpoint connectivity test failed: {str(e)}")
print(" This might be expected if Modal endpoint is not running")
@pytest.mark.asyncio
async def test_transcription_tool_interface(self, temp_dir: str):
"""Test transcription tool interface with sample audio"""
print("\nπ€ Testing transcription tool interface...")
# Create a sample audio file first
sample_file = await self.test_create_sample_audio_file(temp_dir)
try:
# Test the transcription tool
result = await transcribe_audio_file_tool(
audio_file_path=sample_file,
model_size="base",
language="en",
output_format="srt",
enable_speaker_diarization=False
)
print(f"π Transcription tool result:")
print(f" Status: {result.get('processing_status', 'unknown')}")
print(f" Audio file: {result.get('audio_file', 'N/A')}")
print(f" Model used: {result.get('model_used', 'N/A')}")
print(f" Duration: {result.get('audio_duration', 0):.2f}s")
if result.get("processing_status") == "success":
print(f" TXT file: {result.get('txt_file_path', 'N/A')}")
print(f" SRT file: {result.get('srt_file_path', 'N/A')}")
print(f" Segments: {result.get('segment_count', 0)}")
print("β
Transcription tool interface test successful")
# Verify output files exist
if result.get('txt_file_path'):
assert os.path.exists(result['txt_file_path'])
if result.get('srt_file_path'):
assert os.path.exists(result['srt_file_path'])
else:
print(f" Error: {result.get('error_message', 'Unknown error')}")
print("β οΈ Transcription failed (might be due to remote endpoint)")
except Exception as e:
print(f"β Transcription tool test failed: {str(e)}")
print(" This might be expected if remote endpoint is not available")
@pytest.mark.asyncio
async def test_transcription_with_speaker_diarization(self, temp_dir: str):
"""Test transcription with speaker diarization enabled"""
print("\nπ₯ Testing transcription with speaker diarization...")
# Create a sample audio file
sample_file = await self.test_create_sample_audio_file(temp_dir)
try:
# Test transcription with speaker diarization
result = await transcribe_audio_file_tool(
audio_file_path=sample_file,
model_size="base",
language="auto",
output_format="srt",
enable_speaker_diarization=True
)
print(f"π Speaker diarization result:")
print(f" Status: {result.get('processing_status', 'unknown')}")
print(f" Speaker diarization enabled: {result.get('speaker_diarization_enabled', False)}")
print(f" Global speaker count: {result.get('global_speaker_count', 0)}")
if result.get("processing_status") == "success":
speaker_summary = result.get('speaker_summary', {})
print(f" Speaker summary: {speaker_summary}")
print("β
Speaker diarization test successful")
else:
print(f" Error: {result.get('error_message', 'Unknown error')}")
print("β οΈ Speaker diarization failed (might be due to remote endpoint or HF token)")
except Exception as e:
print(f"β Speaker diarization test failed: {str(e)}")
print(" This might be expected if HF token is not configured or endpoint unavailable")
@pytest.mark.asyncio
async def test_different_transcription_models(self, temp_dir: str):
"""Test transcription with different models"""
print("\nπ§ Testing different transcription models...")
sample_file = await self.test_create_sample_audio_file(temp_dir)
models_to_test = ["tiny", "base", "small"]
for model in models_to_test:
print(f"\n Testing model: {model}")
try:
result = await transcribe_audio_file_tool(
audio_file_path=sample_file,
model_size=model,
language="auto",
output_format="txt",
enable_speaker_diarization=False
)
if result.get("processing_status") == "success":
print(f" β
{model} model: Success")
print(f" Segments: {result.get('segment_count', 0)}")
print(f" Duration: {result.get('audio_duration', 0):.2f}s")
else:
print(f" β οΈ {model} model: Failed - {result.get('error_message', 'Unknown')}")
except Exception as e:
print(f" β {model} model: Exception - {str(e)}")
@pytest.mark.asyncio
async def test_transcription_output_formats(self, temp_dir: str):
"""Test different transcription output formats"""
print("\nπ Testing different output formats...")
sample_file = await self.test_create_sample_audio_file(temp_dir)
formats_to_test = ["txt", "srt", "json"]
for format_type in formats_to_test:
print(f"\n Testing format: {format_type}")
try:
result = await transcribe_audio_file_tool(
audio_file_path=sample_file,
model_size="base",
language="auto",
output_format=format_type,
enable_speaker_diarization=False
)
if result.get("processing_status") == "success":
print(f" β
{format_type} format: Success")
# Check for format-specific outputs
if format_type == "txt" and result.get('txt_file_path'):
assert os.path.exists(result['txt_file_path'])
elif format_type == "srt" and result.get('srt_file_path'):
assert os.path.exists(result['srt_file_path'])
else:
print(f" β οΈ {format_type} format: Failed - {result.get('error_message', 'Unknown')}")
except Exception as e:
print(f" β {format_type} format: Exception - {str(e)}")
if __name__ == "__main__":
# Run tests with verbose output
pytest.main([__file__, "-v", "-s"]) |