Spaces:
Running
Running
File size: 6,364 Bytes
f280f9f |
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 |
#!/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() |