Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Test script for Madverse Music API | |
""" | |
import requests | |
import json | |
import time | |
import os | |
# API endpoint | |
API_URL = "http://localhost:8000" | |
# API Key (same as in api.py) | |
API_KEY = os.getenv("MADVERSE_API_KEY", "madverse-music-api-key-2024") | |
# Headers for authenticated requests | |
HEADERS = { | |
"X-API-Key": API_KEY, | |
"Content-Type": "application/json" | |
} | |
def test_health(): | |
"""Test health check endpoint""" | |
print("π Testing health check...") | |
response = requests.get(f"{API_URL}/health") | |
print(f"Status: {response.status_code}") | |
print(f"Response: {response.json()}") | |
print() | |
def test_info(): | |
"""Test info endpoint""" | |
print("π Testing info endpoint...") | |
response = requests.get(f"{API_URL}/info") | |
print(f"Status: {response.status_code}") | |
print(f"Response: {json.dumps(response.json(), indent=2)}") | |
print() | |
def test_analyze_single(audio_url): | |
"""Test music analysis endpoint with single URL""" | |
print(f"π΅ Testing single file analysis with URL: {audio_url}") | |
payload = { | |
"urls": [audio_url] | |
} | |
start_time = time.time() | |
response = requests.post(f"{API_URL}/analyze", json=payload, headers=HEADERS) | |
end_time = time.time() | |
print(f"Status: {response.status_code}") | |
print(f"Request time: {end_time - start_time:.2f} seconds") | |
if response.status_code == 200: | |
result = response.json() | |
print("β Analysis successful!") | |
print(f"Total files: {result['total_files']}") | |
print(f"Successful: {result['successful_analyses']}") | |
print(f"Message: {result['message']}") | |
if result['results']: | |
file_result = result['results'][0] | |
if file_result['success']: | |
print(f"Classification: {file_result['classification']}") | |
print(f"Confidence: {file_result['confidence']:.2%}") | |
print(f"Duration: {file_result['duration']:.1f} seconds") | |
print(f"Processing time: {file_result['processing_time']:.2f} seconds") | |
else: | |
print("β Analysis failed!") | |
print(f"Error: {response.json()}") | |
print() | |
def test_analyze_multiple(audio_urls): | |
"""Test music analysis endpoint with multiple URLs""" | |
print(f"π΅ Testing multiple files analysis with {len(audio_urls)} URLs...") | |
payload = { | |
"urls": audio_urls | |
} | |
start_time = time.time() | |
response = requests.post(f"{API_URL}/analyze", json=payload, headers=HEADERS) | |
end_time = time.time() | |
print(f"Status: {response.status_code}") | |
print(f"Request time: {end_time - start_time:.2f} seconds") | |
if response.status_code == 200: | |
result = response.json() | |
print("β Multiple files analysis successful!") | |
print(f"Total files: {result['total_files']}") | |
print(f"Successful: {result['successful_analyses']}") | |
print(f"Failed: {result['failed_analyses']}") | |
print(f"Message: {result['message']}") | |
print(f"Total processing time: {result['total_processing_time']:.2f} seconds") | |
print("\nπ Individual Results:") | |
for i, file_result in enumerate(result['results'][:3]): # Show first 3 results | |
status = "β " if file_result['success'] else "β" | |
print(f" {i+1}. {status} {file_result['url'][:50]}...") | |
if file_result['success']: | |
print(f" Classification: {file_result['classification']}") | |
print(f" Confidence: {file_result['confidence']:.2%}") | |
else: | |
print(f" Error: {file_result['message']}") | |
if len(result['results']) > 3: | |
print(f" ... and {len(result['results']) - 3} more results") | |
else: | |
print("β Multiple files analysis failed!") | |
print(f"Error: {response.json()}") | |
print() | |
def test_unauthorized_access(): | |
"""Test unauthorized access (without API key)""" | |
print("π Testing unauthorized access...") | |
payload = { | |
"urls": ["https://example.com/test.mp3"] | |
} | |
# Make request without API key | |
response = requests.post(f"{API_URL}/analyze", json=payload) | |
print(f"Status: {response.status_code}") | |
if response.status_code == 401: | |
print("β Unauthorized access properly blocked!") | |
print(f"Error: {response.json()}") | |
else: | |
print("β Unauthorized access not blocked!") | |
print(f"Response: {response.json()}") | |
print() | |
def main(): | |
"""Main test function""" | |
print("π Madverse Music API Test") | |
print("=" * 40) | |
# Test basic endpoints | |
test_health() | |
test_info() | |
# Test authentication | |
test_unauthorized_access() | |
# Test with a sample audio URL (you can replace with your own) | |
# This is just an example - replace with actual audio URLs | |
sample_urls = [ | |
"https://example.com/sample.mp3", # Replace with real URLs | |
"https://example.com/sample2.wav" | |
] | |
print("π To test with real audio:") | |
print("1. Replace sample URLs in this script with real audio URLs") | |
print("2. Or use curl for single file:") | |
print(f' curl -X POST "{API_URL}/analyze" \\') | |
print(f' -H "X-API-Key: {API_KEY}" \\') | |
print(f' -H "Content-Type: application/json" \\') | |
print(f' -d \'{{"urls": ["YOUR_AUDIO_URL"]}}\'') | |
print() | |
print("3. Or use curl for multiple files:") | |
print(f' curl -X POST "{API_URL}/analyze" \\') | |
print(f' -H "X-API-Key: {API_KEY}" \\') | |
print(f' -H "Content-Type: application/json" \\') | |
print(f' -d \'{{"urls": ["URL1", "URL2", "URL3"]}}\'') | |
print() | |
# Uncomment below to test with real URLs | |
# for url in sample_urls: | |
# try: | |
# test_analyze_single(url) | |
# except Exception as e: | |
# print(f"Error testing {url}: {e}") | |
# Uncomment below to test multiple files analysis | |
# try: | |
# test_analyze_multiple(sample_urls) | |
# except Exception as e: | |
# print(f"Error testing multiple files: {e}") | |
if __name__ == "__main__": | |
main() |