Prasanna05 commited on
Commit
c3c1782
·
verified ·
1 Parent(s): f38c279

Add Usage examples

Browse files
Files changed (1) hide show
  1. example.py +75 -0
example.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Example usage of the ONNX NanoCodec decoder
3
+ """
4
+
5
+ import numpy as np
6
+ import onnxruntime as ort
7
+
8
+ def example_basic_inference():
9
+ """Basic ONNX inference example"""
10
+ print("Loading ONNX model...")
11
+
12
+ session = ort.InferenceSession(
13
+ "nano_codec_decoder.onnx",
14
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"]
15
+ )
16
+
17
+ print(f"Providers: {session.get_providers()}")
18
+
19
+ # Create sample input
20
+ num_frames = 10
21
+ tokens = np.random.randint(0, 500, (1, 4, num_frames), dtype=np.int64)
22
+ tokens_len = np.array([num_frames], dtype=np.int64)
23
+
24
+ print(f"\nInput tokens: {tokens.shape}")
25
+
26
+ # Run inference
27
+ outputs = session.run(
28
+ None,
29
+ {"tokens": tokens, "tokens_len": tokens_len}
30
+ )
31
+
32
+ audio, audio_len = outputs
33
+ print(f"Output audio: {audio.shape}")
34
+ print(f"Audio duration: {audio.shape[1] / 22050:.2f} seconds")
35
+
36
+ return audio
37
+
38
+
39
+ def example_with_decoder_class():
40
+ """Example using the decoder class"""
41
+ from onnx_decoder_optimized import ONNXKaniTTSDecoderOptimized
42
+
43
+ print("Initializing decoder...")
44
+ decoder = ONNXKaniTTSDecoderOptimized(
45
+ onnx_model_path="nano_codec_decoder.onnx",
46
+ device="cuda"
47
+ )
48
+
49
+ # Decode multiple frames
50
+ print("\nDecoding frames...")
51
+ for i in range(5):
52
+ codes = [np.random.randint(0, 500) for _ in range(4)]
53
+ audio = decoder.decode_frame(codes)
54
+ print(f" Frame {i+1}: {audio.shape} samples")
55
+
56
+ decoder.reset_history()
57
+ print("✓ Decoding complete")
58
+
59
+
60
+ if __name__ == "__main__":
61
+ print("="*60)
62
+ print("ONNX NanoCodec Decoder Examples")
63
+ print("="*60)
64
+
65
+ # Example 1
66
+ print("\n[1/2] Basic inference...")
67
+ example_basic_inference()
68
+
69
+ # Example 2
70
+ print("\n[2/2] Using decoder class...")
71
+ example_with_decoder_class()
72
+
73
+ print("\n" + "="*60)
74
+ print("Examples complete!")
75
+ print("="*60)