File size: 2,579 Bytes
b65e164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#\!/usr/bin/env python3
import os
import sys
import time

# Configurar ambiente
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'

print("=== CosyVoice English TTS Test with Timing ===")
print()

start_time = time.time()

try:
    from cosyvoice.cli.cosyvoice import CosyVoice
    import torchaudio
    
    model_path = 'pretrained_models/CosyVoice-300M-direct'
    
    # Verificar se o modelo existe
    if not os.path.exists(model_path):
        print(f"❌ Error: Model not found at {model_path}")
        sys.exit(1)
    
    # Medir tempo de carregamento do modelo
    load_start = time.time()
    print("Loading CosyVoice model...")
    cosyvoice = CosyVoice(model_path, load_jit=False, load_trt=False, fp16=False)
    load_time = time.time() - load_start
    print(f"✅ Model loaded in {load_time:.2f} seconds")
    print()
    
    # Texto em inglês para síntese
    text = "Hello\! This is a test of the CosyVoice text-to-speech system. The synthesis is working perfectly and generating high quality audio."
    prompt_text = "Welcome to the speech synthesis demonstration."
    
    print(f"Text: {text}")
    print(f"Prompt: {prompt_text}")
    print()
    
    # Medir tempo de geração
    gen_start = time.time()
    print("Generating audio...")
    
    output_file = "english_test_output.wav"
    for i, j in enumerate(cosyvoice.inference_zero_shot(text, prompt_text, None, stream=False)):
        torchaudio.save(output_file, j['tts_speech'], cosyvoice.sample_rate)
        break
    
    gen_time = time.time() - gen_start
    print(f"✅ Audio generated in {gen_time:.2f} seconds")
    
    # Verificar arquivo gerado
    if os.path.exists(output_file):
        size = os.path.getsize(output_file)
        duration = j['tts_speech'].shape[1] / cosyvoice.sample_rate
        print()
        print(f"📊 File statistics:")
        print(f"   - Filename: {output_file}")
        print(f"   - Size: {size/1024:.1f} KB")
        print(f"   - Duration: {duration:.2f} seconds")
        print(f"   - Sample rate: {cosyvoice.sample_rate} Hz")
    
    total_time = time.time() - start_time
    print()
    print(f"⏱️  Total execution time: {total_time:.2f} seconds")
    print(f"   - Model loading: {load_time:.2f}s ({load_time/total_time*100:.1f}%)")
    print(f"   - Audio generation: {gen_time:.2f}s ({gen_time/total_time*100:.1f}%)")
    
except Exception as e:
    print(f"❌ Error: {e}")
    import traceback
    traceback.print_exc()
    
    total_time = time.time() - start_time
    print(f"\nTotal time before error: {total_time:.2f} seconds")