|
import gradio as gr |
|
import datetime |
|
|
|
|
|
|
|
chat_history = [] |
|
|
|
def add_message(message, username="Anonymous"): |
|
""" |
|
Adds a new message to the chat history with a timestamp and username. |
|
""" |
|
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
formatted_message = f"[{timestamp}] {username}: {message}" |
|
chat_history.append(formatted_message) |
|
|
|
return "\n".join(chat_history) |
|
|
|
|
|
with gr.Blocks(title="SmilyAI Community Chat") as demo: |
|
gr.Markdown( |
|
""" |
|
# Welcome to SmilyAI Community Chat! π |
|
Chat with your fellow community members here. |
|
""" |
|
) |
|
|
|
|
|
|
|
chat_display = gr.Textbox( |
|
label="Chat History", |
|
lines=20, |
|
interactive=False, |
|
autoscroll=True, |
|
value="\n".join(chat_history) |
|
) |
|
|
|
|
|
username_input = gr.Textbox( |
|
label="Your Name (Optional)", |
|
placeholder="Enter your name", |
|
value="Anonymous", |
|
interactive=True |
|
) |
|
|
|
|
|
message_input = gr.Textbox( |
|
label="Your Message", |
|
placeholder="Type your message here...", |
|
lines=3 |
|
) |
|
|
|
|
|
send_button = gr.Button("Send Message") |
|
|
|
|
|
|
|
|
|
send_button.click( |
|
fn=add_message, |
|
inputs=[message_input, username_input], |
|
outputs=chat_display |
|
).then( |
|
|
|
fn=lambda: "", |
|
inputs=[], |
|
outputs=message_input |
|
) |
|
|
|
|
|
message_input.submit( |
|
fn=add_message, |
|
inputs=[message_input, username_input], |
|
outputs=chat_display |
|
).then( |
|
|
|
fn=lambda: "", |
|
inputs=[], |
|
outputs=message_input |
|
) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|