Spaces:
Running
Running
import requests | |
import os | |
def test_c3po_voice(): | |
"""Test the C3PO voice without uploading any files""" | |
# API endpoint for C3PO voice only | |
url = "http://localhost:7860/tts-c3po" | |
# Text to convert to speech | |
text = "Hello there! I am C-3PO, human-cyborg relations. How may I assist you today?" | |
# Prepare the request data | |
data = { | |
"text": text, | |
"language": "en", | |
"no_lang_auto_detect": False | |
} | |
try: | |
print("Testing C3PO voice...") | |
print(f"Text: {text}") | |
response = requests.post(url, data=data) | |
if response.status_code == 200: | |
# Save the generated audio | |
output_filename = "c3po_voice_sample.wav" | |
with open(output_filename, "wb") as f: | |
f.write(response.content) | |
print(f"Success! C3PO voice sample saved as {output_filename}") | |
else: | |
print(f"Error: {response.status_code}") | |
print(response.text) | |
except requests.exceptions.ConnectionError: | |
print("Error: Could not connect to the API. Make sure the server is running on http://localhost:7860") | |
except Exception as e: | |
print(f"Error: {e}") | |
def test_xtts_with_custom_voice(): | |
"""Example of using XTTS with custom voice upload""" | |
# API endpoint | |
url = "http://localhost:7860/tts" | |
# Text to convert to speech | |
text = "This is a test of XTTS voice cloning with a custom reference voice." | |
# Path to your speaker reference audio file | |
speaker_file_path = "reference.wav" # Update this path to your reference audio | |
# Check if speaker file exists | |
if not os.path.exists(speaker_file_path): | |
print(f"Custom voice test skipped: Speaker file not found at {speaker_file_path}") | |
print("To test custom voice cloning:") | |
print("1. Record 3-10 seconds of clear speech") | |
print("2. Save as 'reference.wav' in this directory") | |
print("3. Run this test again") | |
return | |
# Prepare the request data | |
data = { | |
"text": text, | |
"language": "en", | |
"voice_cleanup": False, | |
"no_lang_auto_detect": False | |
} | |
files = { | |
"speaker_file": open(speaker_file_path, "rb") | |
} | |
try: | |
print("Testing XTTS with custom voice...") | |
print(f"Text: {text}") | |
print(f"Speaker file: {speaker_file_path}") | |
response = requests.post(url, data=data, files=files) | |
if response.status_code == 200: | |
# Save the generated audio | |
output_filename = "custom_voice_clone.wav" | |
with open(output_filename, "wb") as f: | |
f.write(response.content) | |
print(f"Success! Custom voice clone saved as {output_filename}") | |
else: | |
print(f"Error: {response.status_code}") | |
print(response.text) | |
except requests.exceptions.ConnectionError: | |
print("Error: Could not connect to the API. Make sure the server is running on http://localhost:7860") | |
except Exception as e: | |
print(f"Error: {e}") | |
finally: | |
files["speaker_file"].close() | |
def test_xtts_fallback_to_c3po(): | |
"""Test XTTS endpoint without speaker file (should use C3PO voice)""" | |
# API endpoint | |
url = "http://localhost:7860/tts" | |
# Text to convert to speech | |
text = "When no custom voice is provided, I will speak in the C3PO voice by default." | |
# Prepare the request data (no speaker file) | |
data = { | |
"text": text, | |
"language": "en", | |
"voice_cleanup": False, | |
"no_lang_auto_detect": False | |
} | |
try: | |
print("Testing XTTS fallback to C3PO voice...") | |
print(f"Text: {text}") | |
response = requests.post(url, data=data) | |
if response.status_code == 200: | |
# Save the generated audio | |
output_filename = "xtts_c3po_fallback.wav" | |
with open(output_filename, "wb") as f: | |
f.write(response.content) | |
print(f"Success! XTTS with C3PO fallback saved as {output_filename}") | |
else: | |
print(f"Error: {response.status_code}") | |
print(response.text) | |
except requests.exceptions.ConnectionError: | |
print("Error: Could not connect to the API. Make sure the server is running on http://localhost:7860") | |
except Exception as e: | |
print(f"Error: {e}") | |
def test_multilingual_c3po(): | |
"""Test C3PO voice in different languages""" | |
# API endpoint for C3PO voice only | |
url = "http://localhost:7860/tts-c3po" | |
# Test different languages | |
test_cases = [ | |
("en", "Hello, I am C-3PO. I am fluent in over six million forms of communication."), | |
("es", "Hola, soy C-3PO. Domino más de seis millones de formas de comunicación."), | |
("fr", "Bonjour, je suis C-3PO. Je maîtrise plus de six millions de formes de communication."), | |
("de", "Hallo, ich bin C-3PO. Ich beherrsche über sechs Millionen Kommunikationsformen."), | |
] | |
for language, text in test_cases: | |
data = { | |
"text": text, | |
"language": language, | |
"no_lang_auto_detect": True # Force the specified language | |
} | |
try: | |
print(f"Testing C3PO voice in {language.upper()}...") | |
print(f"Text: {text}") | |
response = requests.post(url, data=data) | |
if response.status_code == 200: | |
# Save the generated audio | |
output_filename = f"c3po_voice_{language}.wav" | |
with open(output_filename, "wb") as f: | |
f.write(response.content) | |
print(f"Success! C3PO {language} voice saved as {output_filename}") | |
else: | |
print(f"Error: {response.status_code}") | |
print(response.text) | |
except requests.exceptions.ConnectionError: | |
print("Error: Could not connect to the API. Make sure the server is running on http://localhost:7860") | |
except Exception as e: | |
print(f"Error: {e}") | |
print() # Add spacing between tests | |
def get_supported_languages(): | |
"""Get list of supported languages""" | |
try: | |
response = requests.get("http://localhost:7860/languages") | |
if response.status_code == 200: | |
languages = response.json() | |
print("Supported languages:", languages["languages"]) | |
return languages["languages"] | |
else: | |
print("Failed to get languages:", response.status_code) | |
return [] | |
except requests.exceptions.ConnectionError: | |
print("API is not running. Start it with: uvicorn app:app --host 0.0.0.0 --port 7860") | |
return [] | |
def check_api_health(): | |
"""Check if the API is running""" | |
try: | |
response = requests.get("http://localhost:7860/health") | |
if response.status_code == 200: | |
health_info = response.json() | |
print("API Health Check:") | |
print(f" Status: {health_info['status']}") | |
print(f" Device: {health_info['device']}") | |
print(f" Model: {health_info['model']}") | |
print(f" Default Voice: {health_info['default_voice']}") | |
print(f" Languages: {len(health_info['supported_languages'])} supported") | |
return True | |
else: | |
print("API health check failed:", response.status_code) | |
return False | |
except requests.exceptions.ConnectionError: | |
print("API is not running. Start it with: uvicorn app:app --host 0.0.0.0 --port 7860") | |
return False | |
def create_sample_reference(): | |
"""Instructions for creating a reference audio file""" | |
print("\n" + "="*50) | |
print("REFERENCE AUDIO SETUP") | |
print("="*50) | |
print("To use XTTS voice cloning, you need a reference audio file:") | |
print("1. Record 3-10 seconds of clear speech") | |
print("2. Save as WAV format (recommended)") | |
print("3. Ensure good audio quality (no background noise)") | |
print("4. Place the file in the same directory as this script") | |
print("5. Update the 'speaker_file_path' variable in the functions above") | |
print("\nExample recording text:") | |
print("'Hello, this is my voice. I'm recording this sample for voice cloning.'") | |
print("="*50) | |
if __name__ == "__main__": | |
print("XTTS C3PO API Client Example") | |
print("=" * 40) | |
# First check if API is running | |
if check_api_health(): | |
print() | |
# Get supported languages | |
languages = get_supported_languages() | |
print() | |
# Test C3PO voice (no file upload needed) | |
print("1. Testing C3PO voice (no upload required)...") | |
test_c3po_voice() | |
print() | |
# Test XTTS fallback to C3PO | |
print("2. Testing XTTS endpoint without speaker file (C3PO fallback)...") | |
test_xtts_fallback_to_c3po() | |
print() | |
# Test custom voice if reference file exists | |
print("3. Testing custom voice cloning...") | |
test_xtts_with_custom_voice() | |
print() | |
# Test multilingual C3PO | |
print("4. Testing multilingual C3PO voice...") | |
test_multilingual_c3po() | |
print("All tests completed!") | |
print("\nGenerated files:") | |
for file in os.listdir("."): | |
if file.endswith(".wav") and ("c3po" in file or "custom" in file or "xtts" in file): | |
print(f" - {file}") | |
else: | |
print("\nPlease start the API server first:") | |
print("uvicorn app:app --host 0.0.0.0 --port 7860") |