File size: 2,420 Bytes
42f970b c732a18 ab558ef c732a18 2561089 3f17b47 ab558ef 4eaf1b2 ab558ef 3f17b47 da033a6 ab558ef a0245a6 ab558ef 4eaf1b2 ab558ef da033a6 ab558ef b06e2c3 ab558ef b06e2c3 6033bc1 2b17f3a c732a18 ab558ef 3f17b47 2561089 da033a6 26325b4 da033a6 3f17b47 26325b4 2561089 3f17b47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import time
import gradio as gr
import requests
import json
def generate_session_id():
return str(uuid.uuid4())
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 #"c0ced2220b87fc23762facf617157a4f"
})
headers = {
'Content-Type': 'application/json'
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 500: # Verifica si hay un error interno del servidor
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"
)
def dynamic_examples():
examples = [
["Hola", "6072ef21835a01fee0f14ace35e5d814", generate_session_id()],
["Hola", "db674f4a5bb6b696698f2ab5825d68dd", generate_session_id()],
["Hola", "7ba0195e733b613efb3379af7cbd3613", generate_session_id()]
]
return examples
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=dynamic_examples(),
live=True
).launch() |