Spaces:
Running
Running
File size: 5,743 Bytes
3d35b27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
import os
from flask import Flask, render_template
from flask_socketio import SocketIO, send
from pyngrok import ngrok
# Initialize Flask app and SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
# HTML template for the chat
chat_html = '''
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Chakra+Petch:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap" rel="stylesheet">
<style>
body {
background-color: #1e1e1e;
color: #dcdcdc;
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow-x: hidden;
}
.all {
width: 90%;
max-width: 600px;
margin-top: 50px;
}
#header {
font-size: 40px;
margin-bottom: 10px;
text-align: center;
color: #7289da;
font-family: "Chakra Petch", sans-serif;
font-weight: 700;
font-style: normal;
}
#messages {
margin-bottom: 20px;
padding: 10px;
border: 1px solid #555;
background-color: #2c2f33;
overflow-y: auto;
border-radius: 5px;
height: 300px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.5);
}
.message {
margin: 5px 0;
padding: 10px;
border-radius: 5px;
max-width: 70%;
word-wrap: break-word;
box-shadow: 0px 0px 5px rgba(0,0,0,0.5);
}
.sent {
background-color: #7289da;
color: #ffffff;
align-self: flex-end;
margin-left: auto;
text-align: right;
}
.received {
background-color: #36393f;
color: #ffffff;
align-self: flex-start;
margin-right: auto;
}
#myMessage {
width: calc(100% - 100px);
padding: 10px;
height: 40px;
border-radius: 5px;
border: none;
background-color: #40444b;
color: #dcdcdc;
font-size: 15px;
margin-right: 10px;
outline: none;
}
#sendbutton {
width: 40px;
height: 40px;
border: none;
border-radius: 5px;
background-color: #7289da;
color: #ffffff;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
outline: none;
transition: background-color 0.3s;
}
#sendbutton:hover {
background-color: #5a6b9f;
}
#imageUpload {
display: none;
}
.input-container {
display: flex;
align-items: center;
background-color: #2c2f33;
padding: 10px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.5);
}
.fa-paperclip {
font-size: 20px;
color: #7289da;
cursor: pointer;
margin-right: 10px;
}
.fa-paperclip:hover {
color: #5a6b9f;
}
</style>
<title>Spider-Chat</title>
<script src="https://cdn.socket.io/4.0.1/socket.io.min.js"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', (event) => {
var socket = io.connect(window.location.href);
function sendMessage(message) {
if (message.trim() !== '') {
socket.send(message);
}
}
socket.on('message', function(msg) {
var messageElement = document.createElement('div');
messageElement.classList.add('message', 'received');
messageElement.innerHTML = "<strong>User:</strong> " + msg;
document.getElementById('messages').appendChild(messageElement);
document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
});
document.getElementById('sendbutton').onclick = function() {
var msg = document.getElementById('myMessage').value;
sendMessage(msg);
};
document.getElementById('myMessage').addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
var msg = document.getElementById('myMessage').value;
sendMessage(msg);
}
});
document.getElementById('filebutton').onclick = function() {
document.getElementById('imageUpload').click();
};
document.getElementById('imageUpload').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
sendImage(file);
}
});
function sendImage(file) {
const reader = new FileReader();
reader.onload = function(event) {
const imageData = event.target.result;
sendMessage(imageData);
};
reader.readAsDataURL(file);
}
});
</script>
</head>
<body>
<div class="all">
<div id="header">Spider-Chat</div>
<div id="messages"></div>
<div class="input-container">
<label for="imageUpload" class="fas fa-paperclip"></label>
<input type="file" id="imageUpload" accept="image/*">
<input id="myMessage" placeholder="Type here" autocomplete="off">
<button id="sendbutton"><i class="fas fa-paper-plane"></i></button>
</div>
</div>
</body>
</html>
'''
@app.route('/')
def chat():
return chat_html
@socketio.on('message')
def handleMessage(msg):
print('Message: ' + msg)
send(msg, broadcast=True)
# Start ngrok to expose the app
url = ngrok.connect(5000)
print(' * Tunnel URL:', url)
if __name__ == '__main__':
socketio.run(app, port=5000, allow_unsafe_werkzeug=True)
|