Spaces:
Build error
Build error
optimize the max character 400-500
Browse files
app.py
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess, json, os, io, tempfile
|
| 3 |
from faster_whisper import WhisperModel
|
|
@@ -63,7 +68,7 @@ def chat_with_llm(history_messages, user_text):
|
|
| 63 |
for m in (history_messages or [])]
|
| 64 |
)
|
| 65 |
prompt = f"{SYSTEM_PROMPT}\n{history_text}\nUser: {user_text}\nAssistant:"
|
| 66 |
-
out = gen(prompt)[0]["generated_text"]
|
| 67 |
# Return only the new assistant chunk after the prompt
|
| 68 |
return out.split("Assistant:", 1)[-1].strip()
|
| 69 |
|
|
@@ -72,29 +77,66 @@ def chat_with_llm(history_messages, user_text):
|
|
| 72 |
# near top-level (global singletons)
|
| 73 |
_SILERO_TTS = None
|
| 74 |
|
| 75 |
-
def
|
| 76 |
"""
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"""
|
| 80 |
-
import torch, tempfile
|
| 81 |
-
import soundfile as sf
|
| 82 |
-
|
| 83 |
global _SILERO_TTS
|
| 84 |
if _SILERO_TTS is None:
|
|
|
|
| 85 |
obj = torch.hub.load(
|
| 86 |
repo_or_dir="snakers4/silero-models",
|
| 87 |
model="silero_tts",
|
| 88 |
language="en",
|
| 89 |
speaker="v3_en",
|
| 90 |
-
trust_repo=True,
|
| 91 |
)
|
|
|
|
| 92 |
_SILERO_TTS = obj[0] if isinstance(obj, (list, tuple)) else obj
|
| 93 |
|
| 94 |
model = _SILERO_TTS
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
out_wav = tempfile.mktemp(suffix=".wav")
|
| 100 |
sf.write(out_wav, audio, sample_rate)
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tempfile
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
import torch
|
| 6 |
import gradio as gr
|
| 7 |
import subprocess, json, os, io, tempfile
|
| 8 |
from faster_whisper import WhisperModel
|
|
|
|
| 68 |
for m in (history_messages or [])]
|
| 69 |
)
|
| 70 |
prompt = f"{SYSTEM_PROMPT}\n{history_text}\nUser: {user_text}\nAssistant:"
|
| 71 |
+
out = gen(prompt, max_new_tokens=192, temperature=0.7, do_sample=True, repetition_penalty=1.1, pad_token_id=_tok.eos_token_id, eos_token_id=_tok.eos_token_id,)[0]["generated_text"]
|
| 72 |
# Return only the new assistant chunk after the prompt
|
| 73 |
return out.split("Assistant:", 1)[-1].strip()
|
| 74 |
|
|
|
|
| 77 |
# near top-level (global singletons)
|
| 78 |
_SILERO_TTS = None
|
| 79 |
|
| 80 |
+
def _split_text_for_tts(text: str, max_chars: int = 600):
|
| 81 |
"""
|
| 82 |
+
Greedy pack sentences into chunks not exceeding max_chars.
|
| 83 |
+
Keeps punctuation to avoid monotone prosody.
|
| 84 |
+
"""
|
| 85 |
+
# Normalize whitespace
|
| 86 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 87 |
+
|
| 88 |
+
# Prefer sentence boundaries; fall back to hard chunks if no punctuation
|
| 89 |
+
sentences = re.split(r"(?<=[\.\!\?])\s+", text) if re.search(r"[.!?]", text) else [text]
|
| 90 |
+
|
| 91 |
+
chunks, cur = [], ""
|
| 92 |
+
for s in sentences:
|
| 93 |
+
s = s.strip()
|
| 94 |
+
if not s:
|
| 95 |
+
continue
|
| 96 |
+
if len(cur) + len(s) + 1 <= max_chars:
|
| 97 |
+
cur = f"{cur} {s}".strip() if cur else s
|
| 98 |
+
else:
|
| 99 |
+
if cur:
|
| 100 |
+
chunks.append(cur)
|
| 101 |
+
# If a single sentence is longer than max_chars, hard-split it
|
| 102 |
+
while len(s) > max_chars:
|
| 103 |
+
chunks.append(s[:max_chars])
|
| 104 |
+
s = s[max_chars:]
|
| 105 |
+
cur = s
|
| 106 |
+
if cur:
|
| 107 |
+
chunks.append(cur)
|
| 108 |
+
return chunks
|
| 109 |
+
|
| 110 |
+
def tts_silero(text: str, sample_rate: int = 48000, speaker: str = "en_0") -> str:
|
| 111 |
+
"""
|
| 112 |
+
Chunked TTS with Silero. Returns path to a WAV file.
|
| 113 |
"""
|
|
|
|
|
|
|
|
|
|
| 114 |
global _SILERO_TTS
|
| 115 |
if _SILERO_TTS is None:
|
| 116 |
+
# Load once and cache
|
| 117 |
obj = torch.hub.load(
|
| 118 |
repo_or_dir="snakers4/silero-models",
|
| 119 |
model="silero_tts",
|
| 120 |
language="en",
|
| 121 |
speaker="v3_en",
|
| 122 |
+
trust_repo=True, # avoid interactive prompt
|
| 123 |
)
|
| 124 |
+
# Older silero hub returns (model, example_texts, languages, speakers)
|
| 125 |
_SILERO_TTS = obj[0] if isinstance(obj, (list, tuple)) else obj
|
| 126 |
|
| 127 |
model = _SILERO_TTS
|
| 128 |
+
|
| 129 |
+
# Split long text to avoid the "too long" error
|
| 130 |
+
chunks = _split_text_for_tts(text, max_chars=600)
|
| 131 |
+
|
| 132 |
+
audio_all = []
|
| 133 |
+
for ch in chunks:
|
| 134 |
+
wav = model.apply_tts(text=ch, speaker=speaker, sample_rate=sample_rate)
|
| 135 |
+
# Ensure numpy array (sometimes comes as torch tensor)
|
| 136 |
+
wav = wav.detach().cpu().numpy() if hasattr(wav, "detach") else np.asarray(wav)
|
| 137 |
+
audio_all.append(wav)
|
| 138 |
+
|
| 139 |
+
audio = np.concatenate(audio_all) if len(audio_all) > 1 else audio_all[0]
|
| 140 |
|
| 141 |
out_wav = tempfile.mktemp(suffix=".wav")
|
| 142 |
sf.write(out_wav, audio, sample_rate)
|