-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
158 lines (140 loc) · 5.13 KB
/
chat.py
File metadata and controls
158 lines (140 loc) · 5.13 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import flet as ft
from assistant import SmartGurucool
class Message():
def __init__(self, user_name: str, text: str, message_type: str):
self.user_name = user_name
self.text = text
self.message_type = message_type
class ChatMessage(ft.Row):
def __init__(self, message: Message):
super().__init__()
self.vertical_alignment="start"
self.controls=[
ft.CircleAvatar(
content=ft.Text(self.get_initials(message.user_name)),
color=ft.colors.WHITE,
bgcolor=self.get_avatar_color(message.user_name),
),
ft.Column(
[
ft.Text(message.user_name, weight="bold"),
ft.Text(message.text, selectable=True, width=500),
],
tight=True,
spacing=5,
),
]
def get_initials(self, user_name: str):
return user_name[:1].capitalize()
def get_avatar_color(self, user_name: str):
colors_lookup = [
ft.colors.AMBER,
ft.colors.BLUE,
ft.colors.BROWN,
ft.colors.CYAN,
ft.colors.GREEN,
ft.colors.INDIGO,
ft.colors.LIME,
ft.colors.ORANGE,
ft.colors.PINK,
ft.colors.PURPLE,
ft.colors.RED,
ft.colors.TEAL,
ft.colors.YELLOW,
]
return colors_lookup[hash(user_name) % len(colors_lookup)]
def main(page: ft.Page):
# page.horizontal_alignment = "stretch"
page.title = "SmartGuru AI"
page.theme_mode = "light"
page.fonts = {
"organical": "fonts/organical.ttf"
}
# creating the object of SmartGurucool class
smart_guru = SmartGurucool()
def join_chat_click(e):
if not join_user_name.value:
join_user_name.error_text = "We need to know our names first!"
join_user_name.update()
else:
page.session.set("user_name", join_user_name.value)
page.dialog.open = False
new_message.prefix = ft.Text(f"{join_user_name.value}: ")
page.pubsub.send_all(Message(user_name=join_user_name.value, text=f"{join_user_name.value} has joined the chat.", message_type="login_message"))
page.update()
def send_message_click(e):
if new_message.value != "":
# sending the user input
page.pubsub.send_all(Message(page.session.get("user_name"), new_message.value, message_type="chat_message"))
# asking user to wait till we get our response
page.pubsub.send_all(Message(user_name="SmartGuru", text="SmartGuru is getting the response for you...", message_type="login_message"))
# fetching the SmartGuru AI response
ai_response = smart_guru.SmartGuruResponse(str(new_message.value))
page.pubsub.send_all(Message("SmartGuru", str(ai_response).lstrip(), message_type="chat_message"))
new_message.value = ""
new_message.focus()
page.update()
def on_message(message: Message):
if message.message_type == "chat_message":
m = ChatMessage(message)
elif message.message_type == "login_message":
m = ft.Text(message.text, italic=True, color=ft.colors.BLACK45, size=12)
chat.controls.append(m)
page.update()
page.pubsub.subscribe(on_message)
# A dialog asking for a user display name
join_user_name = ft.TextField(
label="Tell me your name...",
autofocus=True,
on_submit=join_chat_click,
)
page.dialog = ft.AlertDialog(
open=True,
modal=True,
title=ft.Text("Welcome!"),
content=ft.Column([join_user_name], width=300, height=70, tight=True),
actions=[ft.ElevatedButton(text="Join chat", on_click=join_chat_click)],
actions_alignment="end",
)
# Chat messages
chat = ft.ListView(
expand=True,
spacing=10,
auto_scroll=True,
)
# A new message entry form
new_message = ft.TextField(
hint_text="Write a message...",
autofocus=True,
shift_enter=True,
min_lines=1,
max_lines=5,
filled=True,
expand=True,
border_radius=20,
on_submit=send_message_click,
border_color=ft.colors.BLUE
)
# Add everything to the page
page.add(
ft.Row([ft.Text("SMARTGURU AI", font_family="organical", style="headlineLarge", color="blue")], alignment="center"),
ft.Container(
content=chat,
border=ft.border.all(2, ft.colors.BLUE),
border_radius=20,
padding=10,
expand=True,
),
ft.Row(
[
new_message,
ft.IconButton(
icon=ft.icons.SEND_ROUNDED,
tooltip="Send message",
on_click=send_message_click,
icon_color=ft.colors.BLUE
),
]
),
)
ft.app(port=8550, target=main, view=ft.WEB_BROWSER, assets_dir="assets")