File size: 1,098 Bytes
3a3406b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)