hf4uwho commited on
Commit
7135eb0
·
1 Parent(s): 5f68145

Add utility scripts: voice tools, chunking, batch generation, space restart

Browse files
README.md CHANGED
@@ -24,6 +24,36 @@ FastAPI server running [kyutai/pocket-tts](https://huggingface.co/kyutai/pocket-
24
  | `GET /voices` | List all available voices |
25
  | `GET /health` | Health check |
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ## Voices (78 total)
28
 
29
  ### Standard voices (from [Nymbo/Pocket-TTS](https://huggingface.co/spaces/Nymbo/Pocket-TTS))
 
24
  | `GET /voices` | List all available voices |
25
  | `GET /health` | Health check |
26
 
27
+ ## Scripts
28
+
29
+ Utility scripts for generating audio, available in `scripts/`:
30
+
31
+ | Script | Purpose |
32
+ |---|---|
33
+ | `voice.py` | One-shot TTS: `python3 voice.py "text" [voice] [format]` or `--file input.txt` |
34
+ | `voice.sh` | Shell wrapper for voice.py |
35
+ | `voice-from-file.py` | Read text from file and generate TTS |
36
+ | `voice-chunked.py` | Split long text and generate in sequence |
37
+ | `voice-long.sh` | Shell script for long text with ffmpeg concat |
38
+ | `chunk_giselle.py` | Split `giselle_60min.txt` into ~10K char chunks on paragraph boundaries |
39
+ | `batch_tts.py` | Full batch generator with auto-restart on failure and ffmpeg concat |
40
+ | `giselle_batch.sh` | Shell batch equivalent for Giselle story generation |
41
+ | `run_giselle_batch.sh` | Sequential batch runner with retry logic |
42
+ | `restart_space.py` | Restart the HF Space via API (requires token) |
43
+
44
+ **Usage:**
45
+ ```bash
46
+ # Quick one-shot
47
+ python3 scripts/voice.py "Hello world" af_alloy ogg
48
+
49
+ # From file
50
+ python3 scripts/voice.py --file story.txt scarlett_johansson ogg
51
+
52
+ # Chunk a long story and generate
53
+ python3 scripts/chunk_giselle.py
54
+ python3 scripts/run_giselle_batch.sh
55
+ ```
56
+
57
  ## Voices (78 total)
58
 
59
  ### Standard voices (from [Nymbo/Pocket-TTS](https://huggingface.co/spaces/Nymbo/Pocket-TTS))
scripts/batch_tts.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Batch TTS generator with auto-restart on failure."""
3
+ import subprocess, time, os, sys, glob
4
+
5
+ TOKEN = os.environ.get("HF_TOKEN", "")
6
+ CHUNK_DIR = "/tmp/tts/smchunks"
7
+ OUT_DIR = "/tmp/tts/smout"
8
+ VOICE = "scarlett_johansson"
9
+ FMT = "ogg"
10
+ SCRIPT = "/home/runner/.openclaw/workspace/scripts/voice.py"
11
+ RESTART_SCRIPT = "/home/runner/.openclaw/workspace/scripts/restart_space.py"
12
+
13
+ os.makedirs(OUT_DIR, exist_ok=True)
14
+
15
+ def restart_space():
16
+ print("Restarting space...")
17
+ subprocess.run(["python3", RESTART_SCRIPT], env={**os.environ, "HF_TOKEN": TOKEN}, capture_output=True)
18
+ print("Waiting 95s for startup...")
19
+ time.sleep(95)
20
+ # Quick test
21
+ r = subprocess.run(
22
+ ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
23
+ "--max-time", "60", "https://hf4uwho-pocket-tts.hf.space/tts?text=x&voice=af_alloy&format=ogg"],
24
+ capture_output=True, text=True, timeout=70
25
+ )
26
+ ok = r.stdout.strip() == "200"
27
+ print(f"Space test: {'OK' if ok else 'FAIL'}")
28
+ return ok
29
+
30
+ def generate_chunk(idx):
31
+ chunk_file = f"{CHUNK_DIR}/{idx:02d}"
32
+ outfile = f"{OUT_DIR}/chunk_{idx:02d}.{FMT}"
33
+ if os.path.exists(outfile) and os.path.getsize(outfile) > 1000:
34
+ print(f" Chunk {idx:02d} already exists, skipping")
35
+ return True
36
+ print(f" Generating chunk {idx:02d}...", end=" ", flush=True)
37
+ r = subprocess.run(
38
+ ["python3", SCRIPT, "--file", chunk_file, VOICE, FMT],
39
+ capture_output=True, text=True, timeout=600
40
+ )
41
+ # Find the output file
42
+ if r.returncode == 0 and r.stdout.strip():
43
+ gen_file = r.stdout.strip()
44
+ if os.path.exists(gen_file) and os.path.getsize(gen_file) > 1000:
45
+ os.rename(gen_file, outfile)
46
+ sz = os.path.getsize(outfile)
47
+ print(f"OK ({sz} bytes)")
48
+ return True
49
+ print(f"FAIL (exit={r.returncode})")
50
+ return False
51
+
52
+ # Get all chunk files
53
+ chunk_files = sorted(glob.glob(f"{CHUNK_DIR}/[0-9][0-9]"))
54
+ total = len(chunk_files)
55
+ print(f"Total chunks: {total}")
56
+
57
+ # Restart space first
58
+ if not restart_space():
59
+ print("Space not responding after restart, retrying...")
60
+ if not restart_space():
61
+ print("FATAL: Space won't start")
62
+ sys.exit(1)
63
+
64
+ failures_in_a_row = 0
65
+ i = 0
66
+
67
+ while i < total:
68
+ idx = int(os.path.basename(chunk_files[i]))
69
+ success = generate_chunk(idx)
70
+
71
+ if success:
72
+ failures_in_a_row = 0
73
+ i += 1
74
+ else:
75
+ failures_in_a_row += 1
76
+ if failures_in_a_row >= 2:
77
+ print(f" {failures_in_a_row} failures, restarting space...")
78
+ if not restart_space():
79
+ print("Space won't restart, waiting 120s and retrying...")
80
+ time.sleep(120)
81
+ if not restart_space():
82
+ print("FATAL: Space won't restart")
83
+ sys.exit(1)
84
+ failures_in_a_row = 0
85
+ else:
86
+ print(" Single failure, waiting 30s and retrying chunk...")
87
+ time.sleep(30)
88
+
89
+ print(f"\nAll {total} chunks generated!")
90
+
91
+ # Concatenate
92
+ ffmpeg_path = "/tmp/ffmpeg-master-latest-linux64-gpl/bin/ffmpeg"
93
+ concat_list = f"{OUT_DIR}/concat.txt"
94
+ with open(concat_list, 'w') as f:
95
+ for j in range(total):
96
+ chunk_path = f"{OUT_DIR}/chunk_{j:02d}.{FMT}"
97
+ if os.path.exists(chunk_path):
98
+ f.write(f"file '{chunk_path}'\n")
99
+
100
+ final = f"{OUT_DIR}/giselle_60min_full.ogg"
101
+ r = subprocess.run(
102
+ [ffmpeg_path, "-y", "-f", "concat", "-safe", "0", "-i", concat_list, "-c", "copy", final],
103
+ capture_output=True, text=True, timeout=60
104
+ )
105
+ if r.returncode == 0:
106
+ sz = os.path.getsize(final)
107
+ # Get duration
108
+ r2 = subprocess.run(
109
+ [ffmpeg_path.replace("ffmpeg", "ffprobe"), "-v", "quiet", "-show_entries", "format=duration", "-of", "csv=p=0", final],
110
+ capture_output=True, text=True, timeout=10
111
+ )
112
+ dur = float(r2.stdout.strip()) if r2.stdout.strip() else 0
113
+ print(f"FINAL: {final}")
114
+ print(f"SIZE: {sz} bytes")
115
+ print(f"DURATION: {dur/60:.1f} minutes")
116
+ else:
117
+ print(f"Concat failed: {r.stderr[:500]}")
scripts/chunk_giselle.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Split giselle_60min.txt into ~10K-12K char chunks on paragraph boundaries."""
3
+ import os
4
+
5
+ SRC = "/home/runner/.openclaw/workspace/scripts/giselle_60min.txt"
6
+ OUTDIR = "/tmp/tts_chunks"
7
+ os.makedirs(OUTDIR, exist_ok=True)
8
+
9
+ with open(SRC) as f:
10
+ text = f.read()
11
+
12
+ # Split on paragraph breaks (double newlines or scene breaks)
13
+ # Each paragraph is a block
14
+ paragraphs = []
15
+ for block in text.split("\n\n"):
16
+ block = block.strip()
17
+ if block:
18
+ paragraphs.append(block)
19
+
20
+ # Greedy chunking: accumulate paragraphs until we hit ~10K chars
21
+ chunks = []
22
+ current = []
23
+ current_size = 0
24
+ TARGET = 10000 # ~10K per chunk
25
+
26
+ for p in paragraphs:
27
+ if current_size + len(p) > TARGET and current:
28
+ chunks.append("\n\n".join(current))
29
+ current = [p]
30
+ current_size = len(p)
31
+ else:
32
+ current.append(p)
33
+ current_size += len(p)
34
+
35
+ if current:
36
+ chunks.append("\n\n".join(current))
37
+
38
+ # Write chunks
39
+ manifest = []
40
+ for i, chunk in enumerate(chunks):
41
+ fname = f"chunk_{i:03d}.txt"
42
+ path = os.path.join(OUTDIR, fname)
43
+ with open(path, "w") as f:
44
+ f.write(chunk)
45
+ sz = len(chunk)
46
+ manifest.append((fname, sz))
47
+ print(f" {fname}: {sz} chars")
48
+
49
+ total = sum(sz for _, sz in manifest)
50
+ print(f"\nTotal: {len(chunks)} chunks, {total} chars")
51
+ print(f"\nEstimated time: ~{sum(max(5, sz//2000) for _, sz in manifest)} min total")
52
+
53
+ # Save manifest
54
+ with open(os.path.join(OUTDIR, "manifest.txt"), "w") as f:
55
+ for fname, sz in manifest:
56
+ f.write(f"{fname}\t{sz}\n")
scripts/giselle_batch.sh ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+ export PATH="/tmp/ffmpeg-master-latest-linux64-gpl/bin:$PATH"
4
+
5
+ WORKSPACE="/home/runner/.openclaw/workspace"
6
+ OUTDIR="/tmp/tts_giselle"
7
+ mkdir -p "$OUTDIR"
8
+ rm -f "$OUTDIR"/*.ogg
9
+
10
+ # Generate each chunk sequentially
11
+ for i in 00 01 02 03 04 05 06 07 08 09; do
12
+ chunk="/tmp/giselle_chunk_${i}.txt"
13
+ echo "=== Starting chunk $i at $(date -u +%H:%M:%S) ==="
14
+ echo "Chunk file: $chunk ($(wc -c < "$chunk") bytes)"
15
+
16
+ # Read text, generate with voice.py
17
+ cd "$WORKSPACE"
18
+ python3 scripts/voice.py "$(cat "$chunk")" scarlett_johansson ogg
19
+
20
+ # Find the newest generated file and move it with chunk index
21
+ newest=$(ls -t /tmp/tts/voice_*.ogg | head -1)
22
+ cp "$newest" "$OUTDIR/chunk_${i}.ogg"
23
+ size=$(stat -c%s "$OUTDIR/chunk_${i}.ogg")
24
+ echo "=== Finished chunk $i: ${size} bytes at $(date -u +%H:%M:%S) ==="
25
+
26
+ # Small delay between chunks
27
+ sleep 2
28
+ done
29
+
30
+ echo "=== All chunks done, creating concat list ==="
31
+
32
+ # Create ffmpeg concat file
33
+ echo "" > /tmp/giselle_concat.txt
34
+ for i in 00 01 02 03 04 05 06 07 08 09; do
35
+ echo "file '${OUTDIR}/chunk_${i}.ogg'" >> /tmp/giselle_concat.txt
36
+ done
37
+
38
+ echo "=== Concatenating ==="
39
+ ffmpeg -y -f concat -safe 0 -i /tmp/giselle_concat.txt -c copy /tmp/giselle_60min_scarjo.ogg 2>&1 | tail -5
40
+
41
+ echo "=== Final file ==="
42
+ ls -lh /tmp/giselle_60min_scarjo.ogg
43
+ echo "=== DONE ==="
scripts/restart_space.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Restart a HuggingFace Space via API. Requires HF token with write access.
3
+ Usage: python3 restart_space.py [OWNER/REPO_NAME]
4
+ Default: hf4uwho/Pocket-TTS
5
+ """
6
+ import sys, os
7
+ from huggingface_hub import HfApi
8
+
9
+ repo_id = sys.argv[1] if len(sys.argv) > 1 else "hf4uwho/Pocket-TTS"
10
+ token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
11
+
12
+ if not token:
13
+ # Try reading from default token path
14
+ token_path = os.path.expanduser("~/.cache/huggingface/token")
15
+ if os.path.exists(token_path):
16
+ token = open(token_path).read().strip()
17
+
18
+ if not token:
19
+ print("ERROR: No HF token found. Set HF_TOKEN env var or login with `huggingface-cli login`")
20
+ sys.exit(1)
21
+
22
+ api = HfApi(token=token)
23
+ try:
24
+ api.restart_space(repo_id=repo_id)
25
+ print(f"Space {repo_id} restart initiated")
26
+ except Exception as e:
27
+ print(f"ERROR restarting space: {e}")
28
+ sys.exit(1)
scripts/run_giselle_batch.sh ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+ export PATH="/tmp/ffmpeg-master-latest-linux64-gpl/bin:$PATH"
4
+ WORKSPACE="/home/runner/.openclaw/workspace"
5
+ CHUNK_DIR="/tmp/tts_chunks"
6
+ OUT_DIR="/tmp/tts_giselle"
7
+ mkdir -p "$OUT_DIR"
8
+ VOICE="scarlett_johansson"
9
+ FMT="ogg"
10
+ LOG="$OUT_DIR/batch.log"
11
+
12
+ echo "=== Batch TTS started at $(date -u +%H:%M:%S) ===" | tee "$LOG"
13
+
14
+ for chunkfile in $(ls "$CHUNK_DIR"/chunk_*.txt | sort); do
15
+ idx=$(basename "$chunkfile" .txt | sed 's/chunk_//')
16
+ outfile="$OUT_DIR/chunk_${idx}.ogg"
17
+
18
+ if [ -f "$outfile" ] && [ $(stat -c%s "$outfile") -gt 1000 ]; then
19
+ echo "Chunk $idx already exists ($(stat -c%s "$outfile") bytes), skipping" | tee -a "$LOG"
20
+ continue
21
+ fi
22
+
23
+ chars=$(wc -c < "$chunkfile")
24
+ echo "=== Starting chunk $idx at $(date -u +%H:%M:%S) (${chars} chars) ===" | tee -a "$LOG"
25
+
26
+ cd "$WORKSPACE"
27
+ result=$(python3 scripts/voice.py --file "$chunkfile" "$VOICE" "$FMT" 2>&1)
28
+ exitcode=$?
29
+
30
+ if [ $exitcode -eq 0 ]; then
31
+ gen_file=$(echo "$result" | tail -1)
32
+ if [ -f "$gen_file" ] && [ $(stat -c%s "$gen_file") -gt 1000 ]; then
33
+ cp "$gen_file" "$outfile"
34
+ echo "=== Chunk $idx done: $(stat -c%s "$outfile") bytes at $(date -u +%H:%M:%S) ===" | tee -a "$LOG"
35
+ else
36
+ echo "=== Chunk $idx FAILED: file missing/small, retrying in 30s ===" | tee -a "$LOG"
37
+ sleep 30
38
+ result=$(python3 scripts/voice.py --file "$chunkfile" "$VOICE" "$FMT" 2>&1)
39
+ if [ $? -eq 0 ]; then
40
+ gen_file=$(echo "$result" | tail -1)
41
+ if [ -f "$gen_file" ] && [ $(stat -c%s "$gen_file") -gt 1000 ]; then
42
+ cp "$gen_file" "$outfile"
43
+ echo "=== Chunk $idx retry OK: $(stat -c%s "$outfile") bytes ===" | tee -a "$LOG"
44
+ fi
45
+ fi
46
+ fi
47
+ else
48
+ echo "=== Chunk $idx FAILED (exit=$exitcode), retrying in 30s ===" | tee -a "$LOG"
49
+ sleep 30
50
+ result=$(python3 scripts/voice.py --file "$chunkfile" "$VOICE" "$FMT" 2>&1)
51
+ if [ $? -eq 0 ]; then
52
+ gen_file=$(echo "$result" | tail -1)
53
+ if [ -f "$gen_file" ] && [ $(stat -c%s "$gen_file") -gt 1000 ]; then
54
+ cp "$gen_file" "$outfile"
55
+ echo "=== Chunk $idx retry OK: $(stat -c%s "$outfile") bytes ===" | tee -a "$LOG"
56
+ fi
57
+ fi
58
+ fi
59
+
60
+ sleep 2
61
+ done
62
+
63
+ echo "=== Concatenating at $(date -u +%H:%M:%S) ===" | tee -a "$LOG"
64
+ concat_list="$OUT_DIR/concat.txt"
65
+ > "$concat_list"
66
+ for chunkfile in $(ls "$CHUNK_DIR"/chunk_*.txt | sort); do
67
+ idx=$(basename "$chunkfile" .txt | sed 's/chunk_//')
68
+ f="$OUT_DIR/chunk_${idx}.ogg"
69
+ if [ -f "$f" ]; then
70
+ echo "file '$f'" >> "$concat_list"
71
+ fi
72
+ done
73
+
74
+ ffmpeg -y -f concat -safe 0 -i "$concat_list" -c copy "$OUT_DIR/giselle_60min_scarjo.ogg" 2>/dev/null
75
+ final_size=$(stat -c%s "$OUT_DIR/giselle_60min_scarjo.ogg" 2>/dev/null || echo 0)
76
+ duration=$(ffprobe -v quiet -show_entries format=duration -of csv=p=0 "$OUT_DIR/giselle_60min_scarjo.ogg" 2>/dev/null || echo "unknown")
77
+ echo "=== FINAL: $OUT_DIR/giselle_60min_scarjo.ogg ===" | tee -a "$LOG"
78
+ echo "=== SIZE: ${final_size} bytes ===" | tee -a "$LOG"
79
+ echo "=== DURATION: ${duration}s ===" | tee -a "$LOG"
80
+ echo "=== Batch complete at $(date -u +%H:%M:%S) ===" | tee -a "$LOG"
scripts/voice-chunked.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Chunked TTS with parallel fetching + retry + ffmpeg concat."""
3
+ import sys, urllib.parse, subprocess, os, hashlib, time, re
4
+ from concurrent.futures import ThreadPoolExecutor, as_completed
5
+
6
+ TTS_URL = "https://hf4uwho-pocket-tts.hf.space/tts"
7
+ OUTDIR = "/tmp/tts"
8
+ os.makedirs(OUTDIR, exist_ok=True)
9
+
10
+ def tts(text, voice, fmt, outfile, timeout=120, retries=2):
11
+ """Generate TTS for a single chunk with retry."""
12
+ encoded = urllib.parse.quote(text)
13
+ url = f"{TTS_URL}?text={encoded}&voice={voice}&format={fmt}"
14
+
15
+ for attempt in range(retries + 1):
16
+ try:
17
+ r = subprocess.run(
18
+ ["curl", "-s", "-o", outfile, "-w", "%{http_code}\n%{time_total}\n%{errormsg}",
19
+ "--max-time", str(timeout), "--connect-timeout", "15", url],
20
+ capture_output=True, text=True, timeout=timeout+10
21
+ )
22
+ lines = r.stdout.strip().split("\n")
23
+ code = lines[0] if lines else "?"
24
+ curl_time = lines[1] if len(lines) > 1 else "?"
25
+ error = lines[2] if len(lines) > 2 else ""
26
+
27
+ if code == "200":
28
+ mime = subprocess.run(["file", "-b", "--mime-type", outfile], capture_output=True, text=True).stdout.strip()
29
+ if mime.startswith("audio/") or mime == "application/ogg":
30
+ return os.path.getsize(outfile)
31
+ else:
32
+ raise RuntimeError(f"Not audio: {mime}")
33
+ elif code == "000":
34
+ if attempt < retries:
35
+ time.sleep(2)
36
+ continue
37
+ raise RuntimeError(f"Connection failed: {error[:80]}")
38
+ else:
39
+ raise RuntimeError(f"HTTP {code}")
40
+ except subprocess.TimeoutExpired:
41
+ if attempt < retries:
42
+ time.sleep(2)
43
+ continue
44
+ raise RuntimeError("Timeout")
45
+
46
+ def chunk_text(text, max_chars=400):
47
+ """Split text into chunks at sentence boundaries."""
48
+ sentences = re.split(r'(?<=[.!?])\s+', text)
49
+ chunks = []
50
+ current = ""
51
+ for s in sentences:
52
+ if len(current) + len(s) + 1 > max_chars and current:
53
+ chunks.append(current.strip())
54
+ current = s
55
+ else:
56
+ current = (current + " " + s).strip()
57
+ if current.strip():
58
+ chunks.append(current.strip())
59
+ return chunks
60
+
61
+ def gen_chunk(i, chunk, voice, fmt, chunkdir, timeout):
62
+ """Generate a single chunk, returns (i, path, size) or (i, path, None) on fail."""
63
+ cfile = f"{chunkdir}/c{i:03d}.{fmt}"
64
+ try:
65
+ sz = tts(chunk, voice, fmt, cfile, timeout=timeout)
66
+ return (i, cfile, sz)
67
+ except Exception as e:
68
+ return (i, cfile, None)
69
+
70
+ def main():
71
+ textfile = sys.argv[1] if len(sys.argv) > 1 else None
72
+ voice = sys.argv[2] if len(sys.argv) > 2 else "af_alloy"
73
+ fmt = sys.argv[3] if len(sys.argv) > 3 else "ogg"
74
+ parallel = int(sys.argv[4]) if len(sys.argv) > 4 else 5 # parallel workers
75
+
76
+ if not textfile:
77
+ print("Usage: voice-chunked.py <textfile> [voice] [format] [parallel]", file=sys.stderr)
78
+ sys.exit(1)
79
+
80
+ with open(textfile, "r") as f:
81
+ text = f.read().strip()
82
+
83
+ chunks = chunk_text(text)
84
+ print(f"Split {len(text)} chars into {len(chunks)} chunks, {parallel} parallel", file=sys.stderr)
85
+
86
+ h = hashlib.md5(f"{textfile}{voice}".encode()).hexdigest()[:12]
87
+ chunkdir = f"{OUTDIR}/chunks_{h}"
88
+ os.makedirs(chunkdir, exist_ok=True)
89
+
90
+ # Estimate per-chunk timeout: 25s per 400 chars
91
+ per_chunk_timeout = max(60, int((len(text) / max(len(chunks), 1)) * 25 / 400 * 3))
92
+ total_start = time.time()
93
+
94
+ # Fire all chunks in parallel via ThreadPoolExecutor
95
+ chunk_files = {} # i -> path
96
+ failed = 0
97
+ done = 0
98
+ n = len(chunks)
99
+
100
+ with ThreadPoolExecutor(max_workers=parallel) as pool:
101
+ fut_map = {pool.submit(gen_chunk, i, c, voice, fmt, chunkdir, per_chunk_timeout): i
102
+ for i, c in enumerate(chunks)}
103
+
104
+ for fut in as_completed(fut_map):
105
+ i, path, sz = fut.result()
106
+ done += 1
107
+ if sz is not None:
108
+ chunk_files[i] = path
109
+ print(f" [{done}/{n}] ok {i:3d} {sz/1024:.0f}KB", file=sys.stderr, flush=True)
110
+ else:
111
+ failed += 1
112
+ print(f" [{done}/{n}] FAIL {i:3d}", file=sys.stderr, flush=True)
113
+
114
+ if not chunk_files:
115
+ print("ERROR: No chunks succeeded", file=sys.stderr)
116
+ sys.exit(1)
117
+
118
+ # Retry failed chunks serially (some may work on retry with less contention)
119
+ if failed > 0:
120
+ print(f"\nRetrying {failed} failed chunks serially...", file=sys.stderr)
121
+ for i, c in enumerate(chunks):
122
+ if i in chunk_files:
123
+ continue
124
+ cfile = f"{chunkdir}/c{i:03d}.{fmt}"
125
+ try:
126
+ sz = tts(c, voice, fmt, cfile, timeout=per_chunk_timeout*2, retries=3)
127
+ chunk_files[i] = cfile
128
+ print(f" Retry ok {i:3d} {sz/1024:.0f}KB", file=sys.stderr, flush=True)
129
+ failed -= 1
130
+ except:
131
+ print(f" Retry failed {i:3d}", file=sys.stderr, flush=True)
132
+
133
+ if failed > 0:
134
+ print(f"WARNING: {failed}/{n} chunks permanently failed", file=sys.stderr)
135
+
136
+ # Concatenate in order
137
+ ordered = [chunk_files[i] for i in sorted(chunk_files)]
138
+ outfile = f"{OUTDIR}/full_{h}.{fmt}"
139
+
140
+ if len(ordered) == 1:
141
+ import shutil
142
+ shutil.copy2(ordered[0], outfile)
143
+ else:
144
+ concat_file = f"{chunkdir}/concat.txt"
145
+ with open(concat_file, "w") as f:
146
+ for cf in ordered:
147
+ f.write(f"file '{cf}'\n")
148
+
149
+ r = subprocess.run(
150
+ ["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", concat_file, "-c", "copy", outfile],
151
+ capture_output=True, text=True, timeout=30
152
+ )
153
+ if r.returncode != 0:
154
+ print(f"ffmpeg failed ({r.returncode}), using raw cat", file=sys.stderr)
155
+ with open(outfile, "wb") as out:
156
+ for cf in ordered:
157
+ with open(cf, "rb") as inp:
158
+ out.write(inp.read())
159
+
160
+ # Cleanup
161
+ import shutil
162
+ shutil.rmtree(chunkdir, ignore_errors=True)
163
+
164
+ total = time.time() - total_start
165
+ sz = os.path.getsize(outfile)
166
+ print(f"Done: {outfile} ({sz/1024:.0f}KB) in {total:.1f}s", file=sys.stderr)
167
+ print(outfile)
168
+
169
+ if __name__ == "__main__":
170
+ main()
scripts/voice-from-file.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Generate TTS from a text file. Handles URL encoding properly."""
3
+ import sys, urllib.parse, subprocess, os, hashlib, time
4
+
5
+ TTS_URL = "https://hf4uwho-pocket-tts.hf.space/tts"
6
+ OUTDIR = "/tmp/tts"
7
+ os.makedirs(OUTDIR, exist_ok=True)
8
+
9
+ textfile = sys.argv[1] if len(sys.argv) > 1 else None
10
+ voice = sys.argv[2] if len(sys.argv) > 2 else "af_alloy"
11
+ fmt = sys.argv[3] if len(sys.argv) > 3 else "ogg"
12
+
13
+ if not textfile:
14
+ print("Usage: voice-from-file.py <textfile> [voice] [format]", file=sys.stderr)
15
+ sys.exit(1)
16
+
17
+ with open(textfile, "r") as f:
18
+ text = f.read().strip()
19
+
20
+ encoded = urllib.parse.quote(text)
21
+ url = f"{TTS_URL}?text={encoded}&voice={voice}&format={fmt}"
22
+ h = hashlib.md5(f"{textfile}{voice}{time.time()}".encode()).hexdigest()[:12]
23
+ outfile = f"{OUTDIR}/magi_{h}.{fmt}"
24
+
25
+ print(f"Generating {len(text)} chars...", file=sys.stderr)
26
+ start = time.time()
27
+
28
+ r = subprocess.run(
29
+ ["curl", "-s", "-o", outfile, "-w", "%{http_code}\n%{time_total}", "--max-time", "300", url],
30
+ capture_output=True, text=True, timeout=310
31
+ )
32
+
33
+ elapsed = time.time() - start
34
+ parts = r.stdout.strip().split("\n")
35
+ code = parts[0] if parts else "?"
36
+ curl_time = parts[1] if len(parts) > 1 else "?"
37
+
38
+ print(f"HTTP {code} | curl: {curl_time}s | total: {elapsed:.1f}s", file=sys.stderr)
39
+
40
+ if code != "200":
41
+ print(f"ERROR: HTTP {code}", file=sys.stderr)
42
+ if os.path.exists(outfile): os.remove(outfile)
43
+ sys.exit(1)
44
+
45
+ sz = os.path.getsize(outfile)
46
+ print(f"Output: {outfile} ({sz} bytes)", file=sys.stderr)
47
+ print(outfile)
scripts/voice-long.sh ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Chunked TTS for long text - splits into sentences, generates each, concatenates
3
+ # Usage: voice-long.sh "text" [voice] [format]
4
+
5
+ TTS_URL="https://hf4uwho-pocket-tts.hf.space/tts"
6
+ TEXT="${1:?Usage: voice-long.sh 'text' [voice] [format]}"
7
+ VOICE="${2:-af_alloy}"
8
+ FORMAT="${3:-ogg}"
9
+ OUTDIR="/tmp/tts"
10
+ mkdir -p "$OUTDIR"
11
+
12
+ HASH=$(echo -n "$TEXT$VOICE$(date +%s%N)" | md5sum | cut -c1-12)
13
+ OUTFILE="$OUTDIR/voice_${HASH}.${FORMAT}"
14
+ CHUNKDIR="$OUTDIR/chunks_${HASH}"
15
+ mkdir -p "$CHUNKDIR"
16
+
17
+ # Split text into sentences (rough but effective)
18
+ CHUNKS=$(python3 -c "
19
+ import re
20
+ text = '''$TEXT'''
21
+ # Split on sentence boundaries, group into chunks of ~200 chars
22
+ sentences = re.split(r'(?<=[.!?])\s+', text)
23
+ chunks = []
24
+ current = ''
25
+ for s in sentences:
26
+ if len(current) + len(s) > 300 and current:
27
+ chunks.append(current.strip())
28
+ current = s
29
+ else:
30
+ current = (current + ' ' + s).strip()
31
+ if current.strip():
32
+ chunks.append(current.strip())
33
+ for i, c in enumerate(chunks):
34
+ print(f'CHUNK_{i}:{c}')
35
+ ")
36
+
37
+ # Generate each chunk
38
+ INDEX=0
39
+ CHUNK_FILES=()
40
+ while IFS= read -r line; do
41
+ if [[ "$line" == CHUNK_* ]]; then
42
+ CHUNK_TEXT="${line#CHUNK_[0-9]*:}"
43
+ # Strip the index prefix
44
+ CHUNK_TEXT="${line#*:}"
45
+ ENCODED=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$CHUNK_TEXT'''))")
46
+ CHUNKFILE="$CHUNKDIR/chunk_${INDEX}.${FORMAT}"
47
+
48
+ HTTP_CODE=$(curl -s -w "%{http_code}" -o "$CHUNKFILE" "${TTS_URL}?text=${ENCODED}&voice=${VOICE}&format=${FORMAT}" --max-time 60 2>/dev/null)
49
+
50
+ if [ "$HTTP_CODE" != "200" ]; then
51
+ echo "ERROR: Chunk $INDEX failed with HTTP $HTTP_CODE" >&2
52
+ rm -rf "$CHUNKDIR"
53
+ exit 1
54
+ fi
55
+
56
+ # Verify audio
57
+ MIME=$(file -b --mime-type "$CHUNKFILE" 2>/dev/null)
58
+ if [[ "$MIME" == audio/* ]] || [[ "$MIME" == application/ogg ]]; then
59
+ CHUNK_FILES+=("$CHUNKFILE")
60
+ INDEX=$((INDEX + 1))
61
+ else
62
+ echo "WARNING: Chunk $INDEX not audio ($MIME), skipping" >&2
63
+ fi
64
+ fi
65
+ done <<< "$CHUNKS"
66
+
67
+ if [ ${#CHUNK_FILES[@]} -eq 0 ]; then
68
+ echo "ERROR: No chunks generated" >&2
69
+ rm -rf "$CHUNKDIR"
70
+ exit 1
71
+ fi
72
+
73
+ # If only one chunk, just use it
74
+ if [ ${#CHUNK_FILES[@]} -eq 1 ]; then
75
+ cp "${CHUNK_FILES[0]}" "$OUTFILE"
76
+ else
77
+ # Concatenate Ogg files using sox or ffmpeg, fallback to simple cat
78
+ if command -v sox &>/dev/null; then
79
+ sox "${CHUNK_FILES[@]}" "$OUTFILE" 2>/dev/null
80
+ elif command -v ffmpeg &>/dev/null; then
81
+ # Create concat list
82
+ CONCFILE="$CHUNKDIR/concat.txt"
83
+ > "$CONCFILE"
84
+ for f in "${CHUNK_FILES[@]}"; do
85
+ echo "file '$f'" >> "$CONCFILE"
86
+ done
87
+ ffmpeg -y -f concat -safe 0 -i "$CONCFILE" -c copy "$OUTFILE" 2>/dev/null
88
+ else
89
+ # Simple cat (works for raw Ogg but may have glitches)
90
+ cat "${CHUNK_FILES[@]}" > "$OUTFILE"
91
+ fi
92
+ fi
93
+
94
+ rm -rf "$CHUNKDIR"
95
+
96
+ if [ -f "$OUTFILE" ] && [ -s "$OUTFILE" ]; then
97
+ echo "$OUTFILE"
98
+ else
99
+ echo "ERROR: Final file empty or missing" >&2
100
+ exit 1
101
+ fi
scripts/voice.sh ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Pocket-TTS voice generator + sender
3
+ # Usage: voice.sh "text" [voice] [format]
4
+ # Output: path to generated audio file (for chaining)
5
+
6
+ TTS_URL="https://hf4uwho-pocket-tts.hf.space/tts"
7
+ TEXT="${1:?Usage: voice.sh 'text' [voice] [format]}"
8
+ VOICE="${2:-af_alloy}"
9
+ FORMAT="${3:-ogg}"
10
+ OUTDIR="/tmp/tts"
11
+ mkdir -p "$OUTDIR"
12
+
13
+ # Unique filename
14
+ HASH=$(echo -n "$TEXT$VOICE$(date +%s%N)" | md5sum | cut -c1-12)
15
+ OUTFILE="$OUTDIR/voice_${HASH}.${FORMAT}"
16
+
17
+ # Generate
18
+ HTTP_CODE=$(curl -s -w "%{http_code}" -o "$OUTFILE" "${TTS_URL}?text=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$TEXT'''))")&voice=${VOICE}&format=${FORMAT}" 2>/dev/null)
19
+
20
+ if [ "$HTTP_CODE" != "200" ]; then
21
+ echo "ERROR: HTTP $HTTP_CODE" >&2
22
+ rm -f "$OUTFILE"
23
+ exit 1
24
+ fi
25
+
26
+ # Verify it's actual audio
27
+ MIME=$(file -b --mime-type "$OUTFILE" 2>/dev/null)
28
+ if [[ "$MIME" != audio/* ]] && [[ "$MIME" != application/ogg ]]; then
29
+ echo "ERROR: Not audio (got $MIME)" >&2
30
+ cat "$OUTFILE" >&2
31
+ rm -f "$OUTFILE"
32
+ exit 1
33
+ fi
34
+
35
+ echo "$OUTFILE"