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

Fix lỗi app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -50
app.py CHANGED
@@ -1,66 +1,91 @@
 
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ử 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()
 
 
 
1
+ import base64
2
  import gradio as gr
3
  from chatbot.core import get_chat_response
4
 
5
+ # ------------------- Load local assets -------------------
6
+ def load_image_as_base64(image_path):
7
+ with open(image_path, "rb") as img_file:
8
+ img_bytes = img_file.read()
9
+ return base64.b64encode(img_bytes).decode("utf-8")
10
+
11
+ # Load background & avatar
12
+ background_base64 = load_image_as_base64("background.jpg") # ảnh nền
13
+ avatar_base64 = load_image_as_base64("avatar.png") # ảnh Kumiko
14
 
15
+ # ------------------- Chat logic -------------------
16
+ history = []
 
 
17
 
18
+ def chat(user_input):
19
+ if not user_input.strip():
20
+ return "", ""
21
 
22
+ # Append user message (bên phải)
23
+ history.append(("right", user_input))
 
24
 
25
+ # Get Kumiko response (bên trái)
26
+ response = get_chat_response(user_input)
27
+ history.append(("left", response))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Render chat HTML
30
+ rendered = ""
31
+ for side, msg in history:
32
+ if side == "left":
33
+ # Kumiko (trái, có avatar)
34
+ rendered += f"""
35
+ <div style='text-align: left; margin: 10px;'>
36
+ <div style='display: flex; align-items: flex-end; gap: 10px;'>
37
+ <img src="data:image/png;base64,{avatar_base64}" width="40" style="border-radius: 50%;" />
38
+ <div style='
39
+ display: inline-block;
40
+ padding: 10px 15px;
41
+ border-radius: 20px;
42
+ background-color: #f0f0f0cc;
43
+ max-width: 60%;
44
+ font-size: 16px;
45
+ '>{msg}</div>
46
+ </div>
47
+ </div>
48
+ """
49
+ else:
50
+ # User (phải, không avatar)
51
+ rendered += f"""
52
+ <div style='text-align: right; margin: 10px;'>
53
+ <div style='
54
+ display: inline-block;
55
+ padding: 10px 15px;
56
+ border-radius: 20px;
57
+ background-color: #d1f0ffcc;
58
+ max-width: 60%;
59
+ font-size: 16px;
60
+ '>{msg}</div>
61
+ </div>
62
+ """
63
 
64
+ return "", rendered
65
 
66
+ # ------------------- Gradio UI -------------------
67
+ with gr.Blocks(css=f"""
68
+ body {{
69
+ background-image: url("data:image/jpg;base64,{background_base64}");
70
  background-size: cover;
 
71
  background-position: center;
72
+ background-attachment: fixed;
73
+ }}
 
 
 
 
 
74
  """) as demo:
75
+ gr.HTML(f"""
76
+ <div style="text-align:center;">
77
+ <h1>🎺 Kumiko v1.5 Assistant 📯</h1>
78
+ <p>Một trợ lý AI dễ thương từ Hibike! Euphonium, sẵn sàng trò chuyện cùng bạn!</p>
79
+ <img src="data:image/png;base64,{avatar_base64}" width="100px" style="border-radius: 100px;" />
80
+ </div>
81
+ """)
82
 
83
+ chat_html = gr.HTML()
84
+ txt = gr.Textbox(placeholder="Nhập đó...", show_label=False)
 
85
 
86
+ txt.submit(chat, inputs=txt, outputs=[txt, chat_html])
87
+ txt.submit(lambda: "", None, txt) # Clear textbox sau khi gửi
88
 
89
+ # ------------------- Launch -------------------
90
+ if __name__ == "__main__":
91
+ demo.launch()