File size: 3,275 Bytes
fb0495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import time


from rag_agent import RAGAgent


rag_agent = RAGAgent()


class ChatBot:
    def __init__(self, rag_agent):
        self.message_history = []
        self.rag_agent = rag_agent

    def get_response(self, message):
        return self.rag_agent.get_response(message)

    def chat(self, message, history):
        time.sleep(1)
        bot_response = self.get_response(message)
        self.message_history.append((message, bot_response))
        return bot_response


def create_chat_interface(rag_agent=rag_agent):
    chatbot = ChatBot(rag_agent=rag_agent)

    custom_css = """

    #chatbot {

        direction: rtl;

        height: 400px;

    }

    .message {

        font-size: 16px;

        text-align: right;

    }

    .message-wrap {

        direction: rtl !important;

    }

    .message-wrap > div {

        direction: rtl !important;

        text-align: right !important;

    }

    .input-box {

        direction: rtl !important;

        text-align: right !important;

    }

    .container {

        direction: rtl;

    }

    .contain {

        direction: rtl !important;

    }

    .bubble {

        direction: rtl !important;

        text-align: right !important;

    }

    textarea, input {

        direction: rtl !important;

        text-align: right !important;

    }

    .user-message, .bot-message {

        direction: rtl !important;

        text-align: right !important;

    }

    """

    with gr.Blocks(css=custom_css) as interface:
        with gr.Column(elem_classes="container"):
            gr.Markdown("ืจื•ืคื ืฉื™ื ื™ื™ื ืืœืงื˜ืจื•ื ื™", rtl=True)

            chatbot_component = gr.Chatbot(
                [],
                elem_id="chatbot",
                height=400,
                rtl=True,
                elem_classes="message-wrap"
            )

            with gr.Row():
                submit_btn = gr.Button("ืฉืœื—", variant="primary")
                txt = gr.Textbox(
                    show_label=False,
                    placeholder="ื”ืงืœื“ ืืช ื”ื”ื•ื“ืขื” ืฉืœืš ื›ืืŸ...",
                    container=False,
                    elem_classes="input-box",
                    rtl=True
                )

            clear_btn = gr.Button("ื ืงื” ืฆ'ืื˜")

        def user_message(user_message, history):
            return "", history + [[user_message, None]]

        def bot_message(history):
            user_message = history[-1][0]
            bot_response = chatbot.chat(user_message, history)
            history[-1][1] = bot_response
            return history

        txt_msg = txt.submit(user_message, [txt, chatbot_component], [txt, chatbot_component], queue=False).then(
            bot_message, chatbot_component, chatbot_component
        )

        submit_btn.click(user_message, [txt, chatbot_component], [txt, chatbot_component], queue=False).then(
            bot_message, chatbot_component, chatbot_component
        )

        clear_btn.click(lambda: None, None, chatbot_component, queue=False)

    return interface


# Launch the interface
chat_interface = create_chat_interface(rag_agent=rag_agent)
chat_interface.launch(share=True)