CVision-assitant's picture
Upload 53 files
3a3406b verified
Raw History Blame Contribute Delete
1.1 kB
from gtts import gTTS
import os
import tempfile
import playsound
def summarize_for_voice(text, max_chars=350):
"""
Resume texto largo para lectura por voz.
Solo toma la primera parte para evitar audios muy extensos.
"""
if not text:
return ""
text = str(text).strip()
if len(text) <= max_chars:
return text
summary = text[:max_chars]
# cortar en última oración
last_dot = summary.rfind(".")
if last_dot != -1:
summary = summary[:last_dot + 1]
return summary + " Resumen finalizado."
def speak(text):
try:
if not text:
return
voice_text = summarize_for_voice(text)
tts = gTTS(
text=voice_text,
lang="es",
slow=False
)
temp_path = tempfile.mktemp(suffix=".mp3")
tts.save(temp_path)
playsound.playsound(temp_path)
if os.path.exists(temp_path):
os.remove(temp_path)
except Exception as e:
print("Error en speak:", e)