|
import time |
|
import gradio as gr |
|
import requests |
|
import json |
|
import uuid |
|
|
|
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 |
|
}) |
|
|
|
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 |
|
|
|
print(api_response) |
|
|
|
|
|
response = json.loads(api_response) |
|
api_response = response['next_actions'][0].get('value') |
|
print(api_response) |
|
|
|
|
|
total_time = 5 |
|
response_length = len(api_response) |
|
sleep_time = total_time / response_length if response_length > 0 else total_time |
|
|
|
if sleep_time > 0.05: |
|
sleep_time = 0.05 |
|
|
|
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","development_one", generate_session_id()], |
|
["Hola","6072ef21835a01fee0f14ace35e5d814", generate_session_id()], |
|
["Hola","db674f4a5bb6b696698f2ab5825d68dd", generate_session_id()], |
|
["Hola","7ba0195e733b613efb3379af7cbd3613", generate_session_id()], |
|
["Hola","354d094e10653b567a98a3c22496fa05", 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() |
|
).launch() |
|
|