File size: 3,863 Bytes
3cdce90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import uuid
from typing import List

import gradio as gr

from src.logger import log_feedback


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(theme=gr.themes.Soft()) 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)

        # ===== FEEDBACK SECTION =====
        with gr.Row():
            with gr.Accordion("Rate this response / Flag Issue", open=False):
                with gr.Row():
                    thumbs_up = gr.Button("👍 Good Answer")
                    thumbs_down = gr.Button("👎 Bad / Flag")

                feedback_reason = gr.Textbox(
                    label="Reason ", placeholder="E.g., Incorrect fees, hallucination,"
                )
                feedback_status = gr.Label(value="", label="Status", show_label=False)

        # Feedback Event Listeners
        # Logic: If Thumbs Up is clicked, send 'Positive'. If Textbox is empty, reason defaults to "Good".
        thumbs_up.click(
            fn=lambda h, s, r: log_feedback("Positive", r if r else "Good", h, s),
            inputs=[chatbot, session_id_box, feedback_reason],
            outputs=[feedback_status],
        )

        # Logic: If Thumbs Down is clicked, send 'Negative' with the content of the textbox.
        thumbs_down.click(
            fn=lambda r, h, s: log_feedback("Negative", r, h, s),
            inputs=[feedback_reason, chatbot, session_id_box],
            outputs=[feedback_status],
        )
        # =============================

        # 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