|
| 1 | +""" |
| 2 | +A test script to test the gRPC service for Moonshine transcription |
| 3 | +""" |
| 4 | +import unittest |
| 5 | +import subprocess |
| 6 | +import time |
| 7 | +import os |
| 8 | +import tempfile |
| 9 | +import shutil |
| 10 | +import backend_pb2 |
| 11 | +import backend_pb2_grpc |
| 12 | + |
| 13 | +import grpc |
| 14 | + |
| 15 | + |
| 16 | +class TestBackendServicer(unittest.TestCase): |
| 17 | + """ |
| 18 | + TestBackendServicer is the class that tests the gRPC service |
| 19 | + """ |
| 20 | + def setUp(self): |
| 21 | + """ |
| 22 | + This method sets up the gRPC service by starting the server |
| 23 | + """ |
| 24 | + self.service = subprocess.Popen(["python3", "backend.py", "--addr", "localhost:50051"]) |
| 25 | + time.sleep(10) |
| 26 | + |
| 27 | + def tearDown(self) -> None: |
| 28 | + """ |
| 29 | + This method tears down the gRPC service by terminating the server |
| 30 | + """ |
| 31 | + self.service.terminate() |
| 32 | + self.service.wait() |
| 33 | + |
| 34 | + def test_server_startup(self): |
| 35 | + """ |
| 36 | + This method tests if the server starts up successfully |
| 37 | + """ |
| 38 | + try: |
| 39 | + self.setUp() |
| 40 | + with grpc.insecure_channel("localhost:50051") as channel: |
| 41 | + stub = backend_pb2_grpc.BackendStub(channel) |
| 42 | + response = stub.Health(backend_pb2.HealthMessage()) |
| 43 | + self.assertEqual(response.message, b'OK') |
| 44 | + except Exception as err: |
| 45 | + print(err) |
| 46 | + self.fail("Server failed to start") |
| 47 | + finally: |
| 48 | + self.tearDown() |
| 49 | + |
| 50 | + def test_load_model(self): |
| 51 | + """ |
| 52 | + This method tests if the model is loaded successfully |
| 53 | + """ |
| 54 | + try: |
| 55 | + self.setUp() |
| 56 | + with grpc.insecure_channel("localhost:50051") as channel: |
| 57 | + stub = backend_pb2_grpc.BackendStub(channel) |
| 58 | + response = stub.LoadModel(backend_pb2.ModelOptions(Model="moonshine/tiny")) |
| 59 | + self.assertTrue(response.success) |
| 60 | + self.assertEqual(response.message, "Model loaded successfully") |
| 61 | + except Exception as err: |
| 62 | + print(err) |
| 63 | + self.fail("LoadModel service failed") |
| 64 | + finally: |
| 65 | + self.tearDown() |
| 66 | + |
| 67 | + def test_audio_transcription(self): |
| 68 | + """ |
| 69 | + This method tests if audio transcription works successfully |
| 70 | + """ |
| 71 | + # Create a temporary directory for the audio file |
| 72 | + temp_dir = tempfile.mkdtemp() |
| 73 | + audio_file = os.path.join(temp_dir, 'audio.wav') |
| 74 | + |
| 75 | + try: |
| 76 | + # Download the audio file to the temporary directory |
| 77 | + print(f"Downloading audio file to {audio_file}...") |
| 78 | + url = "https://cdn.openai.com/whisper/draft-20220913a/micro-machines.wav" |
| 79 | + result = subprocess.run( |
| 80 | + ["wget", "-q", url, "-O", audio_file], |
| 81 | + capture_output=True, |
| 82 | + text=True |
| 83 | + ) |
| 84 | + if result.returncode != 0: |
| 85 | + self.fail(f"Failed to download audio file: {result.stderr}") |
| 86 | + |
| 87 | + # Verify the file was downloaded |
| 88 | + if not os.path.exists(audio_file): |
| 89 | + self.fail(f"Audio file was not downloaded to {audio_file}") |
| 90 | + |
| 91 | + self.setUp() |
| 92 | + with grpc.insecure_channel("localhost:50051") as channel: |
| 93 | + stub = backend_pb2_grpc.BackendStub(channel) |
| 94 | + # Load the model first |
| 95 | + load_response = stub.LoadModel(backend_pb2.ModelOptions(Model="moonshine/tiny")) |
| 96 | + self.assertTrue(load_response.success) |
| 97 | + |
| 98 | + # Perform transcription |
| 99 | + transcript_request = backend_pb2.TranscriptRequest(dst=audio_file) |
| 100 | + transcript_response = stub.AudioTranscription(transcript_request) |
| 101 | + |
| 102 | + # Print the transcribed text for debugging |
| 103 | + print(f"Transcribed text: {transcript_response.text}") |
| 104 | + print(f"Number of segments: {len(transcript_response.segments)}") |
| 105 | + |
| 106 | + # Verify response structure |
| 107 | + self.assertIsNotNone(transcript_response) |
| 108 | + self.assertIsNotNone(transcript_response.text) |
| 109 | + # Protobuf repeated fields return a sequence, not a list |
| 110 | + self.assertIsNotNone(transcript_response.segments) |
| 111 | + # Check if segments is iterable (has length) |
| 112 | + self.assertGreaterEqual(len(transcript_response.segments), 0) |
| 113 | + |
| 114 | + # Verify the transcription contains the expected text |
| 115 | + expected_text = "This is the micro machine man presenting the most midget miniature" |
| 116 | + self.assertIn( |
| 117 | + expected_text.lower(), |
| 118 | + transcript_response.text.lower(), |
| 119 | + f"Expected text '{expected_text}' not found in transcription: '{transcript_response.text}'" |
| 120 | + ) |
| 121 | + |
| 122 | + # If we got segments, verify they have the expected structure |
| 123 | + if len(transcript_response.segments) > 0: |
| 124 | + segment = transcript_response.segments[0] |
| 125 | + self.assertIsNotNone(segment.text) |
| 126 | + self.assertIsInstance(segment.id, int) |
| 127 | + else: |
| 128 | + # Even if no segments, we should have text |
| 129 | + self.assertIsNotNone(transcript_response.text) |
| 130 | + self.assertGreater(len(transcript_response.text), 0) |
| 131 | + except Exception as err: |
| 132 | + print(err) |
| 133 | + self.fail("AudioTranscription service failed") |
| 134 | + finally: |
| 135 | + self.tearDown() |
| 136 | + # Clean up the temporary directory |
| 137 | + if os.path.exists(temp_dir): |
| 138 | + shutil.rmtree(temp_dir) |
| 139 | + |
0 commit comments