MihaiPopa-1 commited on
Commit
315379b
·
verified ·
1 Parent(s): 38b610c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -91,20 +91,34 @@ def encode_decode_focal(audio_input):
91
  with torch.no_grad():
92
  toks = codec.sig_to_toks(sig)
93
  rec_sig = codec.toks_to_sig(toks)
94
-
95
- # Get binary codes for true compression
96
  codes = codec.toks_to_codes(toks)
97
 
98
- # --- Save as truly compressed binary file ---
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  temp_dir = tempfile.mkdtemp()
100
  fc_file_path = os.path.join(temp_dir, "compressed_tokens.fc")
101
 
102
- # Convert codes to binary and pack
103
- codes_cpu = codes.cpu().numpy().astype(np.uint8)
104
- packed_bits = np.packbits(codes_cpu.flatten())
105
 
106
  with open(fc_file_path, 'wb') as f:
107
- f.write(packed_bits.tobytes())
 
 
 
 
108
 
109
  # Calculate stats
110
  file_size_bytes = os.path.getsize(fc_file_path)
@@ -113,6 +127,7 @@ def encode_decode_focal(audio_input):
113
  actual_bitrate = (file_size_bytes * 8) / duration_sec
114
 
115
  print(f"Duration: {duration_sec:.2f}s")
 
116
  print(f"File size: {file_size_bytes} bytes (expected: ~{expected_size:.0f} bytes)")
117
  print(f"Actual bitrate: {actual_bitrate:.0f} bps")
118
 
@@ -122,7 +137,7 @@ def encode_decode_focal(audio_input):
122
  if len(decoded_wav_output.shape) == 0:
123
  decoded_wav_output = decoded_wav_output.reshape(1)
124
 
125
- status_msg = f"✅ Duration: {duration_sec:.1f}s | File: {file_size_bytes} bytes | Bitrate: {actual_bitrate:.0f} bps"
126
 
127
  return (codec.sample_rate_output, decoded_wav_output), fc_file_path, status_msg
128
 
 
91
  with torch.no_grad():
92
  toks = codec.sig_to_toks(sig)
93
  rec_sig = codec.toks_to_sig(toks)
 
 
94
  codes = codec.toks_to_codes(toks)
95
 
96
+ # --- DEBUG: Print what we actually have ---
97
+ print(f"\n=== DEBUG INFO ===")
98
+ print(f"Tokens shape: {toks.shape}")
99
+ print(f"Tokens dtype: {toks.dtype}")
100
+ print(f"Tokens min/max: {toks.min().item()}/{toks.max().item()}")
101
+ print(f"First 10 tokens: {toks[0, :10].cpu().numpy()}")
102
+
103
+ print(f"\nCodes shape: {codes.shape}")
104
+ print(f"Codes dtype: {codes.dtype}")
105
+ print(f"Codes min/max: {codes.min().item()}/{codes.max().item()}")
106
+ print(f"First 10 codes (flattened): {codes.flatten()[:10].cpu().numpy()}")
107
+ print(f"=================\n")
108
+
109
+ # --- Save the compressed data ---
110
  temp_dir = tempfile.mkdtemp()
111
  fc_file_path = os.path.join(temp_dir, "compressed_tokens.fc")
112
 
113
+ # Try saving tokens directly as integers (more reliable)
114
+ toks_cpu = toks.cpu().numpy().astype(np.int32)
 
115
 
116
  with open(fc_file_path, 'wb') as f:
117
+ # Write header: number of tokens
118
+ num_tokens = toks_cpu.size
119
+ f.write(num_tokens.to_bytes(4, byteorder='little'))
120
+ # Write tokens as raw bytes
121
+ f.write(toks_cpu.tobytes())
122
 
123
  # Calculate stats
124
  file_size_bytes = os.path.getsize(fc_file_path)
 
127
  actual_bitrate = (file_size_bytes * 8) / duration_sec
128
 
129
  print(f"Duration: {duration_sec:.2f}s")
130
+ print(f"Num tokens: {num_tokens}")
131
  print(f"File size: {file_size_bytes} bytes (expected: ~{expected_size:.0f} bytes)")
132
  print(f"Actual bitrate: {actual_bitrate:.0f} bps")
133
 
 
137
  if len(decoded_wav_output.shape) == 0:
138
  decoded_wav_output = decoded_wav_output.reshape(1)
139
 
140
+ status_msg = f"✅ Duration: {duration_sec:.1f}s | Tokens: {num_tokens} | File: {file_size_bytes} bytes | Bitrate: {actual_bitrate:.0f} bps"
141
 
142
  return (codec.sample_rate_output, decoded_wav_output), fc_file_path, status_msg
143