anke01 commited on
Commit
17cd4f9
·
verified ·
1 Parent(s): 0eabd55

Upload ./infer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. infer.py +121 -0
infer.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+ """
4
+ Qwen3-TTS-12Hz Inference Script (Official Format)
5
+
6
+ Usage:
7
+ # Single inference
8
+ python infer.py \
9
+ --model_path ./output_uyghur/checkpoint-final \
10
+ --text "بۇ بىر سىناق ئاۋازى." \
11
+ --speaker uyghur_speaker \
12
+ --output test.wav
13
+
14
+ # Quick test
15
+ python infer.py \
16
+ --model_path ./output_uyghur/checkpoint-final \
17
+ --speaker uyghur_speaker \
18
+ --quick_test
19
+ """
20
+
21
+ import argparse
22
+ import os
23
+ import sys
24
+ import torch
25
+ import soundfile as sf
26
+
27
+ try:
28
+ from qwen_tts import Qwen3TTSModel
29
+ except ImportError:
30
+ print("Error: qwen_tts not installed. Please run: pip install qwen-tts")
31
+ sys.exit(1)
32
+
33
+
34
+ def main():
35
+ parser = argparse.ArgumentParser(description='Qwen3-TTS-12Hz Inference')
36
+ parser.add_argument('--model_path', type=str, required=True,
37
+ help='Path to fine-tuned model checkpoint')
38
+ parser.add_argument('--text', type=str, default=None,
39
+ help='Text to synthesize')
40
+ parser.add_argument('--speaker', type=str, default='uyghur_speaker',
41
+ help='Speaker name (must match training --speaker_name)')
42
+ parser.add_argument('--output', type=str, default='output.wav',
43
+ help='Output WAV file path')
44
+ parser.add_argument('--device', type=str, default='cuda:0',
45
+ help='Device (cuda:0, cpu)')
46
+ parser.add_argument('--quick_test', action='store_true',
47
+ help='Run quick test with sample texts')
48
+ args = parser.parse_args()
49
+
50
+ # Check device
51
+ if args.device.startswith('cuda') and not torch.cuda.is_available():
52
+ print("CUDA not available, falling back to CPU")
53
+ args.device = 'cpu'
54
+
55
+ print(f"Loading model from: {args.model_path}")
56
+ print(f"Device: {args.device}")
57
+
58
+ # Load model
59
+ try:
60
+ tts = Qwen3TTSModel.from_pretrained(
61
+ args.model_path,
62
+ device_map=args.device,
63
+ dtype=torch.bfloat16,
64
+ )
65
+ except Exception as e:
66
+ print(f"Error loading model: {e}")
67
+ sys.exit(1)
68
+
69
+ print("Model loaded successfully")
70
+
71
+ # Quick test mode
72
+ if args.quick_test:
73
+ test_texts = [
74
+ "بۇ بىر سىناق ئاۋازى.",
75
+ "ياخشىمۇسىز، بۈگۈن ھاۋا ناھايىتى ياخشى.",
76
+ "ئىككىنچى سىناق.",
77
+ ]
78
+
79
+ for i, text in enumerate(test_texts):
80
+ output_path = f"test_{i}.wav"
81
+ print(f"\n[Test {i+1}] {text}")
82
+
83
+ try:
84
+ wavs, sr = tts.generate_custom_voice(
85
+ text=text,
86
+ speaker=args.speaker,
87
+ language='Auto',
88
+ )
89
+ sf.write(output_path, wavs[0], sr)
90
+ print(f"Saved to: {output_path}")
91
+ except Exception as e:
92
+ print(f"Error: {e}")
93
+
94
+ return
95
+
96
+ # Single inference
97
+ if args.text is None:
98
+ print("Error: Please specify --text or use --quick_test")
99
+ sys.exit(1)
100
+
101
+ print(f"\nGenerating speech for: {args.text}")
102
+ print(f"Speaker: {args.speaker}")
103
+
104
+ try:
105
+ wavs, sr = tts.generate_custom_voice(
106
+ text=args.text,
107
+ speaker=args.speaker,
108
+ language='Auto',
109
+ )
110
+ sf.write(args.output, wavs[0], sr)
111
+ print(f"\nSaved to: {args.output}")
112
+ print(f"Sample rate: {sr} Hz, Duration: {len(wavs[0])/sr:.2f}s")
113
+ except Exception as e:
114
+ print(f"Error: {e}")
115
+ sys.exit(1)
116
+
117
+ print("\nInference completed!")
118
+
119
+
120
+ if __name__ == "__main__":
121
+ main()