Spaces:
Sleeping
Sleeping
shubhajit07
commited on
Commit
•
32471ec
1
Parent(s):
964353f
Initial
Browse files- Dockerfile +14 -0
- main.py +103 -0
- requirements.txt +3 -0
- static/css/style.css +69 -0
- templates/base.html +19 -0
- templates/home.html +24 -0
- templates/room.html +52 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["gunicorn", "main:app", "-w", "1", "--threads", "100", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, session, redirect, url_for
|
2 |
+
from flask_socketio import join_room, leave_room, send, SocketIO
|
3 |
+
import random
|
4 |
+
from string import ascii_uppercase
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
app.config["SECRET_KEY"] = "hjhjsdahhds"
|
8 |
+
socketio = SocketIO(app)
|
9 |
+
|
10 |
+
rooms = {}
|
11 |
+
|
12 |
+
def generate_unique_code(length):
|
13 |
+
while True:
|
14 |
+
code = ""
|
15 |
+
for _ in range(length):
|
16 |
+
code += random.choice(ascii_uppercase)
|
17 |
+
|
18 |
+
if code not in rooms:
|
19 |
+
break
|
20 |
+
|
21 |
+
return code
|
22 |
+
|
23 |
+
@app.route("/", methods=["POST", "GET"])
|
24 |
+
def home():
|
25 |
+
session.clear()
|
26 |
+
if request.method == "POST":
|
27 |
+
name = request.form.get("name")
|
28 |
+
code = request.form.get("code")
|
29 |
+
join = request.form.get("join", False)
|
30 |
+
create = request.form.get("create", False)
|
31 |
+
|
32 |
+
if not name:
|
33 |
+
return render_template("home.html", error="Please enter a name.", code=code, name=name)
|
34 |
+
|
35 |
+
if join != False and not code:
|
36 |
+
return render_template("home.html", error="Please enter a room code.", code=code, name=name)
|
37 |
+
|
38 |
+
room = code
|
39 |
+
if create != False:
|
40 |
+
room = generate_unique_code(4)
|
41 |
+
rooms[room] = {"members": 0, "messages": []}
|
42 |
+
elif code not in rooms:
|
43 |
+
return render_template("home.html", error="Room does not exist.", code=code, name=name)
|
44 |
+
|
45 |
+
session["room"] = room
|
46 |
+
session["name"] = name
|
47 |
+
return redirect(url_for("room"))
|
48 |
+
|
49 |
+
return render_template("home.html")
|
50 |
+
|
51 |
+
@app.route("/room")
|
52 |
+
def room():
|
53 |
+
room = session.get("room")
|
54 |
+
if room is None or session.get("name") is None or room not in rooms:
|
55 |
+
return redirect(url_for("home"))
|
56 |
+
|
57 |
+
return render_template("room.html", code=room, messages=rooms[room]["messages"])
|
58 |
+
|
59 |
+
@socketio.on("message")
|
60 |
+
def message(data):
|
61 |
+
room = session.get("room")
|
62 |
+
if room not in rooms:
|
63 |
+
return
|
64 |
+
|
65 |
+
content = {
|
66 |
+
"name": session.get("name"),
|
67 |
+
"message": data["data"]
|
68 |
+
}
|
69 |
+
send(content, to=room)
|
70 |
+
rooms[room]["messages"].append(content)
|
71 |
+
print(f"{session.get('name')} said: {data['data']}")
|
72 |
+
|
73 |
+
@socketio.on("connect")
|
74 |
+
def connect(auth):
|
75 |
+
room = session.get("room")
|
76 |
+
name = session.get("name")
|
77 |
+
if not room or not name:
|
78 |
+
return
|
79 |
+
if room not in rooms:
|
80 |
+
leave_room(room)
|
81 |
+
return
|
82 |
+
|
83 |
+
join_room(room)
|
84 |
+
send({"name": name, "message": "has entered the room"}, to=room)
|
85 |
+
rooms[room]["members"] += 1
|
86 |
+
print(f"{name} joined room {room}")
|
87 |
+
|
88 |
+
@socketio.on("disconnect")
|
89 |
+
def disconnect():
|
90 |
+
room = session.get("room")
|
91 |
+
name = session.get("name")
|
92 |
+
leave_room(room)
|
93 |
+
|
94 |
+
if room in rooms:
|
95 |
+
rooms[room]["members"] -= 1
|
96 |
+
if rooms[room]["members"] <= 0:
|
97 |
+
del rooms[room]
|
98 |
+
|
99 |
+
send({"name": name, "message": "has left the room"}, to=room)
|
100 |
+
print(f"{name} has left the room {room}")
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
socketio.run(app, debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
flask-socketio
|
3 |
+
gunicorn
|
static/css/style.css
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.content {
|
2 |
+
display: flex;
|
3 |
+
align-items: center;
|
4 |
+
justify-content: center;
|
5 |
+
}
|
6 |
+
|
7 |
+
.buttons {
|
8 |
+
display: flex;
|
9 |
+
flex-direction: column;
|
10 |
+
align-items: center;
|
11 |
+
justify-content: center;
|
12 |
+
gap: 10px;
|
13 |
+
}
|
14 |
+
|
15 |
+
.join {
|
16 |
+
display: flex;
|
17 |
+
flex-direction: row;
|
18 |
+
width: 100%;
|
19 |
+
}
|
20 |
+
|
21 |
+
.create-btn {
|
22 |
+
width: 100%;
|
23 |
+
}
|
24 |
+
|
25 |
+
.message-box {
|
26 |
+
border-color: black;
|
27 |
+
border-width: 2px;
|
28 |
+
border-style: solid;
|
29 |
+
border-radius: 10px;
|
30 |
+
background-color: whitesmoke;
|
31 |
+
height: 80vh;
|
32 |
+
display: flex;
|
33 |
+
flex-direction: column;
|
34 |
+
width: 80vw;
|
35 |
+
align-items: stretch;
|
36 |
+
}
|
37 |
+
|
38 |
+
.messages {
|
39 |
+
overflow-y: scroll;
|
40 |
+
flex: 1;
|
41 |
+
width: 100%;
|
42 |
+
}
|
43 |
+
|
44 |
+
.inputs {
|
45 |
+
padding: 10px;
|
46 |
+
display: flex;
|
47 |
+
}
|
48 |
+
|
49 |
+
h2 {
|
50 |
+
text-align: center;
|
51 |
+
}
|
52 |
+
|
53 |
+
#message {
|
54 |
+
flex: 1;
|
55 |
+
}
|
56 |
+
|
57 |
+
.text {
|
58 |
+
display: flex;
|
59 |
+
flex-direction: row;
|
60 |
+
align-items: center;
|
61 |
+
justify-content: space-between;
|
62 |
+
padding-left: 10px;
|
63 |
+
padding-right: 10px;
|
64 |
+
}
|
65 |
+
|
66 |
+
.muted {
|
67 |
+
font-size: 10px;
|
68 |
+
color: darkgray;
|
69 |
+
}
|
templates/base.html
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<title>Chat App</title>
|
6 |
+
<link
|
7 |
+
rel="stylesheet"
|
8 |
+
href="{{url_for('static', filename='css/style.css')}}"
|
9 |
+
/>
|
10 |
+
<script
|
11 |
+
src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"
|
12 |
+
integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA=="
|
13 |
+
crossorigin="anonymous"
|
14 |
+
></script>
|
15 |
+
</head>
|
16 |
+
<body>
|
17 |
+
<div class="content">{% block content %} {% endblock %}</div>
|
18 |
+
</body>
|
19 |
+
</html>
|
templates/home.html
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{% extends 'base.html' %} {% block content %}
|
2 |
+
<form method="post" class="buttons">
|
3 |
+
<h3>Enter The Chat Room</h3>
|
4 |
+
<div>
|
5 |
+
<label>Name:</label>
|
6 |
+
<input
|
7 |
+
type="text"
|
8 |
+
placeholder="Pick a name!"
|
9 |
+
name="name"
|
10 |
+
value="{{name}}"
|
11 |
+
/>
|
12 |
+
</div>
|
13 |
+
<div class="join">
|
14 |
+
<input type="text" placeholder="Room Code" name="code" value="{{code}}" />
|
15 |
+
<button type="submit" name="join">Join a Room</button>
|
16 |
+
</div>
|
17 |
+
<button type="submit" name="create" class="create-btn">Create a Room</button>
|
18 |
+
{% if error %}
|
19 |
+
<ul>
|
20 |
+
<li>{{error}}</li>
|
21 |
+
</ul>
|
22 |
+
{% endif %}
|
23 |
+
</form>
|
24 |
+
{% endblock %}
|
templates/room.html
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{% extends 'base.html' %} {% block content %}
|
2 |
+
<div class="message-box">
|
3 |
+
<h2>Chat Room: {{code}}</h2>
|
4 |
+
<div class="messages" id="messages"></div>
|
5 |
+
<div class="inputs">
|
6 |
+
<input
|
7 |
+
type="text"
|
8 |
+
rows="3"
|
9 |
+
placeholder="Message"
|
10 |
+
name="message"
|
11 |
+
id="message"
|
12 |
+
/>
|
13 |
+
<button type="button" name="send" id="send-btn" onClick="sendMessage()">
|
14 |
+
Send
|
15 |
+
</button>
|
16 |
+
</div>
|
17 |
+
</div>
|
18 |
+
<script type="text/javascript">
|
19 |
+
var socketio = io();
|
20 |
+
|
21 |
+
const messages = document.getElementById("messages");
|
22 |
+
|
23 |
+
const createMessage = (name, msg) => {
|
24 |
+
const content = `
|
25 |
+
<div class="text">
|
26 |
+
<span>
|
27 |
+
<strong>${name}</strong>: ${msg}
|
28 |
+
</span>
|
29 |
+
<span class="muted">
|
30 |
+
${new Date().toLocaleString()}
|
31 |
+
</span>
|
32 |
+
</div>
|
33 |
+
`;
|
34 |
+
messages.innerHTML += content;
|
35 |
+
};
|
36 |
+
|
37 |
+
socketio.on("message", (data) => {
|
38 |
+
createMessage(data.name, data.message);
|
39 |
+
});
|
40 |
+
|
41 |
+
const sendMessage = () => {
|
42 |
+
const message = document.getElementById("message");
|
43 |
+
if (message.value == "") return;
|
44 |
+
socketio.emit("message", { data: message.value });
|
45 |
+
message.value = "";
|
46 |
+
};
|
47 |
+
</script>
|
48 |
+
{% for msg in messages %}
|
49 |
+
<script type="text/javascript">
|
50 |
+
createMessage("{{msg.name}}", "{{msg.message}}");
|
51 |
+
</script>
|
52 |
+
{% endfor %} {% endblock %}
|