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
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import schedule
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from google.cloud import texttospeech
|
| 6 |
+
import openai
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import threading
|
| 9 |
+
from datetime import datetime, timedelta
|
| 10 |
+
from streamlit_autorefresh import st_autorefresh
|
| 11 |
+
import tempfile
|
| 12 |
+
import base64
|
| 13 |
+
import PyPDF2
|
| 14 |
+
|
| 15 |
+
# Cargar las variables de entorno desde el archivo .env
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
+
# Configura la clave de API de Google Cloud
|
| 19 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials/botidinamix-g.json"
|
| 20 |
+
|
| 21 |
+
# Configura la clave de API de OpenAI
|
| 22 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 23 |
+
|
| 24 |
+
# Verifica que la clave API de OpenAI est茅 configurada
|
| 25 |
+
if not openai.api_key:
|
| 26 |
+
st.error("No API key provided for OpenAI. Please set your API key in the .env file.")
|
| 27 |
+
|
| 28 |
+
# Funci贸n para leer un archivo PDF y extraer el texto
|
| 29 |
+
def leer_pdf(file):
|
| 30 |
+
pdf_reader = PyPDF2.PdfFileReader(file)
|
| 31 |
+
texto = ""
|
| 32 |
+
for page_num in range(pdf_reader.numPages):
|
| 33 |
+
page = pdf_reader.getPage(page_num)
|
| 34 |
+
texto += page.extract_text()
|
| 35 |
+
return texto
|
| 36 |
+
|
| 37 |
+
# Funci贸n para dividir el texto en una lista de recordatorios
|
| 38 |
+
def procesar_recordatorios(texto):
|
| 39 |
+
recordatorios = texto.split('\n')
|
| 40 |
+
return [r.strip() for r in recordatorios if r.strip()]
|
| 41 |
+
|
| 42 |
+
# Funci贸n para generar una respuesta de voz usando Google Cloud Text-to-Speech
|
| 43 |
+
def generar_respuesta_de_voz(text):
|
| 44 |
+
client = texttospeech.TextToSpeechClient()
|
| 45 |
+
|
| 46 |
+
synthesis_input = texttospeech.SynthesisInput(text=text)
|
| 47 |
+
voice = texttospeech.VoiceSelectionParams(language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE)
|
| 48 |
+
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
|
| 52 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 53 |
+
tmp_file.write(response.audio_content)
|
| 54 |
+
audio_file_path = tmp_file.name
|
| 55 |
+
|
| 56 |
+
st.write(f"Audio generado en: {audio_file_path}") # Depuraci贸n
|
| 57 |
+
return audio_file_path
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.error(f"Error al generar el audio: {e}")
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
# Funci贸n para generar y reproducir mensajes peri贸dicos
|
| 63 |
+
def tarea_periodica(mensaje):
|
| 64 |
+
audio_path = generar_respuesta_de_voz(mensaje)
|
| 65 |
+
if audio_path:
|
| 66 |
+
st.session_state.last_audio_path = audio_path
|
| 67 |
+
st.experimental_rerun()
|
| 68 |
+
else:
|
| 69 |
+
st.error("No se pudo generar el audio para el mensaje.")
|
| 70 |
+
|
| 71 |
+
# Funci贸n para programar los recordatorios en horas espec铆ficas
|
| 72 |
+
def programar_recordatorios(recordatorios, horarios):
|
| 73 |
+
schedule.clear() # Limpiar tareas programadas anteriormente
|
| 74 |
+
for i, (recordatorio, horario) in enumerate(zip(recordatorios, horarios)):
|
| 75 |
+
schedule.every().day.at(horario).do(lambda r=recordatorio: tarea_periodica(r))
|
| 76 |
+
st.write(f"Recordatorio {i+1} programado para las {horario}.")
|
| 77 |
+
|
| 78 |
+
# Crear la aplicaci贸n Streamlit
|
| 79 |
+
st.title("Asistente de Recordatorios de Voz")
|
| 80 |
+
st.write("Este asistente reproduce recordatorios en voz autom谩ticamente seg煤n los horarios configurados.")
|
| 81 |
+
|
| 82 |
+
# Inicializar el 铆ndice de recordatorios en la sesi贸n
|
| 83 |
+
if 'indice_recordatorio' not in st.session_state:
|
| 84 |
+
st.session_state.indice_recordatorio = 0
|
| 85 |
+
|
| 86 |
+
# Controles de interfaz para subir el archivo PDF y configurar los horarios
|
| 87 |
+
pdf_file = st.file_uploader("Sube el archivo PDF con los recordatorios", type="pdf")
|
| 88 |
+
|
| 89 |
+
# Procesar el archivo PDF y obtener la lista de recordatorios
|
| 90 |
+
if pdf_file:
|
| 91 |
+
texto_pdf = leer_pdf(pdf_file)
|
| 92 |
+
recordatorios = procesar_recordatorios(texto_pdf)
|
| 93 |
+
st.write("Recordatorios extra铆dos del PDF:")
|
| 94 |
+
st.write(recordatorios)
|
| 95 |
+
|
| 96 |
+
# Campo de texto para ingresar los horarios de los recordatorios
|
| 97 |
+
horarios_input = st.text_area("Escribe los horarios de los recordatorios en formato HH:MM, separados por comas", "09:00,13:00,18:00")
|
| 98 |
+
horarios = [h.strip() for h in horarios_input.split(',')]
|
| 99 |
+
|
| 100 |
+
if st.button("Programar Recordatorios"):
|
| 101 |
+
programar_recordatorios(recordatorios, horarios)
|
| 102 |
+
st.success("Recordatorios programados seg煤n los horarios indicados.")
|
| 103 |
+
|
| 104 |
+
# Iniciar el scheduler en un hilo separado
|
| 105 |
+
def run_scheduler():
|
| 106 |
+
while True:
|
| 107 |
+
schedule.run_pending()
|
| 108 |
+
time.sleep(1)
|
| 109 |
+
|
| 110 |
+
if 'scheduler_thread' not in st.session_state:
|
| 111 |
+
st.session_state.scheduler_thread = threading.Thread(target=run_scheduler)
|
| 112 |
+
st.session_state.scheduler_thread.start()
|
| 113 |
+
|
| 114 |
+
# C贸digo JavaScript para reproducci贸n autom谩tica de audio
|
| 115 |
+
if 'last_audio_path' in st.session_state:
|
| 116 |
+
with open(st.session_state.last_audio_path, "rb") as audio_file:
|
| 117 |
+
audio_bytes = audio_file.read()
|
| 118 |
+
audio_base64 = base64.b64encode(audio_bytes).decode()
|
| 119 |
+
|
| 120 |
+
audio_html = f"""
|
| 121 |
+
<audio autoplay>
|
| 122 |
+
<source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
|
| 123 |
+
Your browser does not support the audio element.
|
| 124 |
+
</audio>
|
| 125 |
+
"""
|
| 126 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
| 127 |
+
|
| 128 |
+
# Autorefresh cada 30 segundos (puedes ajustar este intervalo)
|
| 129 |
+
st_autorefresh(interval=30000, key="datarefresh")
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-cloud-texttospeech
|
| 3 |
+
schedule
|
| 4 |
+
transformers
|
| 5 |
+
torch
|
| 6 |
+
streamlit_autorefresh
|
respuesta.mp3
ADDED
|
Binary file (174 kB). View file
|
|
|