Spaces:
Build error
Build error
Update agent_functions.py
Browse files- agent_functions.py +44 -0
agent_functions.py
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
|
|
| 1 |
# agent_functions.py
|
| 2 |
import pandas as pd
|
| 3 |
from reportlab.lib.pagesizes import letter
|
| 4 |
from reportlab.pdfgen import canvas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class PedidoTool:
|
| 7 |
def __init__(self, menu_df):
|
|
@@ -45,6 +53,42 @@ def generar_pdf_orden(order_details, filepath='orden_compra.pdf'):
|
|
| 45 |
c.save()
|
| 46 |
return filepath
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def tomar_pedido_agente(pedido, menu_df):
|
| 49 |
pedido_tool = PedidoTool(menu_df)
|
| 50 |
confirmados = pedido_tool.tomar_pedido(pedido)
|
|
|
|
| 1 |
+
|
| 2 |
# agent_functions.py
|
| 3 |
import pandas as pd
|
| 4 |
from reportlab.lib.pagesizes import letter
|
| 5 |
from reportlab.pdfgen import canvas
|
| 6 |
+
import openai
|
| 7 |
+
import tempfile
|
| 8 |
+
from google.cloud import texttospeech
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# Instancia el cliente de Text-to-Speech
|
| 12 |
+
client = texttospeech.TextToSpeechClient()
|
| 13 |
|
| 14 |
class PedidoTool:
|
| 15 |
def __init__(self, menu_df):
|
|
|
|
| 53 |
c.save()
|
| 54 |
return filepath
|
| 55 |
|
| 56 |
+
def obtener_respuesta(pregunta, texto_preprocesado, modelo, temperatura=0.5):
|
| 57 |
+
try:
|
| 58 |
+
response = openai.ChatCompletion.create(
|
| 59 |
+
model=modelo,
|
| 60 |
+
messages=[
|
| 61 |
+
{"role": "system", "content": "Actua como Ana una asesora de ventas del restaurante Sazon Burguer, tienes un tono muy amable y cordial"},
|
| 62 |
+
{"role": "user", "content": f"{pregunta}\n\nContexto: {texto_preprocesado}"}
|
| 63 |
+
],
|
| 64 |
+
temperature=temperatura
|
| 65 |
+
)
|
| 66 |
+
respuesta = response.choices[0].message['content'].strip()
|
| 67 |
+
|
| 68 |
+
# Configura la solicitud de síntesis de voz
|
| 69 |
+
input_text = texttospeech.SynthesisInput(text=respuesta)
|
| 70 |
+
voice = texttospeech.VoiceSelectionParams(
|
| 71 |
+
language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.MALE,
|
| 72 |
+
)
|
| 73 |
+
audio_config = texttospeech.AudioConfig(
|
| 74 |
+
audio_encoding=texttospeech.AudioEncoding.MP3
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Realiza la solicitud de síntesis de voz
|
| 78 |
+
response = client.synthesize_speech(
|
| 79 |
+
input=input_text, voice=voice, audio_config=audio_config
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Guarda el audio en un archivo temporal
|
| 83 |
+
audio_path = tempfile.mktemp(suffix=".mp3")
|
| 84 |
+
with open(audio_path, "wb") as audio_file:
|
| 85 |
+
audio_file.write(response.audio_content)
|
| 86 |
+
|
| 87 |
+
return respuesta, audio_path
|
| 88 |
+
|
| 89 |
+
except openai.OpenAIError as e:
|
| 90 |
+
return f"Error al comunicarse con OpenAI: {e}", None
|
| 91 |
+
|
| 92 |
def tomar_pedido_agente(pedido, menu_df):
|
| 93 |
pedido_tool = PedidoTool(menu_df)
|
| 94 |
confirmados = pedido_tool.tomar_pedido(pedido)
|