|
import time |
|
import gradio as gr |
|
import requests |
|
import json |
|
|
|
def slow_api_response(message, history, property_id="c0ced2220b87fc23762facf617157a4f", session_id="12345"): |
|
url = "https://data-monopolio.dev.dd360.mx/ai-assistant/v1" |
|
|
|
payload = json.dumps({ |
|
"query": message, |
|
"sessionId": session_id, |
|
"numMessages": "3", |
|
"userId": "user-01", |
|
"property_id": property_id |
|
}) |
|
|
|
headers = { |
|
'Content-Type': 'application/json' |
|
} |
|
|
|
try: |
|
|
|
response = requests.request("POST", url, headers=headers, data=payload) |
|
if response.status_code == 500: |
|
yield "Error: No existe el ID de la propiedad." |
|
|
|
else: |
|
api_response = response.text |
|
api_response = api_response.encode().decode('unicode_escape') |
|
|
|
total_time = 5 |
|
response_length = len(api_response) |
|
sleep_time = total_time / response_length if response_length > 0 else total_time |
|
|
|
if response_length > 3000: |
|
yield api_response |
|
|
|
else: |
|
for i in range(response_length): |
|
time.sleep(sleep_time) |
|
yield api_response[:i + 1] |
|
|
|
except requests.RequestException as e: |
|
yield f"Error: {str(e)}" |
|
|
|
property_id_input = gr.Textbox( |
|
label="Property ID", |
|
placeholder="Ingresa el ID de la propiedad" |
|
) |
|
|
|
session_id_input = gr.Textbox( |
|
label="Session ID", |
|
placeholder="Ingresa el ID de tu sesion" |
|
) |
|
|
|
examples = [ |
|
["hola", "c0ced2220b87fc23762facf617157a4f", "12345"], |
|
["me gustaria mas informacion de la casa en venta", "c0ced2220b87fc23762facf617157a4f", "12345"], |
|
["acepta mascotas?", "c0ced2220b87fc23762facf617157a4f", "12345"] |
|
] |
|
|
|
demo = gr.ChatInterface(fn=slow_api_response, |
|
title="AI Assistant", |
|
additional_inputs=[property_id_input, session_id_input], |
|
description="Esta app simula una conversación con un asistente virtual que conoce una propiedad.", |
|
examples=examples |
|
).launch() |