Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,6 @@ from fpdf import FPDF
|
|
| 12 |
import streamlit as st
|
| 13 |
import requests
|
| 14 |
from google.cloud import texttospeech
|
| 15 |
-
import io
|
| 16 |
-
from docx import Document
|
| 17 |
|
| 18 |
nltk.download('punkt', quiet=True)
|
| 19 |
nltk.download('stopwords', quiet=True)
|
|
@@ -302,7 +300,7 @@ def buscar_datos_guardados():
|
|
| 302 |
|
| 303 |
# Link to download the file
|
| 304 |
try:
|
| 305 |
-
with open(os.path.join(carpeta, archivo_seleccionado), 'rb') as file:
|
| 306 |
st.download_button(
|
| 307 |
label="📥 Descargar Archivo TXT",
|
| 308 |
data=file,
|
|
@@ -459,28 +457,92 @@ def mostrar_recomendaciones():
|
|
| 459 |
st.title("⭐ Recomendaciones")
|
| 460 |
st.write("Aquí puedes encontrar recomendaciones y consejos útiles.")
|
| 461 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
def manejar_pregunta_usuario(pregunta_usuario, archivo_pdf=None):
|
| 463 |
st.session_state['mensajes_chat'].append({"role": "user", "content": pregunta_usuario})
|
| 464 |
with st.chat_message("user"):
|
| 465 |
st.markdown(pregunta_usuario)
|
| 466 |
|
| 467 |
texto_preprocesado = ""
|
|
|
|
| 468 |
if archivo_pdf:
|
| 469 |
texto_pdf = extraer_texto_pdf(archivo_pdf)
|
| 470 |
texto_preprocesado = preprocesar_texto(texto_pdf)
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
instrucciones_preprocesado = preprocesar_texto(instrucciones_pdf)
|
| 476 |
-
|
| 477 |
-
# Combinar el texto preprocesado del PDF cargado con las instrucciones predefinidas
|
| 478 |
-
contexto = instrucciones_preprocesado + "\n" + texto_preprocesado
|
| 479 |
|
| 480 |
# Obtener respuesta del modelo usando Assistant ID si está presente
|
| 481 |
assistant_id = st.session_state.get('assistant_id', '')
|
| 482 |
if assistant_id:
|
| 483 |
-
prompt = f"{
|
| 484 |
response = openai.ChatCompletion.create(
|
| 485 |
model=st.session_state['modelo'],
|
| 486 |
messages=[
|
|
@@ -494,7 +556,7 @@ def manejar_pregunta_usuario(pregunta_usuario, archivo_pdf=None):
|
|
| 494 |
else:
|
| 495 |
respuesta = obtener_respuesta(
|
| 496 |
pregunta_usuario,
|
| 497 |
-
|
| 498 |
st.session_state['modelo'],
|
| 499 |
st.session_state['temperatura'],
|
| 500 |
assistant_id
|
|
@@ -720,72 +782,5 @@ def mostrar_pagina_principal():
|
|
| 720 |
if pregunta_usuario:
|
| 721 |
manejar_pregunta_usuario(pregunta_usuario, archivo_pdf)
|
| 722 |
|
| 723 |
-
def mostrar_galatea_asistente():
|
| 724 |
-
st.markdown(
|
| 725 |
-
"""
|
| 726 |
-
<style>
|
| 727 |
-
#video-container {
|
| 728 |
-
position: relative;
|
| 729 |
-
width: 100%;
|
| 730 |
-
height: 40vh;
|
| 731 |
-
background-color: lightblue;
|
| 732 |
-
overflow: hidden;
|
| 733 |
-
display: flex;
|
| 734 |
-
justify-content: center;
|
| 735 |
-
align-items: center;
|
| 736 |
-
}
|
| 737 |
-
#background-video {
|
| 738 |
-
width: 100%;
|
| 739 |
-
height: auto;
|
| 740 |
-
}
|
| 741 |
-
#chat-container {
|
| 742 |
-
margin-top: 20px;
|
| 743 |
-
width: 100%;
|
| 744 |
-
display: flex;
|
| 745 |
-
flex-direction: column;
|
| 746 |
-
justify-content: center;
|
| 747 |
-
align-items: center;
|
| 748 |
-
}
|
| 749 |
-
.chat-message {
|
| 750 |
-
background: rgba(255, 255, 255, 0.8);
|
| 751 |
-
border-radius: 10px;
|
| 752 |
-
padding: 10px;
|
| 753 |
-
margin-bottom: 10px;
|
| 754 |
-
max-width: 70%;
|
| 755 |
-
}
|
| 756 |
-
.chat-message.user {
|
| 757 |
-
align-self: flex-start;
|
| 758 |
-
}
|
| 759 |
-
.chat-message.assistant {
|
| 760 |
-
align-self: flex-end;
|
| 761 |
-
background: rgba(0, 123, 255, 0.8);
|
| 762 |
-
color: white;
|
| 763 |
-
}
|
| 764 |
-
</style>
|
| 765 |
-
<div id="video-container">
|
| 766 |
-
<video id="background-video" autoplay loop muted playsinline>
|
| 767 |
-
<source src="https://cdn.leonardo.ai/users/645c3d5c-ca1b-4ce8-aefa-a091494e0d09/generations/aaa569a1-8952-4e8e-9c3c-71cc66e62f04/aaa569a1-8952-4e8e-9c3c-71cc66e62f04.mp4" type="video/mp4">
|
| 768 |
-
</video>
|
| 769 |
-
</div>
|
| 770 |
-
<div id="chat-container">
|
| 771 |
-
""",
|
| 772 |
-
unsafe_allow_html=True
|
| 773 |
-
)
|
| 774 |
-
|
| 775 |
-
for mensaje in st.session_state['mensajes_chat']:
|
| 776 |
-
clase = "user" if mensaje["role"] == "user" else "assistant"
|
| 777 |
-
st.markdown(f'<div class="chat-message {clase}">{mensaje["content"]}</div>', unsafe_allow_html=True)
|
| 778 |
-
|
| 779 |
-
pregunta_usuario = st.text_input("Escribe tu pregunta aquí:", key='unique_chat_input_key', value=st.session_state['transcripcion_voz'])
|
| 780 |
-
if st.button("Enviar Pregunta"):
|
| 781 |
-
manejar_pregunta_usuario(pregunta_usuario)
|
| 782 |
-
|
| 783 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 784 |
-
|
| 785 |
-
if st.session_state['imagen_asistente']:
|
| 786 |
-
st.image(st.session_state['imagen_asistente'], use_column_width=True)
|
| 787 |
-
else:
|
| 788 |
-
st.warning("No se ha cargado ninguna imagen. Por favor, carga una imagen en la página principal.")
|
| 789 |
-
|
| 790 |
if __name__ == "__main__":
|
| 791 |
main()
|
|
|
|
| 12 |
import streamlit as st
|
| 13 |
import requests
|
| 14 |
from google.cloud import texttospeech
|
|
|
|
|
|
|
| 15 |
|
| 16 |
nltk.download('punkt', quiet=True)
|
| 17 |
nltk.download('stopwords', quiet=True)
|
|
|
|
| 300 |
|
| 301 |
# Link to download the file
|
| 302 |
try:
|
| 303 |
+
with open(os.path.join(carpeta, archivo_seleccionado), 'rb') as file):
|
| 304 |
st.download_button(
|
| 305 |
label="📥 Descargar Archivo TXT",
|
| 306 |
data=file,
|
|
|
|
| 457 |
st.title("⭐ Recomendaciones")
|
| 458 |
st.write("Aquí puedes encontrar recomendaciones y consejos útiles.")
|
| 459 |
|
| 460 |
+
def mostrar_galatea_asistente():
|
| 461 |
+
st.markdown(
|
| 462 |
+
"""
|
| 463 |
+
<style>
|
| 464 |
+
#video-container {
|
| 465 |
+
position: relative;
|
| 466 |
+
width: 100%;
|
| 467 |
+
height: 40vh;
|
| 468 |
+
background-color: lightblue;
|
| 469 |
+
overflow: hidden;
|
| 470 |
+
display: flex;
|
| 471 |
+
justify-content: center;
|
| 472 |
+
align-items: center;
|
| 473 |
+
}
|
| 474 |
+
#background-video {
|
| 475 |
+
width: 100%;
|
| 476 |
+
height: auto;
|
| 477 |
+
}
|
| 478 |
+
#chat-container {
|
| 479 |
+
margin-top: 20px;
|
| 480 |
+
width: 100%;
|
| 481 |
+
display: flex;
|
| 482 |
+
flex-direction: column;
|
| 483 |
+
justify-content: center;
|
| 484 |
+
align-items: center;
|
| 485 |
+
}
|
| 486 |
+
.chat-message {
|
| 487 |
+
background: rgba(255, 255, 255, 0.8);
|
| 488 |
+
border-radius: 10px;
|
| 489 |
+
padding: 10px;
|
| 490 |
+
margin-bottom: 10px;
|
| 491 |
+
max-width: 70%;
|
| 492 |
+
}
|
| 493 |
+
.chat-message.user {
|
| 494 |
+
align-self: flex-start;
|
| 495 |
+
}
|
| 496 |
+
.chat-message.assistant {
|
| 497 |
+
align-self: flex-end;
|
| 498 |
+
background: rgba(0, 123, 255, 0.8);
|
| 499 |
+
color: white;
|
| 500 |
+
}
|
| 501 |
+
</style>
|
| 502 |
+
<div id="video-container">
|
| 503 |
+
<video id="background-video" autoplay loop muted playsinline>
|
| 504 |
+
<source src="https://cdn.leonardo.ai/users/645c3d5c-ca1b-4ce8-aefa-a091494e0d09/generations/aaa569a1-8952-4e8e-9c3c-71cc66e62f04/aaa569a1-8952-4e8e-9c3c-71cc66e62f04.mp4" type="video/mp4">
|
| 505 |
+
</video>
|
| 506 |
+
</div>
|
| 507 |
+
<div id="chat-container">
|
| 508 |
+
""",
|
| 509 |
+
unsafe_allow_html=True
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
for mensaje in st.session_state['mensajes_chat']:
|
| 513 |
+
clase = "user" if mensaje["role"] == "user" else "assistant"
|
| 514 |
+
st.markdown(f'<div class="chat-message {clase}">{mensaje["content"]}</div>', unsafe_allow_html=True)
|
| 515 |
+
|
| 516 |
+
pregunta_usuario = st.text_input("Escribe tu pregunta aquí:", key='unique_chat_input_key', value=st.session_state['transcripcion_voz'])
|
| 517 |
+
if st.button("Enviar Pregunta"):
|
| 518 |
+
manejar_pregunta_usuario(pregunta_usuario)
|
| 519 |
+
|
| 520 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 521 |
+
|
| 522 |
+
if st.session_state['imagen_asistente']:
|
| 523 |
+
st.image(st.session_state['imagen_asistente'], use_column_width=True)
|
| 524 |
+
else:
|
| 525 |
+
st.warning("No se ha cargado ninguna imagen. Por favor, carga una imagen en la página principal.")
|
| 526 |
+
|
| 527 |
def manejar_pregunta_usuario(pregunta_usuario, archivo_pdf=None):
|
| 528 |
st.session_state['mensajes_chat'].append({"role": "user", "content": pregunta_usuario})
|
| 529 |
with st.chat_message("user"):
|
| 530 |
st.markdown(pregunta_usuario)
|
| 531 |
|
| 532 |
texto_preprocesado = ""
|
| 533 |
+
|
| 534 |
if archivo_pdf:
|
| 535 |
texto_pdf = extraer_texto_pdf(archivo_pdf)
|
| 536 |
texto_preprocesado = preprocesar_texto(texto_pdf)
|
| 537 |
+
else:
|
| 538 |
+
with open(os.path.join("assets", "instrucciones.pdf"), "rb") as file:
|
| 539 |
+
texto_pdf = extraer_texto_pdf(file)
|
| 540 |
+
texto_preprocesado = preprocesar_texto(texto_pdf)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
# Obtener respuesta del modelo usando Assistant ID si está presente
|
| 543 |
assistant_id = st.session_state.get('assistant_id', '')
|
| 544 |
if assistant_id:
|
| 545 |
+
prompt = f"{texto_preprocesado}\n\n{pregunta_usuario}"
|
| 546 |
response = openai.ChatCompletion.create(
|
| 547 |
model=st.session_state['modelo'],
|
| 548 |
messages=[
|
|
|
|
| 556 |
else:
|
| 557 |
respuesta = obtener_respuesta(
|
| 558 |
pregunta_usuario,
|
| 559 |
+
texto_preprocesado,
|
| 560 |
st.session_state['modelo'],
|
| 561 |
st.session_state['temperatura'],
|
| 562 |
assistant_id
|
|
|
|
| 782 |
if pregunta_usuario:
|
| 783 |
manejar_pregunta_usuario(pregunta_usuario, archivo_pdf)
|
| 784 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
if __name__ == "__main__":
|
| 786 |
main()
|