Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,10 +12,7 @@ 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
|
| 16 |
-
from reportlab.pdfgen import canvas
|
| 17 |
-
import csv
|
| 18 |
-
import pandas as pd
|
| 19 |
|
| 20 |
# Configuraci贸n de NLTK
|
| 21 |
nltk.download('punkt')
|
|
@@ -29,52 +26,6 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
| 29 |
# Instancia el cliente de Text-to-Speech
|
| 30 |
client = texttospeech.TextToSpeechClient()
|
| 31 |
|
| 32 |
-
# Funci贸n para leer el archivo CSV del men煤
|
| 33 |
-
def leer_menu_csv(filepath):
|
| 34 |
-
menu = pd.read_csv(filepath)
|
| 35 |
-
return menu
|
| 36 |
-
|
| 37 |
-
# Clase para manejar el pedido
|
| 38 |
-
class PedidoTool:
|
| 39 |
-
def __init__(self, menu_df):
|
| 40 |
-
self.menu_df = menu_df
|
| 41 |
-
|
| 42 |
-
def tomar_pedido(self, pedido):
|
| 43 |
-
items = pedido.split(',')
|
| 44 |
-
confirmados = []
|
| 45 |
-
for item in items:
|
| 46 |
-
if item.strip() in self.menu_df['item'].values:
|
| 47 |
-
confirmados.append(item.strip())
|
| 48 |
-
return confirmados
|
| 49 |
-
|
| 50 |
-
# Clase para procesar la orden
|
| 51 |
-
class OrdenTool:
|
| 52 |
-
def __init__(self, menu_df):
|
| 53 |
-
self.menu_df = menu_df
|
| 54 |
-
|
| 55 |
-
def procesar_orden(self, pedido):
|
| 56 |
-
items = pedido.split(',')
|
| 57 |
-
total = 0
|
| 58 |
-
for item in items:
|
| 59 |
-
price = self.menu_df[self.menu_df['item'] == item.strip()]['price'].values[0]
|
| 60 |
-
total += price
|
| 61 |
-
return total
|
| 62 |
-
|
| 63 |
-
# Funci贸n para generar el PDF de la orden
|
| 64 |
-
def generar_pdf_orden(order_details, filepath='orden_compra.pdf'):
|
| 65 |
-
c = canvas.Canvas(filepath, pagesize=letter)
|
| 66 |
-
width, height = letter
|
| 67 |
-
c.drawString(100, height - 100, "Orden de Compra")
|
| 68 |
-
y = height - 120
|
| 69 |
-
total = 0
|
| 70 |
-
for item, details in order_details.items():
|
| 71 |
-
c.drawString(100, y, f"{item}: ${details['price']}")
|
| 72 |
-
y -= 20
|
| 73 |
-
total += details['price']
|
| 74 |
-
c.drawString(100, y - 20, f"Total: ${total}")
|
| 75 |
-
c.save()
|
| 76 |
-
return filepath
|
| 77 |
-
|
| 78 |
# Funci贸n para extraer texto del PDF
|
| 79 |
def extraer_texto_pdf(archivo):
|
| 80 |
texto = ""
|
|
@@ -140,17 +91,6 @@ def obtener_respuesta(pregunta, texto_preprocesado, modelo, temperatura=0.5):
|
|
| 140 |
st.error(f"Error al comunicarse con OpenAI: {e}")
|
| 141 |
return "Lo siento, no puedo procesar tu solicitud en este momento."
|
| 142 |
|
| 143 |
-
# Definici贸n de Agentes
|
| 144 |
-
def tomar_pedido_agente(pedido, menu_df):
|
| 145 |
-
pedido_tool = PedidoTool(menu_df)
|
| 146 |
-
confirmados = pedido_tool.tomar_pedido(pedido)
|
| 147 |
-
return confirmados
|
| 148 |
-
|
| 149 |
-
def procesar_orden_agente(pedido, menu_df):
|
| 150 |
-
orden_tool = OrdenTool(menu_df)
|
| 151 |
-
total = orden_tool.procesar_orden(pedido)
|
| 152 |
-
return total
|
| 153 |
-
|
| 154 |
# Main App
|
| 155 |
def main():
|
| 156 |
# --- Dise帽o general ---
|
|
@@ -344,8 +284,31 @@ def main():
|
|
| 344 |
order_details = {item: {'price': menu_df[menu_df['item'] == item]['price'].values[0]} for item in confirmados}
|
| 345 |
pdf_path = generar_pdf_orden(order_details)
|
| 346 |
st.markdown(f"[Descargar PDF de la Orden]({pdf_path})", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
else:
|
| 348 |
-
st.warning("Por favor, ingresa
|
| 349 |
|
| 350 |
if __name__ == "__main__":
|
| 351 |
main()
|
|
|
|
| 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 leer_menu_csv, tomar_pedido_agente, procesar_orden_agente, generar_pdf_orden
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Configuraci贸n de NLTK
|
| 18 |
nltk.download('punkt')
|
|
|
|
| 26 |
# Instancia el cliente de Text-to-Speech
|
| 27 |
client = texttospeech.TextToSpeechClient()
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Funci贸n para extraer texto del PDF
|
| 30 |
def extraer_texto_pdf(archivo):
|
| 31 |
texto = ""
|
|
|
|
| 91 |
st.error(f"Error al comunicarse con OpenAI: {e}")
|
| 92 |
return "Lo siento, no puedo procesar tu solicitud en este momento."
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# Main App
|
| 95 |
def main():
|
| 96 |
# --- Dise帽o general ---
|
|
|
|
| 284 |
order_details = {item: {'price': menu_df[menu_df['item'] == item]['price'].values[0]} for item in confirmados}
|
| 285 |
pdf_path = generar_pdf_orden(order_details)
|
| 286 |
st.markdown(f"[Descargar PDF de la Orden]({pdf_path})", unsafe_allow_html=True)
|
| 287 |
+
|
| 288 |
+
# --- Chat para Agentes ---
|
| 289 |
+
st.subheader("Chat con Agentes")
|
| 290 |
+
if 'mensajes_agente' not in st.session_state:
|
| 291 |
+
st.session_state.mensajes_agente = []
|
| 292 |
+
|
| 293 |
+
for mensaje in st.session_state.mensajes_agente:
|
| 294 |
+
with st.chat_message(mensaje["role"]):
|
| 295 |
+
st.markdown(mensaje["content"])
|
| 296 |
+
|
| 297 |
+
agente_pregunta = st.text_input("Escribe tu pregunta para el agente:")
|
| 298 |
+
if st.button("Enviar al Agente"):
|
| 299 |
+
if agente_pregunta:
|
| 300 |
+
st.session_state.mensajes_agente.append({"role": "user", "content": agente_pregunta, "timestamp": time.time()})
|
| 301 |
+
with st.chat_message("user"):
|
| 302 |
+
st.markdown(agente_pregunta)
|
| 303 |
+
|
| 304 |
+
# Procesar la respuesta del agente
|
| 305 |
+
with st.spinner("El agente est谩 respondiendo..."):
|
| 306 |
+
respuesta_agente = obtener_respuesta(agente_pregunta, '', modelo="gpt-4", temperatura=0.5)
|
| 307 |
+
st.session_state.mensajes_agente.append({"role": "assistant", "content": respuesta_agente, "timestamp": time.time()})
|
| 308 |
+
with st.chat_message("assistant"):
|
| 309 |
+
st.markdown(respuesta_agente)
|
| 310 |
else:
|
| 311 |
+
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 312 |
|
| 313 |
if __name__ == "__main__":
|
| 314 |
main()
|