File size: 12,411 Bytes
2ce4373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Test file for Multi-Utility Server endpoints.
Test both Subtitles and Embeddings APIs with the deployed server.

Usage:
    python test_endpoints.py

Modify the test data in the TEST_DATA section to test different inputs.
"""

import json
import os
from pathlib import Path
from typing import Any, Dict, List

import requests

# ============================================================================
# CONFIGURATION
# ============================================================================

BASE_URL = "https://abhisheksan-multiutility-server.hf.space"
API_KEY = "zL6Rod5869J0"  # Replace with your actual API key

# ============================================================================
# TEST DATA - MODIFY THESE TO TEST DIFFERENT INPUTS
# ============================================================================

# Test data for Subtitles API
SUBTITLE_TEST_DATA = {
    "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
    "lang": "en",
}

# Test data for Embeddings API
EMBEDDING_TEST_DATA = {
    "texts": [
        "Hello, how are you?",
        "Machine learning is fascinating",
        "Python is a great programming language",
    ],
    "normalize": True,
}

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================


def print_section(title: str):
    """Print a formatted section header."""
    print("\n" + "=" * 80)
    print(f"  {title}")
    print("=" * 80 + "\n")


def print_response(response: requests.Response):
    """Print formatted response details."""
    print(f"Status Code: {response.status_code}")
    print(f"Response Time: {response.elapsed.total_seconds():.2f}s")
    print("\nResponse Headers:")
    for key, value in response.headers.items():
        print(f"  {key}: {value}")
    print("\nResponse Body:")
    try:
        print(json.dumps(response.json(), indent=2))
    except Exception:
        print(response.text)


def make_request(
    method: str,
    endpoint: str,
    data: Dict[str, Any] = None,
    headers: Dict[str, str] = None,
) -> requests.Response:
    """Make HTTP request to the API."""
    url = f"{BASE_URL}{endpoint}"
    default_headers = {"Content-Type": "application/json", "X-API-Key": API_KEY}

    if headers:
        default_headers.update(headers)

    try:
        if method.upper() == "GET":
            response = requests.get(url, headers=default_headers)
        elif method.upper() == "POST":
            response = requests.post(url, json=data, headers=default_headers)
        else:
            raise ValueError(f"Unsupported HTTP method: {method}")

        return response
    except requests.exceptions.RequestException as e:
        print(f"❌ Request failed: {str(e)}")
        raise


# ============================================================================
# TEST FUNCTIONS
# ============================================================================


def test_health_check():
    """Test the main health check endpoint."""
    print_section("TEST: Health Check")

    try:
        response = make_request("GET", "/health")
        print_response(response)

        if response.status_code == 200:
            print("\n✅ Health check passed!")
        else:
            print("\n❌ Health check failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")


def test_root_endpoint():
    """Test the root endpoint."""
    print_section("TEST: Root Endpoint")

    try:
        response = make_request("GET", "/")
        print_response(response)

        if response.status_code == 200:
            print("\n✅ Root endpoint test passed!")
        else:
            print("\n❌ Root endpoint test failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")


def test_subtitles_health():
    """Test the subtitles service health check."""
    print_section("TEST: Subtitles Health Check")

    try:
        response = make_request("GET", "/api/v1/subtitles/health")
        print_response(response)

        if response.status_code == 200:
            print("\n✅ Subtitles health check passed!")
        else:
            print("\n❌ Subtitles health check failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")


def test_audio_transcription():
    """Test the audio file transcription endpoint."""
    print_section("TEST: Audio File Transcription")

    # Create a small test audio file (silent WAV)
    # This is a minimal WAV file header for a 1-second silent audio
    print("Creating test audio file...")
    test_audio = b"RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\x00\x00\x00\x00"

    test_file_path = "test_audio.wav"

    try:
        # Write test audio file
        with open(test_file_path, "wb") as f:
            f.write(test_audio)

        print(f"Test file created: {test_file_path}")
        print("Note: Using minimal WAV file for testing\n")

        # Make request with file upload
        url = f"{BASE_URL}/api/v1/subtitles/transcribe"
        headers = {"X-API-Key": API_KEY}

        with open(test_file_path, "rb") as f:
            files = {"file": ("test_audio.wav", f, "audio/wav")}
            data = {"lang": "en"}

            response = requests.post(url, headers=headers, files=files, data=data)

        print_response(response)

        if response.status_code == 200:
            data = response.json()
            transcription_count = len(data.get("transcription", []))
            print(f"\n✅ Audio transcription passed!")
            print(f"   - Language: {data.get('language')}")
            print(f"   - File name: {data.get('file_name')}")
            print(f"   - Number of segments: {transcription_count}")

            # Print transcription segments
            if transcription_count > 0:
                print(f"\n   Transcription segments:")
                for i, line in enumerate(data.get("transcription", [])[:5]):
                    print(f"     {i + 1}. {line}")
            else:
                print("   (No transcription - test audio is silent)")
        else:
            print("\n❌ Audio transcription failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")
    finally:
        # Cleanup test file
        if os.path.exists(test_file_path):
            os.remove(test_file_path)
            print(f"\n   Cleaned up test file: {test_file_path}")


def test_subtitles_extract():
    """Test the subtitles extraction endpoint."""
    print_section("TEST: Extract Subtitles")

    print("Test Data:")
    print(json.dumps(SUBTITLE_TEST_DATA, indent=2))
    print()

    try:
        response = make_request(
            "POST", "/api/v1/subtitles/extract", data=SUBTITLE_TEST_DATA
        )
        print_response(response)

        if response.status_code == 200:
            data = response.json()
            subtitle_count = len(data.get("subtitles", []))
            print(f"\n✅ Subtitles extraction passed!")
            print(f"   - Video ID: {data.get('video_id')}")
            print(f"   - Language: {data.get('language')}")
            print(f"   - Number of subtitle lines: {subtitle_count}")

            # Print first few subtitle lines
            if subtitle_count > 0:
                print(f"\n   First 5 subtitle lines:")
                for i, line in enumerate(data.get("subtitles", [])[:5]):
                    print(f"     {i + 1}. {line}")
        else:
            print("\n❌ Subtitles extraction failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")


def test_embeddings_health():
    """Test the embeddings service health check."""
    print_section("TEST: Embeddings Health Check")

    try:
        response = make_request("GET", "/api/v1/embeddings/health")
        print_response(response)

        if response.status_code == 200:
            print("\n✅ Embeddings health check passed!")
        else:
            print("\n❌ Embeddings health check failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")


def test_embeddings_generate():
    """Test the embeddings generation endpoint."""
    print_section("TEST: Generate Embeddings")

    print("Test Data:")
    print(json.dumps(EMBEDDING_TEST_DATA, indent=2))
    print()

    try:
        response = make_request(
            "POST", "/api/v1/embeddings/generate", data=EMBEDDING_TEST_DATA
        )
        print_response(response)

        if response.status_code == 200:
            data = response.json()
            embeddings = data.get("embeddings", [])
            print(f"\n✅ Embeddings generation passed!")
            print(f"   - Model: {data.get('model')}")
            print(f"   - Dimensions: {data.get('dimensions')}")
            print(f"   - Number of embeddings: {len(embeddings)}")

            # Print shape of embeddings
            if embeddings:
                print(f"\n   Embedding shapes:")
                for i, emb in enumerate(embeddings):
                    print(f"     Text {i + 1}: {len(emb)} dimensions")
                    print(f"       First 5 values: {emb[:5]}")
        else:
            print("\n❌ Embeddings generation failed!")
    except Exception as e:
        print(f"\n❌ Test failed with error: {str(e)}")


def test_error_handling():
    """Test error handling with invalid data."""
    print_section("TEST: Error Handling")

    print("Testing with invalid subtitle URL...")
    invalid_subtitle_data = {"url": "https://invalid-url.com/video", "lang": "en"}

    try:
        response = make_request(
            "POST", "/api/v1/subtitles/extract", data=invalid_subtitle_data
        )
        print_response(response)

        if response.status_code >= 400:
            print("\n✅ Error handling works correctly!")
        else:
            print("\n⚠️  Expected error response but got success!")
    except Exception as e:
        print(f"\n✅ Error handling works: {str(e)}")

    print("\n" + "-" * 80 + "\n")
    print("Testing with empty embeddings text list...")
    invalid_embedding_data = {"texts": [], "normalize": True}

    try:
        response = make_request(
            "POST", "/api/v1/embeddings/generate", data=invalid_embedding_data
        )
        print_response(response)

        if response.status_code >= 400:
            print("\n✅ Error handling works correctly!")
        else:
            print("\n⚠️  Expected error response but got success!")
    except Exception as e:
        print(f"\n✅ Error handling works: {str(e)}")


# ============================================================================
# MAIN TEST RUNNER
# ============================================================================


def run_all_tests():
    """Run all test functions."""
    print("\n")
    print("+" + "=" * 78 + "+")
    print("|" + " " * 20 + "MULTI-UTILITY SERVER API TESTS" + " " * 28 + "|")
    print("+" + "=" * 78 + "+")
    print(f"\nBase URL: {BASE_URL}")
    print(
        f"API Key: {'*' * len(API_KEY) if API_KEY != 'your-api-key-here' else 'NOT SET'}"
    )

    if API_KEY == "your-api-key-here":
        print("\n⚠️  WARNING: Please set your API key in the API_KEY variable!")
        print("   Some tests may fail without a valid API key.\n")

    # Run all tests
    test_health_check()
    test_root_endpoint()
    test_subtitles_health()
    test_subtitles_extract()
    test_audio_transcription()
    test_embeddings_health()
    test_embeddings_generate()
    test_error_handling()

    # Summary
    print_section("TEST SUMMARY")
    print("All tests completed!")
    print(
        "\nTo modify test inputs, edit the TEST_DATA section at the top of this file."
    )
    print("\nAvailable endpoints:")
    print(f"  - GET  {BASE_URL}/health")
    print(f"  - GET  {BASE_URL}/")
    print(f"  - GET  {BASE_URL}/api/v1/subtitles/health")
    print(f"  - POST {BASE_URL}/api/v1/subtitles/extract (requires network access)")
    print(f"  - POST {BASE_URL}/api/v1/subtitles/transcribe (upload audio file)")
    print(f"  - GET  {BASE_URL}/api/v1/embeddings/health")
    print(f"  - POST {BASE_URL}/api/v1/embeddings/generate")
    print()


if __name__ == "__main__":
    run_all_tests()