anhkhoiphan commited on
Commit
c67bfcf
·
verified ·
1 Parent(s): 1ff0b3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -36
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
- # ------------------- Load avatar as base64 -------------------
6
- def load_image_as_base64(image_path):
7
- with open(image_path, "rb") as img_file:
8
- img_bytes = img_file.read()
9
- encoded = base64.b64encode(img_bytes).decode("utf-8")
10
- return f"data:image/png;base64,{encoded}"
11
-
12
- avatar_base64 = load_image_as_base64("avatar.png")
13
-
14
- # ------------------- Chat logic -------------------
15
- def chat(user_input, history=[]):
16
- response = get_chat_response(user_input)
17
- return history, history
18
-
19
- # ------------------- Gradio UI -------------------
20
- with gr.Blocks(theme="shivi/calm_seafoam") as demo:
21
- gr.Markdown(f"""
22
- <div style="text-align:center;">
23
- <h1>🎺 Kumiko v1.5 Assistant 📯</h1>
24
- <p>Một trợ lý AI dễ thương từ <i>Hibike! Euphonium</i>, sẵn sàng trò chuyện cùng bạn!</p>
25
- <img src="{avatar_base64}" width="120px" style="border-radius: 100px;" />
26
- </div>
27
- """)
28
-
29
- chatbot = gr.Chatbot(label="Kumiko Chat", show_label=True)
30
- msg = gr.Textbox(label="Nhập tin nhắn của bạn...", placeholder="Gõ gì đó nhé...")
31
-
32
- state = gr.State([]) # giữ lịch sử
33
-
34
- msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
35
- msg.submit(lambda: "", None, msg) # Clear textbox sau khi gửi
36
-
37
- # ------------------- Launch -------------------
38
- if __name__ == "__main__":
39
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()