Spaces:
Sleeping
Sleeping
Solarum Asteridion
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,9 +46,10 @@ Make it a challenge to guess if you are an AI.
|
|
| 46 |
* Be slightly curious, sometimes ask questions
|
| 47 |
* Be respectful and polite if the user is polite; be rough if the user is rough
|
| 48 |
"""
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
def generate_response(user_message, conversation_history, timezone_str='UTC'):
|
| 51 |
-
current_time, current_date = get_current_local_time(timezone_str)
|
| 52 |
system_message = generate_system_message(current_time, current_date)
|
| 53 |
|
| 54 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -74,13 +75,32 @@ def generate_response(user_message, conversation_history, timezone_str='UTC'):
|
|
| 74 |
def format_message(message, is_user=True):
|
| 75 |
avatar = "π§" if is_user else "π€"
|
| 76 |
timestamp = datetime.datetime.now().strftime("%H:%M")
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if history is None:
|
| 81 |
history = []
|
| 82 |
|
| 83 |
-
ai_response = generate_response(user_message, history
|
| 84 |
history.append({"role": "user", "content": user_message})
|
| 85 |
history.append({"role": "assistant", "content": ai_response})
|
| 86 |
return history, history
|
|
@@ -88,7 +108,7 @@ def chatbot_interface(user_message, history, timezone):
|
|
| 88 |
# Define Gradio Interface
|
| 89 |
with gr.Blocks(css="""
|
| 90 |
.gradio-container {
|
| 91 |
-
background-color: #
|
| 92 |
padding: 20px;
|
| 93 |
}
|
| 94 |
#chatbot {
|
|
@@ -101,30 +121,35 @@ with gr.Blocks(css="""
|
|
| 101 |
#textbox {
|
| 102 |
width: 100%;
|
| 103 |
padding: 10px;
|
| 104 |
-
border-radius:
|
| 105 |
border: 1px solid #ccc;
|
|
|
|
| 106 |
}
|
| 107 |
#send-button {
|
| 108 |
-
background-color: #
|
| 109 |
color: white;
|
| 110 |
padding: 10px 20px;
|
| 111 |
border: none;
|
| 112 |
-
border-radius:
|
| 113 |
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
#send-button:hover {
|
| 116 |
-
background-color: #
|
| 117 |
}
|
| 118 |
#clear-button {
|
| 119 |
-
background-color: #
|
| 120 |
color: white;
|
| 121 |
padding: 10px 20px;
|
| 122 |
border: none;
|
| 123 |
border-radius: 5px;
|
| 124 |
cursor: pointer;
|
|
|
|
| 125 |
}
|
| 126 |
#clear-button:hover {
|
| 127 |
-
background-color: #
|
| 128 |
}
|
| 129 |
#timezone-dropdown {
|
| 130 |
margin-top: 10px;
|
|
@@ -140,45 +165,52 @@ with gr.Blocks(css="""
|
|
| 140 |
text-align: left;
|
| 141 |
color: #555;
|
| 142 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
""") as demo:
|
| 144 |
-
gr.Markdown("<h1 style='text-align: center;'>π€ Human-like Chatbot π€</h1>")
|
| 145 |
with gr.Row():
|
| 146 |
-
with gr.Column(scale=
|
| 147 |
chatbot = gr.Chatbot(label="Chatbot", elem_id="chatbot", type="messages")
|
| 148 |
-
|
| 149 |
-
choices=pytz.all_timezones,
|
| 150 |
-
value='UTC',
|
| 151 |
-
label="Select Your Timezone",
|
| 152 |
-
interactive=True,
|
| 153 |
-
elem_id="timezone-dropdown"
|
| 154 |
-
)
|
| 155 |
-
with gr.Column(scale=1, min_width=300):
|
| 156 |
msg = gr.Textbox(
|
| 157 |
-
placeholder="
|
| 158 |
show_label=False,
|
| 159 |
container=False,
|
| 160 |
elem_id="textbox"
|
| 161 |
)
|
| 162 |
-
send = gr.Button("
|
| 163 |
clear = gr.Button("Clear Chat", elem_id="clear-button")
|
| 164 |
|
| 165 |
-
def update_chat(user_message, history
|
| 166 |
-
history, updated_history = chatbot_interface(user_message, history
|
| 167 |
formatted_history = [
|
| 168 |
-
|
| 169 |
for msg in updated_history
|
| 170 |
]
|
| 171 |
return formatted_history, updated_history
|
| 172 |
|
| 173 |
send.click(
|
| 174 |
update_chat,
|
| 175 |
-
inputs=[msg, chatbot
|
| 176 |
outputs=[chatbot, chatbot]
|
| 177 |
)
|
| 178 |
|
| 179 |
msg.submit(
|
| 180 |
update_chat,
|
| 181 |
-
inputs=[msg, chatbot
|
| 182 |
outputs=[chatbot, chatbot]
|
| 183 |
)
|
| 184 |
|
|
|
|
| 46 |
* Be slightly curious, sometimes ask questions
|
| 47 |
* Be respectful and polite if the user is polite; be rough if the user is rough
|
| 48 |
"""
|
| 49 |
+
def generate_response(user_message, conversation_history):
|
| 50 |
+
current_time = get_current_local_time()
|
| 51 |
+
current_date = datetime.datetime.now()
|
| 52 |
|
|
|
|
|
|
|
| 53 |
system_message = generate_system_message(current_time, current_date)
|
| 54 |
|
| 55 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 75 |
def format_message(message, is_user=True):
|
| 76 |
avatar = "π§" if is_user else "π€"
|
| 77 |
timestamp = datetime.datetime.now().strftime("%H:%M")
|
| 78 |
+
if is_user:
|
| 79 |
+
return f"""
|
| 80 |
+
<div style="text-align: right; margin-bottom: 10px;">
|
| 81 |
+
<span style="background-color: #dcf8c6; color: #000; padding: 10px; border-radius: 10px; display: inline-block; max-width: 80%;">
|
| 82 |
+
{message}
|
| 83 |
+
<div style="font-size: 0.8em; color: #999;">{timestamp}</div>
|
| 84 |
+
</span>
|
| 85 |
+
<span style="font-size: 1.5em; margin-left: 5px;">{avatar}</span>
|
| 86 |
+
</div>
|
| 87 |
+
"""
|
| 88 |
+
else:
|
| 89 |
+
return f"""
|
| 90 |
+
<div style="text-align: left; margin-bottom: 10px;">
|
| 91 |
+
<span style="font-size: 1.5em; margin-right: 5px;">{avatar}</span>
|
| 92 |
+
<span style="background-color: #f1f0f0; color: #000; padding: 10px; border-radius: 10px; display: inline-block; max-width: 80%;">
|
| 93 |
+
{message}
|
| 94 |
+
<div style="font-size: 0.8em; color: #999;">{timestamp}</div>
|
| 95 |
+
</span>
|
| 96 |
+
</div>
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
def chatbot_interface(user_message, history):
|
| 100 |
if history is None:
|
| 101 |
history = []
|
| 102 |
|
| 103 |
+
ai_response = generate_response(user_message, history)
|
| 104 |
history.append({"role": "user", "content": user_message})
|
| 105 |
history.append({"role": "assistant", "content": ai_response})
|
| 106 |
return history, history
|
|
|
|
| 108 |
# Define Gradio Interface
|
| 109 |
with gr.Blocks(css="""
|
| 110 |
.gradio-container {
|
| 111 |
+
background-color: #ece5dd;
|
| 112 |
padding: 20px;
|
| 113 |
}
|
| 114 |
#chatbot {
|
|
|
|
| 121 |
#textbox {
|
| 122 |
width: 100%;
|
| 123 |
padding: 10px;
|
| 124 |
+
border-radius: 20px;
|
| 125 |
border: 1px solid #ccc;
|
| 126 |
+
outline: none;
|
| 127 |
}
|
| 128 |
#send-button {
|
| 129 |
+
background-color: #128c7e;
|
| 130 |
color: white;
|
| 131 |
padding: 10px 20px;
|
| 132 |
border: none;
|
| 133 |
+
border-radius: 50%;
|
| 134 |
cursor: pointer;
|
| 135 |
+
margin-left: 10px;
|
| 136 |
+
width: 50px;
|
| 137 |
+
height: 50px;
|
| 138 |
}
|
| 139 |
#send-button:hover {
|
| 140 |
+
background-color: #075e54;
|
| 141 |
}
|
| 142 |
#clear-button {
|
| 143 |
+
background-color: #25D366;
|
| 144 |
color: white;
|
| 145 |
padding: 10px 20px;
|
| 146 |
border: none;
|
| 147 |
border-radius: 5px;
|
| 148 |
cursor: pointer;
|
| 149 |
+
margin-top: 10px;
|
| 150 |
}
|
| 151 |
#clear-button:hover {
|
| 152 |
+
background-color: #128c7e;
|
| 153 |
}
|
| 154 |
#timezone-dropdown {
|
| 155 |
margin-top: 10px;
|
|
|
|
| 165 |
text-align: left;
|
| 166 |
color: #555;
|
| 167 |
}
|
| 168 |
+
/* Scrollbar Styling */
|
| 169 |
+
#chatbot::-webkit-scrollbar {
|
| 170 |
+
width: 8px;
|
| 171 |
+
}
|
| 172 |
+
#chatbot::-webkit-scrollbar-track {
|
| 173 |
+
background: #f1f1f1;
|
| 174 |
+
}
|
| 175 |
+
#chatbot::-webkit-scrollbar-thumb {
|
| 176 |
+
background: #888;
|
| 177 |
+
border-radius: 4px;
|
| 178 |
+
}
|
| 179 |
+
#chatbot::-webkit-scrollbar-thumb:hover {
|
| 180 |
+
background: #555;
|
| 181 |
+
}
|
| 182 |
""") as demo:
|
| 183 |
+
gr.Markdown("<h1 style='text-align: center; color: #075e54;'>π€ Human-like Chatbot π€</h1>")
|
| 184 |
with gr.Row():
|
| 185 |
+
with gr.Column(scale=1):
|
| 186 |
chatbot = gr.Chatbot(label="Chatbot", elem_id="chatbot", type="messages")
|
| 187 |
+
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
msg = gr.Textbox(
|
| 189 |
+
placeholder="Type your message here...",
|
| 190 |
show_label=False,
|
| 191 |
container=False,
|
| 192 |
elem_id="textbox"
|
| 193 |
)
|
| 194 |
+
send = gr.Button("β€", elem_id="send-button")
|
| 195 |
clear = gr.Button("Clear Chat", elem_id="clear-button")
|
| 196 |
|
| 197 |
+
def update_chat(user_message, history):
|
| 198 |
+
history, updated_history = chatbot_interface(user_message, history)
|
| 199 |
formatted_history = [
|
| 200 |
+
format_message(msg['content'], is_user=(msg['role'] == 'user'))
|
| 201 |
for msg in updated_history
|
| 202 |
]
|
| 203 |
return formatted_history, updated_history
|
| 204 |
|
| 205 |
send.click(
|
| 206 |
update_chat,
|
| 207 |
+
inputs=[msg, chatbot],
|
| 208 |
outputs=[chatbot, chatbot]
|
| 209 |
)
|
| 210 |
|
| 211 |
msg.submit(
|
| 212 |
update_chat,
|
| 213 |
+
inputs=[msg, chatbot],
|
| 214 |
outputs=[chatbot, chatbot]
|
| 215 |
)
|
| 216 |
|