File size: 2,565 Bytes
3740bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec8bc3b
3740bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
import uuid
from typing import List

import gradio as gr


def respond(
    message: str, history: List, session_id: str, intent_classifier, retriever
):
    """
    Gradio's main response function
    
    Args:
        message: User's message
        history: Chat history
        session_id: Session identifier
        intent_classifier: IntentClassifier instance
        retriever: Vector store retriever instance
    
    Returns:
        Tuple of (empty string for input box, updated history)
    """
    # Import here to avoid circular imports
    from app import get_context_and_answer
    
    if not session_id:
        session_id = str(uuid.uuid4())

    bot_response = get_context_and_answer(
        message, history, session_id, intent_classifier, retriever
    )
    history.append([message, bot_response])

    return "", history


def create_interface(intent_classifier, retriever):
    """
    Create Gradio interface
    
    Args:
        intent_classifier: IntentClassifier instance
        retriever: Vector store retriever instance
    
    Returns:
        Gradio Blocks interface
    """
    with gr.Blocks() as demo:
        gr.Markdown("""
        # ASKXENO
        **Welcome to XENO AI Support!**
        
        I can help you with questions about XENO financial services including:
        - Account management and setup
        - Transaction processes and fees
        - Platform features and troubleshooting
        - General service information
        
        *Simply type your question below to get started!*
        """)

        # Hidden state for session
        session_id_box = gr.Textbox(
            label="Session ID", value=str(uuid.uuid4()), visible=False
        )

        chatbot = gr.Chatbot(
            label="XENO Assistant", bubble_full_width=False, height=450
        )

        with gr.Row():
            msg = gr.Textbox(
                label="Your Message",
                placeholder="Type your question here...",
                scale=4,
            )
            send_button = gr.Button("Send", variant="primary", scale=1)

        # Chat Event Listeners - Pass components to respond function
        send_button.click(
            lambda msg, chat, sid: respond(msg, chat, sid, intent_classifier, retriever),
            [msg, chatbot, session_id_box],
            [msg, chatbot],
        )
        msg.submit(
            lambda msg, chat, sid: respond(msg, chat, sid, intent_classifier, retriever),
            [msg, chatbot, session_id_box],
            [msg, chatbot],
        )

    return demo