Download modules/talk.py from CVision-assitant/asistente-virtual-CVision: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/CVision-assitant/asistente-virtual-CVision/resolve/main/modules/talk.py
- Command line
-
hf download hf://spaces/CVision-assitant/asistente-virtual-CVision/modules/talk.py
-
curl -L -o talk.py https://huggingface.co/spaces/CVision-assitant/asistente-virtual-CVision/resolve/main/modules/talk.py
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) |