Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import openai
|
| 3 |
from dotenv import load_dotenv
|
|
@@ -10,9 +11,9 @@ from nltk.stem import SnowballStemmer
|
|
| 10 |
import PyPDF2
|
| 11 |
import time
|
| 12 |
from google.cloud import texttospeech
|
| 13 |
-
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
| 14 |
from Historial.historial_chat import cargar_historial, guardar_historial
|
| 15 |
-
from agent_functions import menu_df, tomar_pedido_agente, procesar_orden_agente, generar_pdf_orden, obtener_respuesta
|
| 16 |
|
| 17 |
# Configuraci贸n de NLTK
|
| 18 |
nltk.download('punkt')
|
|
@@ -47,6 +48,15 @@ def preprocesar_texto(texto):
|
|
| 47 |
tokens = [stemmer.stem(word) for word in tokens]
|
| 48 |
return " ".join(tokens)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Main App
|
| 51 |
def main():
|
| 52 |
# --- Dise帽o general ---
|
|
@@ -143,10 +153,22 @@ def main():
|
|
| 143 |
st.session_state.run_webrtc = True
|
| 144 |
if st.session_state.get("run_webrtc", False):
|
| 145 |
webrtc_ctx = webrtc_streamer(
|
| 146 |
-
key="example",
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
)
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
st.markdown("---")
|
| 151 |
st.subheader("馃搫 Subir PDF")
|
| 152 |
st.info("Sube un archivo PDF y el asistente responder谩 en funci贸n de su contenido.")
|
|
@@ -242,6 +264,12 @@ def main():
|
|
| 242 |
pdf_path = generar_pdf_orden(order_details)
|
| 243 |
st.markdown(f"[Descargar PDF de la Orden]({pdf_path})", unsafe_allow_html=True)
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# --- Chat para Agentes ---
|
| 246 |
st.subheader("Chat con Agentes")
|
| 247 |
if 'mensajes_agente' not in st.session_state:
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
import openai
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 11 |
import PyPDF2
|
| 12 |
import time
|
| 13 |
from google.cloud import texttospeech
|
| 14 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 15 |
from Historial.historial_chat import cargar_historial, guardar_historial
|
| 16 |
+
from agent_functions import menu_df, tomar_pedido_agente, procesar_orden_agente, generar_pdf_orden, obtener_respuesta, generar_mensaje_automatico
|
| 17 |
|
| 18 |
# Configuraci贸n de NLTK
|
| 19 |
nltk.download('punkt')
|
|
|
|
| 48 |
tokens = [stemmer.stem(word) for word in tokens]
|
| 49 |
return " ".join(tokens)
|
| 50 |
|
| 51 |
+
# Clase para procesar audio
|
| 52 |
+
class AudioProcessor(AudioProcessorBase):
|
| 53 |
+
def __init__(self):
|
| 54 |
+
self.audio_bytes = b''
|
| 55 |
+
|
| 56 |
+
def recv(self, frame):
|
| 57 |
+
self.audio_bytes += frame.to_ndarray().tobytes()
|
| 58 |
+
return frame
|
| 59 |
+
|
| 60 |
# Main App
|
| 61 |
def main():
|
| 62 |
# --- Dise帽o general ---
|
|
|
|
| 153 |
st.session_state.run_webrtc = True
|
| 154 |
if st.session_state.get("run_webrtc", False):
|
| 155 |
webrtc_ctx = webrtc_streamer(
|
| 156 |
+
key="example",
|
| 157 |
+
mode=WebRtcMode.SENDONLY,
|
| 158 |
+
audio_receiver_size=256,
|
| 159 |
+
rtc_configuration={
|
| 160 |
+
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
|
| 161 |
+
},
|
| 162 |
+
media_stream_constraints={"audio": True},
|
| 163 |
+
audio_processor_factory=AudioProcessor,
|
| 164 |
)
|
| 165 |
|
| 166 |
+
if webrtc_ctx.audio_receiver:
|
| 167 |
+
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
|
| 168 |
+
for audio_frame in audio_frames:
|
| 169 |
+
audio_bytes = audio_frame.to_ndarray().tobytes()
|
| 170 |
+
on_audio(audio_bytes)
|
| 171 |
+
|
| 172 |
st.markdown("---")
|
| 173 |
st.subheader("馃搫 Subir PDF")
|
| 174 |
st.info("Sube un archivo PDF y el asistente responder谩 en funci贸n de su contenido.")
|
|
|
|
| 264 |
pdf_path = generar_pdf_orden(order_details)
|
| 265 |
st.markdown(f"[Descargar PDF de la Orden]({pdf_path})", unsafe_allow_html=True)
|
| 266 |
|
| 267 |
+
# Generar mensaje autom谩tico
|
| 268 |
+
mensaje_automatico = generar_mensaje_automatico(confirmados)
|
| 269 |
+
st.session_state.mensajes_agente.append({"role": "assistant", "content": mensaje_automatico, "timestamp": time.time()})
|
| 270 |
+
with st.chat_message("assistant"):
|
| 271 |
+
st.markdown(mensaje_automatico)
|
| 272 |
+
|
| 273 |
# --- Chat para Agentes ---
|
| 274 |
st.subheader("Chat con Agentes")
|
| 275 |
if 'mensajes_agente' not in st.session_state:
|