File size: 2,848 Bytes
42f970b
c732a18
ab558ef
 
72b9895
c732a18
2561089
 
 
3f17b47
ab558ef
4eaf1b2
ab558ef
 
3f17b47
da033a6
ab558ef
a0245a6
ab558ef
 
 
 
 
4eaf1b2
ab558ef
da033a6
ab558ef
b06e2c3
 
ab558ef
b06e2c3
 
df6345d
66dc585
032d170
22d38ae
 
df6345d
22d38ae
66dc585
b06e2c3
6033bc1
 
 
 
b537a99
 
 
2b17f3a
 
 
 
 
 
 
c732a18
ab558ef
 
 
3f17b47
 
 
 
 
 
 
 
 
 
dfa43d7
 
c809a8b
2a50542
 
93584d3
 
dfa43d7
 
da033a6
26325b4
da033a6
de86020
26325b4
dfa43d7
1cae2db
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
74
75
76
77
78
79
80
81
82
83
84
85
86
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 #"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')
            print(api_response)

            #if property_id == 'development_one':
            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()