owiedotch commited on
Commit
544ae95
·
verified ·
1 Parent(s): 85dc4b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -8,6 +8,8 @@ import spaces
8
  import torch
9
  import tempfile
10
  import io
 
 
11
 
12
  # Initialize the model
13
  def load_model():
@@ -17,7 +19,7 @@ semanticodec = load_model()
17
 
18
  @spaces.GPU(duration=20)
19
  def encode_audio(audio_path):
20
- """Encode audio file to tokens and return them as a binary buffer"""
21
  tokens = semanticodec.encode(audio_path)
22
  # Move tokens to CPU before converting to numpy
23
  if isinstance(tokens, torch.Tensor):
@@ -31,19 +33,27 @@ def encode_audio(audio_path):
31
  # Verify the buffer has content
32
  if buffer.getbuffer().nbytes == 0:
33
  raise Exception("Failed to create token buffer")
34
-
35
- return buffer, f"Encoded to {len(tokens)} tokens"
 
 
 
 
 
 
 
 
 
36
 
37
  @spaces.GPU(duration=60)
38
- def decode_tokens(token_buffer):
39
  """Decode tokens to audio"""
40
- # Ensure the buffer has content
41
- if not token_buffer or token_buffer.getbuffer().nbytes == 0:
42
- return None, "Error: Empty token buffer"
43
 
44
- # Reset buffer position to start
45
- token_buffer.seek(0)
46
- tokens = np.load(token_buffer)
47
 
48
  # Convert to torch tensor if needed by the model
49
  if hasattr(semanticodec, 'decode_requires_tensor') and semanticodec.decode_requires_tensor:
 
8
  import torch
9
  import tempfile
10
  import io
11
+ import uuid
12
+ from pathlib import Path
13
 
14
  # Initialize the model
15
  def load_model():
 
19
 
20
  @spaces.GPU(duration=20)
21
  def encode_audio(audio_path):
22
+ """Encode audio file to tokens and return them as a file"""
23
  tokens = semanticodec.encode(audio_path)
24
  # Move tokens to CPU before converting to numpy
25
  if isinstance(tokens, torch.Tensor):
 
33
  # Verify the buffer has content
34
  if buffer.getbuffer().nbytes == 0:
35
  raise Exception("Failed to create token buffer")
36
+
37
+ # Create a temporary file in /tmp which is writable in Spaces
38
+ temp_dir = "/tmp"
39
+ os.makedirs(temp_dir, exist_ok=True)
40
+ temp_file_path = os.path.join(temp_dir, f"tokens_{uuid.uuid4()}.oterin")
41
+
42
+ # Write buffer to the temporary file
43
+ with open(temp_file_path, "wb") as f:
44
+ f.write(buffer.getvalue())
45
+
46
+ return temp_file_path, f"Encoded to {len(tokens)} tokens"
47
 
48
  @spaces.GPU(duration=60)
49
+ def decode_tokens(token_file):
50
  """Decode tokens to audio"""
51
+ # Ensure the file exists and has content
52
+ if not token_file or not os.path.exists(token_file):
53
+ return None, "Error: Empty or missing token file"
54
 
55
+ # Load tokens from file
56
+ tokens = np.load(token_file)
 
57
 
58
  # Convert to torch tensor if needed by the model
59
  if hasattr(semanticodec, 'decode_requires_tensor') and semanticodec.decode_requires_tensor: