Use demo.load
Browse files
app.py
CHANGED
@@ -11,31 +11,38 @@ DATA_FILE = os.path.join("/data", DATA_FILENAME)
|
|
11 |
MAX_LINES = 20
|
12 |
|
13 |
def generate_html(me: str) -> str:
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
html += "</div>"
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
|
|
|
|
|
|
|
|
|
39 |
def store_message(writer: str, message: str, me: str):
|
40 |
if writer and message:
|
41 |
with open(DATA_FILE, "a") as csvfile:
|
@@ -80,15 +87,14 @@ with gr.Blocks(css=css) as demo:
|
|
80 |
gr.Markdown(
|
81 |
"""
|
82 |
<h1><center>Persistent Storage Chat</center></h1>
|
83 |
-
<p>This is a simple demo that implements a chatroom using persistent storage
|
84 |
-
All the messages are saved in a file, but only the last 20 ones are displayed.</p>
|
85 |
"""
|
86 |
)
|
87 |
|
88 |
with gr.Group():
|
89 |
with (login_box := gr.Row().style(equal_height=True)):
|
90 |
username = gr.Textbox(
|
91 |
-
label="What's your name?
|
92 |
)
|
93 |
login_btn = gr.Button("Enter the chat")
|
94 |
|
@@ -97,7 +103,7 @@ with gr.Blocks(css=css) as demo:
|
|
97 |
output = gr.HTML(label="Chat")
|
98 |
with gr.Row():
|
99 |
message = gr.Textbox(
|
100 |
-
label="Your message
|
101 |
)
|
102 |
|
103 |
username.submit(
|
@@ -116,4 +122,5 @@ with gr.Blocks(css=css) as demo:
|
|
116 |
inputs=[message, state],
|
117 |
outputs=[output, message],
|
118 |
)
|
119 |
-
demo.
|
|
|
|
11 |
MAX_LINES = 20
|
12 |
|
13 |
def generate_html(me: str) -> str:
|
14 |
+
try:
|
15 |
+
with open(DATA_FILE) as csvfile:
|
16 |
+
reader = csv.reader(csvfile)
|
17 |
+
rows = deque(reader, maxlen=MAX_LINES)
|
18 |
+
if len(rows) == 0:
|
19 |
+
return "no messages yet"
|
20 |
+
else:
|
21 |
+
html = "<div class='chatbot'>"
|
22 |
+
for row, _ in itertools.zip_longest(rows, range(MAX_LINES), fillvalue=("", "")):
|
23 |
+
username = row[0]
|
24 |
+
if username == me:
|
25 |
+
msg_type = "own"
|
26 |
+
elif username == "[chatbot]":
|
27 |
+
msg_type = "admin"
|
28 |
+
elif username == "":
|
29 |
+
msg_type = "empty"
|
30 |
+
else:
|
31 |
+
msg_type = "other"
|
32 |
+
html += "<div class='message'>"
|
33 |
+
html += f"<span class='nick'>{row[0]}</span>"
|
34 |
+
html += f"<span class='{msg_type}'>{row[1]}</span>"
|
35 |
+
html += "</div>"
|
36 |
html += "</div>"
|
37 |
+
return html
|
38 |
+
except:
|
39 |
+
return "no messages yet"
|
40 |
|
41 |
|
42 |
+
def refresh(state):
|
43 |
+
print(f"refresh, username: {state['username']}")
|
44 |
+
return generate_html(state["username"])
|
45 |
+
|
46 |
def store_message(writer: str, message: str, me: str):
|
47 |
if writer and message:
|
48 |
with open(DATA_FILE, "a") as csvfile:
|
|
|
87 |
gr.Markdown(
|
88 |
"""
|
89 |
<h1><center>Persistent Storage Chat</center></h1>
|
90 |
+
<p>This is a simple demo that implements a chatroom using persistent storage.</p>
|
|
|
91 |
"""
|
92 |
)
|
93 |
|
94 |
with gr.Group():
|
95 |
with (login_box := gr.Row().style(equal_height=True)):
|
96 |
username = gr.Textbox(
|
97 |
+
label="What's your name?", show_label=True, max_lines=1
|
98 |
)
|
99 |
login_btn = gr.Button("Enter the chat")
|
100 |
|
|
|
103 |
output = gr.HTML(label="Chat")
|
104 |
with gr.Row():
|
105 |
message = gr.Textbox(
|
106 |
+
label="Your message", show_label=True, max_lines=1
|
107 |
)
|
108 |
|
109 |
username.submit(
|
|
|
122 |
inputs=[message, state],
|
123 |
outputs=[output, message],
|
124 |
)
|
125 |
+
demo.load(refresh, queue=True, inputs=[state], outputs=output, every=5)
|
126 |
+
demo.queue().launch(server_name="0.0.0.0")
|