Spaces:
Runtime error
Runtime error
some debug code
Browse files- chunks.mp3 +0 -0
- chunks.pkl +3 -0
- debug_000.py +74 -0
- debug_001.py +60 -0
chunks.mp3
ADDED
Binary file (117 kB). View file
|
|
chunks.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d85ef7ee28d01dab9ee6faa439f791a1647cc937e0c68e3e5a73d5bd2f071d7f
|
3 |
+
size 117337
|
debug_000.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
from elevenlabs import generate, play
|
4 |
+
from elevenlabs import set_api_key
|
5 |
+
from elevenlabs import generate, stream
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
account_sid = os.environ["ELEVENLABS_API_KEY"]
|
10 |
+
voice_id="2OviOUQc1JsQRQgNkVBj"
|
11 |
+
model_id="eleven_monolingual_v1"
|
12 |
+
set_api_key(account_sid)
|
13 |
+
|
14 |
+
def stream_tts(prompt):
|
15 |
+
audio_stream = generate(
|
16 |
+
text=prompt,
|
17 |
+
voice=voice_id,
|
18 |
+
model=model_id,
|
19 |
+
stream_chunk_size=2048,
|
20 |
+
stream=True,
|
21 |
+
)
|
22 |
+
return audio_stream
|
23 |
+
|
24 |
+
prompts=[
|
25 |
+
"erm",
|
26 |
+
"Cabbages, my dear friend!",
|
27 |
+
"Did you know that the world's largest cabbage weighed 62.71 kilograms?",
|
28 |
+
"Simply remarkable!",
|
29 |
+
"How are you today?",
|
30 |
+
]
|
31 |
+
|
32 |
+
mpv_command = ["mpv", "--no-cache", "--no-terminal", "--", "fd://0"]
|
33 |
+
mpv_process = subprocess.Popen(
|
34 |
+
mpv_command,
|
35 |
+
stdin=subprocess.PIPE,
|
36 |
+
stdout=subprocess.DEVNULL,
|
37 |
+
stderr=subprocess.DEVNULL,
|
38 |
+
)
|
39 |
+
|
40 |
+
load_chunks = False
|
41 |
+
load_chunks = os.path.exists("chunks.pkl")
|
42 |
+
|
43 |
+
# check if chunks.pkl exists
|
44 |
+
if load_chunks:
|
45 |
+
# try open chunks
|
46 |
+
with open("chunks.pkl", "rb") as f:
|
47 |
+
import pickle
|
48 |
+
chunks = pickle.load(f)
|
49 |
+
for chunk in chunks:
|
50 |
+
mpv_process.stdin.write(chunk)
|
51 |
+
mpv_process.stdin.flush()
|
52 |
+
|
53 |
+
else:
|
54 |
+
chunks = []
|
55 |
+
|
56 |
+
for prompt in prompts:
|
57 |
+
for chunk in stream_tts(prompt):
|
58 |
+
if chunk is not None:
|
59 |
+
chunks.append(chunk)
|
60 |
+
mpv_process.stdin.write(chunk) # type: ignore
|
61 |
+
mpv_process.stdin.flush() # type: ignore
|
62 |
+
|
63 |
+
# save chunks to file as a pickled list of bytes
|
64 |
+
with open("chunks.pkl", "wb") as f:
|
65 |
+
import pickle
|
66 |
+
pickle.dump(chunks, f)
|
67 |
+
with open("chunks.mp3", "wb") as f:
|
68 |
+
for chunk in chunks:
|
69 |
+
f.write(chunk)
|
70 |
+
|
71 |
+
|
72 |
+
if mpv_process.stdin:
|
73 |
+
mpv_process.stdin.close()
|
74 |
+
mpv_process.wait()
|
debug_001.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import os
|
3 |
+
import subprocess
|
4 |
+
import av
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
|
8 |
+
mpv_command = ["mpv", "--no-cache", "--no-terminal", "--", "fd://0"]
|
9 |
+
mpv_process = subprocess.Popen(
|
10 |
+
mpv_command,
|
11 |
+
stdin=subprocess.PIPE,
|
12 |
+
stdout=subprocess.DEVNULL,
|
13 |
+
stderr=subprocess.DEVNULL,
|
14 |
+
)
|
15 |
+
|
16 |
+
load_chunks = False
|
17 |
+
load_chunks = os.path.exists("chunks.pkl")
|
18 |
+
|
19 |
+
|
20 |
+
audio_frames = []
|
21 |
+
|
22 |
+
# try open chunks
|
23 |
+
with open("chunks.pkl", "rb") as f:
|
24 |
+
import pickle
|
25 |
+
chunks = pickle.load(f)
|
26 |
+
append = False
|
27 |
+
for chunk in chunks:
|
28 |
+
mpv_process.stdin.write(chunk)
|
29 |
+
mpv_process.stdin.flush()
|
30 |
+
# np_chunk = np.frombuffer(chunk, dtype=np.int16)
|
31 |
+
# aa = av.AudioFrame.from_ndarray(chunk)
|
32 |
+
try:
|
33 |
+
if append:
|
34 |
+
bytes_io.write(chunk)
|
35 |
+
append = False
|
36 |
+
bytes_io.seek(0)
|
37 |
+
else:
|
38 |
+
bytes_io = io.BytesIO(chunk)
|
39 |
+
container = av.open(bytes_io, 'r')
|
40 |
+
audio_stream = next(s for s in container.streams if s.type == 'audio')
|
41 |
+
for frame in container.decode(audio_stream):
|
42 |
+
# Convert the audio frame to a NumPy array
|
43 |
+
array = frame.to_ndarray()
|
44 |
+
|
45 |
+
# Now you can use av.AudioFrame.from_ndarray
|
46 |
+
audio_frame = av.AudioFrame.from_ndarray(array, format='flt', layout='mono')
|
47 |
+
audio_frame.sample_rate = 44100
|
48 |
+
|
49 |
+
audio_frames.append(audio_frame)
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
print (e)
|
53 |
+
append = True
|
54 |
+
bytes_io.seek(0, io.SEEK_END)
|
55 |
+
continue
|
56 |
+
|
57 |
+
|
58 |
+
if mpv_process.stdin:
|
59 |
+
mpv_process.stdin.close()
|
60 |
+
mpv_process.wait()
|