Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import random
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from transformers import pipeline
|
| 9 |
from google.cloud import texttospeech
|
|
|
|
| 10 |
|
| 11 |
# Cargar variables de entorno desde el archivo .env
|
| 12 |
load_dotenv()
|
|
@@ -16,30 +17,41 @@ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "botidinamix-g.json"
|
|
| 16 |
# Configuración de Streamlit
|
| 17 |
st.set_page_config(page_title="Asistente Teológico", page_icon="📖")
|
| 18 |
|
| 19 |
-
# Estilos CSS personalizados
|
| 20 |
st.markdown(
|
| 21 |
"""
|
| 22 |
<style>
|
|
|
|
|
|
|
| 23 |
body {
|
| 24 |
-
background:
|
|
|
|
| 25 |
color: #333;
|
|
|
|
| 26 |
}
|
| 27 |
.stButton>button {
|
| 28 |
background-color: #4CAF50;
|
| 29 |
color: white;
|
| 30 |
border-radius: 10px;
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
.stTextInput>div>div>input {
|
| 33 |
border: 1px solid #4CAF50;
|
| 34 |
border-radius: 10px;
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
.stMarkdown>div>p {
|
| 37 |
color: black;
|
| 38 |
max-height: 300px;
|
| 39 |
overflow-y: auto;
|
|
|
|
| 40 |
}
|
| 41 |
.stMarkdown>h1, .stMarkdown>h2, .stMarkdown>h3, .stMarkdown>h4, .stMarkdown>h5, .stMarkdown>h6 {
|
| 42 |
color: blue;
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
.stAudio {
|
| 45 |
margin-top: 20px;
|
|
@@ -49,6 +61,7 @@ st.markdown(
|
|
| 49 |
.stFileUploader>div>div>input {
|
| 50 |
border: 1px solid #4CAF50;
|
| 51 |
border-radius: 10px;
|
|
|
|
| 52 |
}
|
| 53 |
.spinner {
|
| 54 |
border: 8px solid #f3f3f3;
|
|
@@ -72,6 +85,7 @@ st.markdown(
|
|
| 72 |
.assistant-response {
|
| 73 |
max-height: 200px;
|
| 74 |
overflow-y: auto;
|
|
|
|
| 75 |
}
|
| 76 |
</style>
|
| 77 |
""",
|
|
@@ -85,7 +99,7 @@ st.markdown("Bienvenido al Asistente Teológico, donde puedes preguntar sobre in
|
|
| 85 |
|
| 86 |
# Barra lateral para la navegación
|
| 87 |
st.sidebar.title("Navegación")
|
| 88 |
-
page = st.sidebar.selectbox("Selecciona una página", ["Chat Asistente", "Generador de Frases Bíblicas", "Recibir Reflexión"])
|
| 89 |
|
| 90 |
# Cargar el modelo de clasificación de imágenes
|
| 91 |
clasificador = pipeline("zero-shot-image-classification")
|
|
@@ -145,6 +159,30 @@ def obtener_respuesta(pregunta):
|
|
| 145 |
respuesta = response['choices'][0]['message']['content']
|
| 146 |
return respuesta
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
if page == "Chat Asistente":
|
| 149 |
# Chat con el asistente
|
| 150 |
st.subheader("🗣️ Chat con el Asistente")
|
|
@@ -263,3 +301,16 @@ elif page == "Recibir Reflexión":
|
|
| 263 |
st.markdown(audio_html, unsafe_allow_html=True)
|
| 264 |
else:
|
| 265 |
st.warning("No se encontraron reflexiones en la carpeta especificada.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from transformers import pipeline
|
| 9 |
from google.cloud import texttospeech
|
| 10 |
+
import PyPDF2
|
| 11 |
|
| 12 |
# Cargar variables de entorno desde el archivo .env
|
| 13 |
load_dotenv()
|
|
|
|
| 17 |
# Configuración de Streamlit
|
| 18 |
st.set_page_config(page_title="Asistente Teológico", page_icon="📖")
|
| 19 |
|
| 20 |
+
# Estilos CSS personalizados con la nueva imagen de fondo y tipografía
|
| 21 |
st.markdown(
|
| 22 |
"""
|
| 23 |
<style>
|
| 24 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
|
| 25 |
+
|
| 26 |
body {
|
| 27 |
+
background: url('https://www.dekowizja.pl/img2/1000/78118676/obraz-na-plotnie-laka-slonce-ziarno-piekny.jpg');
|
| 28 |
+
background-size: cover;
|
| 29 |
color: #333;
|
| 30 |
+
font-family: 'Roboto', sans-serif;
|
| 31 |
}
|
| 32 |
.stButton>button {
|
| 33 |
background-color: #4CAF50;
|
| 34 |
color: white;
|
| 35 |
border-radius: 10px;
|
| 36 |
+
font-size: 16px;
|
| 37 |
+
padding: 10px 20px;
|
| 38 |
}
|
| 39 |
.stTextInput>div>div>input {
|
| 40 |
border: 1px solid #4CAF50;
|
| 41 |
border-radius: 10px;
|
| 42 |
+
font-size: 16px;
|
| 43 |
+
padding: 10px;
|
| 44 |
}
|
| 45 |
.stMarkdown>div>p {
|
| 46 |
color: black;
|
| 47 |
max-height: 300px;
|
| 48 |
overflow-y: auto;
|
| 49 |
+
font-size: 16px;
|
| 50 |
}
|
| 51 |
.stMarkdown>h1, .stMarkdown>h2, .stMarkdown>h3, .stMarkdown>h4, .stMarkdown>h5, .stMarkdown>h6 {
|
| 52 |
color: blue;
|
| 53 |
+
font-weight: bold;
|
| 54 |
+
font-style: italic;
|
| 55 |
}
|
| 56 |
.stAudio {
|
| 57 |
margin-top: 20px;
|
|
|
|
| 61 |
.stFileUploader>div>div>input {
|
| 62 |
border: 1px solid #4CAF50;
|
| 63 |
border-radius: 10px;
|
| 64 |
+
font-size: 16px;
|
| 65 |
}
|
| 66 |
.spinner {
|
| 67 |
border: 8px solid #f3f3f3;
|
|
|
|
| 85 |
.assistant-response {
|
| 86 |
max-height: 200px;
|
| 87 |
overflow-y: auto;
|
| 88 |
+
font-size: 16px;
|
| 89 |
}
|
| 90 |
</style>
|
| 91 |
""",
|
|
|
|
| 99 |
|
| 100 |
# Barra lateral para la navegación
|
| 101 |
st.sidebar.title("Navegación")
|
| 102 |
+
page = st.sidebar.selectbox("Selecciona una página", ["Chat Asistente", "Generador de Frases Bíblicas", "Recibir Reflexión", "La conexión"])
|
| 103 |
|
| 104 |
# Cargar el modelo de clasificación de imágenes
|
| 105 |
clasificador = pipeline("zero-shot-image-classification")
|
|
|
|
| 159 |
respuesta = response['choices'][0]['message']['content']
|
| 160 |
return respuesta
|
| 161 |
|
| 162 |
+
# Función para extraer texto de un PDF
|
| 163 |
+
def extraer_texto_pdf(pdf_path):
|
| 164 |
+
text = ""
|
| 165 |
+
with open(pdf_path, "rb") as file:
|
| 166 |
+
reader = PyPDF2.PdfFileReader(file)
|
| 167 |
+
for page_num in range(reader.numPages):
|
| 168 |
+
text += reader.getPage(page_num).extract_text()
|
| 169 |
+
return text
|
| 170 |
+
|
| 171 |
+
# Función "La conexión" para generar oraciones del PDF
|
| 172 |
+
def generar_oracion_desde_pdf():
|
| 173 |
+
pdf_path = "diario-de-oraciones.pdf" # Ruta al PDF
|
| 174 |
+
texto_pdf = extraer_texto_pdf(pdf_path)
|
| 175 |
+
prompt = f"Genera una oración inspiradora basada en el siguiente texto: {texto_pdf[:2000]}" # Limitar a los primeros 2000 caracteres
|
| 176 |
+
response = openai.ChatCompletion.create(
|
| 177 |
+
model="gpt-4",
|
| 178 |
+
messages=[{"role": "system", "content": "Eres un asistente teológico que genera oraciones inspiradoras basadas en textos."},
|
| 179 |
+
{"role": "user", "content": prompt}],
|
| 180 |
+
temperature=0.7,
|
| 181 |
+
max_tokens=250,
|
| 182 |
+
)
|
| 183 |
+
respuesta = response['choices'][0]['message']['content']
|
| 184 |
+
return respuesta
|
| 185 |
+
|
| 186 |
if page == "Chat Asistente":
|
| 187 |
# Chat con el asistente
|
| 188 |
st.subheader("🗣️ Chat con el Asistente")
|
|
|
|
| 301 |
st.markdown(audio_html, unsafe_allow_html=True)
|
| 302 |
else:
|
| 303 |
st.warning("No se encontraron reflexiones en la carpeta especificada.")
|
| 304 |
+
|
| 305 |
+
elif page == "La conexión":
|
| 306 |
+
st.subheader("🙏 La conexión: Generador de Oraciones")
|
| 307 |
+
if st.button("Generar Oración"):
|
| 308 |
+
oracion = generar_oracion_desde_pdf()
|
| 309 |
+
st.markdown(oracion)
|
| 310 |
+
audio_base64 = text_to_speech_base64(oracion)
|
| 311 |
+
audio_html = f"""
|
| 312 |
+
<audio autoplay>
|
| 313 |
+
<source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
|
| 314 |
+
</audio>
|
| 315 |
+
"""
|
| 316 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|