Commit ·
715494c
1
Parent(s): 44f7f5d
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,43 +3,32 @@ from datetime import datetime
|
|
| 3 |
from gradio.components import *
|
| 4 |
from gradio.inputs import *
|
| 5 |
|
| 6 |
-
|
| 7 |
-
CHATROOMS = {}
|
| 8 |
-
VERSIONS = {
|
| 9 |
-
"app.py, version 1.0": "app.py",
|
| 10 |
-
"app_2.py, version 1.1": "app_2.py"
|
| 11 |
-
}
|
| 12 |
|
| 13 |
-
def chatroom(username, message,
|
| 14 |
-
if chatroom_name not in
|
| 15 |
-
|
| 16 |
if send_message:
|
| 17 |
now = datetime.now()
|
| 18 |
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 19 |
-
|
| 20 |
-
message_history = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return message_history, ""
|
| 22 |
|
| 23 |
-
|
| 24 |
-
inputs
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
choose("Version", choices=list(VERSIONS.keys()))
|
| 31 |
-
]
|
| 32 |
-
outputs = ["html", "text"]
|
| 33 |
-
|
| 34 |
-
# Define interface
|
| 35 |
-
iface = gr.Interface(fn=chatroom,
|
| 36 |
-
inputs=inputs,
|
| 37 |
-
outputs=outputs,
|
| 38 |
layout="vertical",
|
| 39 |
title="Public Chatroom",
|
| 40 |
-
description="Type your username and
|
| 41 |
|
| 42 |
-
# Define file paths
|
| 43 |
-
version = iface.inputs[-1].value
|
| 44 |
-
iface.share(file_name=VERSIONS[version])
|
| 45 |
iface.launch()
|
|
|
|
| 3 |
from gradio.components import *
|
| 4 |
from gradio.inputs import *
|
| 5 |
|
| 6 |
+
chatrooms = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def chatroom(username, message, image, send_message, chatroom_name):
|
| 9 |
+
if chatroom_name not in chatrooms:
|
| 10 |
+
chatrooms[chatroom_name] = []
|
| 11 |
if send_message:
|
| 12 |
now = datetime.now()
|
| 13 |
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 14 |
+
chatrooms[chatroom_name].append((username, message, image, timestamp))
|
| 15 |
+
message_history = ""
|
| 16 |
+
for msg in chatrooms[chatroom_name]:
|
| 17 |
+
if msg[1]:
|
| 18 |
+
message_history += f"{msg[3]} {msg[0]}: {msg[1]}<br>"
|
| 19 |
+
if msg[2]:
|
| 20 |
+
message_history += f"{msg[3]} {msg[0]}: <img src='data:image/png;base64,{msg[2]}' /><br>"
|
| 21 |
return message_history, ""
|
| 22 |
|
| 23 |
+
iface = gr.Interface(fn=chatroom,
|
| 24 |
+
inputs=[Textbox("Username"),
|
| 25 |
+
Textbox("Message"),
|
| 26 |
+
Image(shape=(224, 224), image_mode="RGB", source="upload", label="Image"),
|
| 27 |
+
Checkbox("Send"),
|
| 28 |
+
Textbox(label="Chatroom Name")],
|
| 29 |
+
outputs=["html", "text"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
layout="vertical",
|
| 31 |
title="Public Chatroom",
|
| 32 |
+
description="Type your username, message, and/or upload an image, and click Send to send to the chatroom.")
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
iface.launch()
|