Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,66 @@
|
|
| 1 |
-
import base64
|
| 2 |
import gradio as gr
|
| 3 |
from chatbot.core import get_chat_response
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from chatbot.core import get_chat_response
|
| 3 |
|
| 4 |
+
# Lưu tin nhắn trong bộ nhớ
|
| 5 |
+
history = []
|
| 6 |
+
|
| 7 |
+
# Hàm xử lý chat
|
| 8 |
+
def chat(user_message):
|
| 9 |
+
if user_message.strip() == "":
|
| 10 |
+
return gr.update(value="", visible=True), gr.update(visible=True)
|
| 11 |
+
|
| 12 |
+
# Thêm tin nhắn của user
|
| 13 |
+
history.append(("left", user_message))
|
| 14 |
+
|
| 15 |
+
# Lấy phản hồi từ assistant (Kumiko)
|
| 16 |
+
response = get_chat_response(user_message)
|
| 17 |
+
history.append(("right", response))
|
| 18 |
+
|
| 19 |
+
# Tạo HTML để hiển thị hội thoại
|
| 20 |
+
chat_html = ""
|
| 21 |
+
for sender, msg in history:
|
| 22 |
+
align = "left" if sender == "left" else "right"
|
| 23 |
+
chat_html += f"""
|
| 24 |
+
<div style='text-align: {align}; margin: 10px;'>
|
| 25 |
+
<div style='
|
| 26 |
+
display: inline-block;
|
| 27 |
+
padding: 10px 15px;
|
| 28 |
+
border-radius: 20px;
|
| 29 |
+
max-width: 60%;
|
| 30 |
+
background-color: {"#e6f0ff" if align=="left" else "#d6f5d6"};
|
| 31 |
+
color: black;
|
| 32 |
+
font-size: 16px;
|
| 33 |
+
'>{msg}</div>
|
| 34 |
+
</div>
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
return gr.update(value=""), chat_html
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# ---------------- UI ---------------- #
|
| 41 |
+
with gr.Blocks(css="""
|
| 42 |
+
body {
|
| 43 |
+
background-image: url('https://i.imgur.com/Qr71crq.jpg');
|
| 44 |
+
background-size: cover;
|
| 45 |
+
background-attachment: fixed;
|
| 46 |
+
background-position: center;
|
| 47 |
+
}
|
| 48 |
+
.messagebox {
|
| 49 |
+
background-color: rgba(255, 255, 255, 0.8);
|
| 50 |
+
padding: 16px;
|
| 51 |
+
border-radius: 20px;
|
| 52 |
+
margin: 8px;
|
| 53 |
+
}
|
| 54 |
+
""") as demo:
|
| 55 |
+
gr.Markdown("<h1 style='text-align: center;'>🎺 Kumiko Assistant v1.5 📯</h1>")
|
| 56 |
+
|
| 57 |
+
chat_display = gr.HTML()
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
user_input = gr.Textbox(placeholder="Nhập tin nhắn của bạn...", show_label=False, lines=2)
|
| 61 |
+
send_btn = gr.Button("Gửi")
|
| 62 |
+
|
| 63 |
+
send_btn.click(fn=chat, inputs=user_input, outputs=[user_input, chat_display])
|
| 64 |
+
user_input.submit(fn=chat, inputs=user_input, outputs=[user_input, chat_display])
|
| 65 |
+
|
| 66 |
+
demo.launch()
|