ysharma HF staff pcuenq HF staff commited on
Commit
5a837f6
0 Parent(s):

Duplicate from pcuenq/irc

Browse files

Co-authored-by: Pedro Cuenca <[email protected]>

Files changed (4) hide show
  1. .gitattributes +27 -0
  2. .gitignore +3 -0
  3. README.md +13 -0
  4. app.py +126 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env/
2
+ data/
3
+ .vscode/
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Persistent Chatroom
3
+ emoji: 🐨
4
+ colorFrom: pink
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.35.2
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: pcuenq/irc
11
+ ---
12
+
13
+ # Configuration
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import csv
3
+ import gradio as gr
4
+ import itertools
5
+ import random
6
+ from collections import deque
7
+
8
+ DATA_FILENAME = "data.csv"
9
+ DATA_FILE = os.path.join("/data", DATA_FILENAME)
10
+
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
+ if msg_type != "admin":
34
+ html += f"<span class='nick'>{row[0]}</span>"
35
+ html += f"<span class='{msg_type}'>{row[1]}</span>"
36
+ html += "</div>"
37
+ html += "</div>"
38
+ return html
39
+ except:
40
+ return "no messages yet"
41
+
42
+
43
+ def refresh(state):
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:
49
+ csv.writer(csvfile).writerow([writer, message])
50
+
51
+ return generate_html(me)
52
+
53
+ anons = "alligator, anteater, armadillo, auroch, axolotl, badger, bat, bear, beaver, blobfish, buffalo, camel, chameleon, cheetah, chipmunk, chinchilla, chupacabra, cormorant, coyote, crow, dingo, dinosaur, dog, dolphin, dragon, duck, dumbo octopus, elephant, ferret, fox, frog, giraffe, goose, gopher, grizzly, hamster, hedgehog, hippo, hyena, jackal, jackalope, ibex, ifrit, iguana, kangaroo, kiwi, koala, kraken, lemur, leopard, liger, lion, llama, manatee, mink, monkey, moose, narwhal, nyan cat, orangutan, otter, panda, penguin, platypus, python, pumpkin, quagga, quokka, rabbit, raccoon, rhino, sheep, shrew, skunk, slow loris, squirrel, tiger, turtle, unicorn, walrus, wolf, wolverine, wombat"
54
+ anons = anons.split(",")
55
+
56
+ def login(username, state):
57
+ if username == "":
58
+ username = f"Anonymous {random.choice(anons).strip()}"
59
+ print(username)
60
+ state["username"] = username
61
+ store_message("[chatbot]", f"{username} joins the chat", username)
62
+ return (
63
+ state,
64
+ gr.update(visible=False),
65
+ gr.update(visible=True),
66
+ generate_html(username),
67
+ )
68
+
69
+ def send_message(message, state):
70
+ username = state["username"]
71
+ if message != "":
72
+ store_message(username, message, me=username)
73
+ return (generate_html(username), "")
74
+
75
+ css = """
76
+ .message {height: 28px;}
77
+ .nick {font-weight:bold;}
78
+ .other {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
79
+ .own {background-color:lime;color:white; padding:4px;margin:4px;border-radius:4px; }
80
+ .admin {background-color:orange;color:black; padding:4px;margin:4px;border-radius:4px; }
81
+ """
82
+ with gr.Blocks(css=css) as demo:
83
+ state = gr.Variable({
84
+ 'username': None,
85
+ })
86
+
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(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
+
101
+ with (chat_box := gr.Box(visible=False)):
102
+ with gr.Row():
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(
110
+ login,
111
+ inputs=[username, state],
112
+ outputs=[state, login_box, chat_box, output],
113
+ )
114
+ login_btn.click(
115
+ fn=login,
116
+ inputs=[username, state],
117
+ outputs=[state, login_box, chat_box, output],
118
+ )
119
+
120
+ message.submit(
121
+ send_message,
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()