Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
|
4 |
+
|
5 |
+
from rag_agent import RAGAgent
|
6 |
+
|
7 |
+
|
8 |
+
rag_agent = RAGAgent()
|
9 |
+
|
10 |
+
|
11 |
+
class ChatBot:
|
12 |
+
def __init__(self, rag_agent):
|
13 |
+
self.message_history = []
|
14 |
+
self.rag_agent = rag_agent
|
15 |
+
|
16 |
+
def get_response(self, message):
|
17 |
+
return self.rag_agent.get_response(message)
|
18 |
+
|
19 |
+
def chat(self, message, history):
|
20 |
+
time.sleep(1)
|
21 |
+
bot_response = self.get_response(message)
|
22 |
+
self.message_history.append((message, bot_response))
|
23 |
+
return bot_response
|
24 |
+
|
25 |
+
|
26 |
+
def create_chat_interface(rag_agent=rag_agent):
|
27 |
+
chatbot = ChatBot(rag_agent=rag_agent)
|
28 |
+
|
29 |
+
custom_css = """
|
30 |
+
#chatbot {
|
31 |
+
direction: rtl;
|
32 |
+
height: 400px;
|
33 |
+
}
|
34 |
+
.message {
|
35 |
+
font-size: 16px;
|
36 |
+
text-align: right;
|
37 |
+
}
|
38 |
+
.message-wrap {
|
39 |
+
direction: rtl !important;
|
40 |
+
}
|
41 |
+
.message-wrap > div {
|
42 |
+
direction: rtl !important;
|
43 |
+
text-align: right !important;
|
44 |
+
}
|
45 |
+
.input-box {
|
46 |
+
direction: rtl !important;
|
47 |
+
text-align: right !important;
|
48 |
+
}
|
49 |
+
.container {
|
50 |
+
direction: rtl;
|
51 |
+
}
|
52 |
+
.contain {
|
53 |
+
direction: rtl !important;
|
54 |
+
}
|
55 |
+
.bubble {
|
56 |
+
direction: rtl !important;
|
57 |
+
text-align: right !important;
|
58 |
+
}
|
59 |
+
textarea, input {
|
60 |
+
direction: rtl !important;
|
61 |
+
text-align: right !important;
|
62 |
+
}
|
63 |
+
.user-message, .bot-message {
|
64 |
+
direction: rtl !important;
|
65 |
+
text-align: right !important;
|
66 |
+
}
|
67 |
+
"""
|
68 |
+
|
69 |
+
with gr.Blocks(css=custom_css) as interface:
|
70 |
+
with gr.Column(elem_classes="container"):
|
71 |
+
gr.Markdown("ืจืืคื ืฉืื ืืื ืืืงืืจืื ื", rtl=True)
|
72 |
+
|
73 |
+
chatbot_component = gr.Chatbot(
|
74 |
+
[],
|
75 |
+
elem_id="chatbot",
|
76 |
+
height=400,
|
77 |
+
rtl=True,
|
78 |
+
elem_classes="message-wrap"
|
79 |
+
)
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
submit_btn = gr.Button("ืฉืื", variant="primary")
|
83 |
+
txt = gr.Textbox(
|
84 |
+
show_label=False,
|
85 |
+
placeholder="ืืงืื ืืช ืืืืืขื ืฉืื ืืื...",
|
86 |
+
container=False,
|
87 |
+
elem_classes="input-box",
|
88 |
+
rtl=True
|
89 |
+
)
|
90 |
+
|
91 |
+
clear_btn = gr.Button("ื ืงื ืฆ'ืื")
|
92 |
+
|
93 |
+
def user_message(user_message, history):
|
94 |
+
return "", history + [[user_message, None]]
|
95 |
+
|
96 |
+
def bot_message(history):
|
97 |
+
user_message = history[-1][0]
|
98 |
+
bot_response = chatbot.chat(user_message, history)
|
99 |
+
history[-1][1] = bot_response
|
100 |
+
return history
|
101 |
+
|
102 |
+
txt_msg = txt.submit(user_message, [txt, chatbot_component], [txt, chatbot_component], queue=False).then(
|
103 |
+
bot_message, chatbot_component, chatbot_component
|
104 |
+
)
|
105 |
+
|
106 |
+
submit_btn.click(user_message, [txt, chatbot_component], [txt, chatbot_component], queue=False).then(
|
107 |
+
bot_message, chatbot_component, chatbot_component
|
108 |
+
)
|
109 |
+
|
110 |
+
clear_btn.click(lambda: None, None, chatbot_component, queue=False)
|
111 |
+
|
112 |
+
return interface
|
113 |
+
|
114 |
+
|
115 |
+
# Launch the interface
|
116 |
+
chat_interface = create_chat_interface(rag_agent=rag_agent)
|
117 |
+
chat_interface.launch(share=True)
|