Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +31 -0
- app.py +150 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
|
| 5 |
+
ENV HOME=/home/user \
|
| 6 |
+
PATH=/home/user/.local/bin:$PATH \
|
| 7 |
+
PORT=7860 \
|
| 8 |
+
HOST=0.0.0.0
|
| 9 |
+
|
| 10 |
+
WORKDIR $HOME/app
|
| 11 |
+
|
| 12 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 13 |
+
gcc \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
+
|
| 16 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 17 |
+
|
| 18 |
+
COPY requirements.txt .
|
| 19 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
+
|
| 21 |
+
COPY --chown=user . $HOME/app
|
| 22 |
+
|
| 23 |
+
USER user
|
| 24 |
+
|
| 25 |
+
EXPOSE 7860
|
| 26 |
+
|
| 27 |
+
CMD ["python", "app.py"]
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
### 📁 **4. Create directory: templates/**
|
| 31 |
+
Create a folder called `templates` in the same directory as your other files.
|
app.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
WHACKY CHAT APP - COMPLETE VERSION
|
| 4 |
+
A modern, whacky Flask-SocketIO chat application
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from flask import Flask, render_template, request, redirect, url_for, session
|
| 8 |
+
from flask_socketio import SocketIO, emit
|
| 9 |
+
import secrets
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# Hugging Face Spaces compatibility
|
| 13 |
+
PORT = int(os.environ.get("PORT", 7860))
|
| 14 |
+
HOST = os.environ.get("HOST", "0.0.0.0")
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
app.config['SECRET_KEY'] = secrets.token_hex(16)
|
| 18 |
+
|
| 19 |
+
# Configure SocketIO for Hugging Face Spaces
|
| 20 |
+
socketio = SocketIO(
|
| 21 |
+
app,
|
| 22 |
+
cors_allowed_origins='*',
|
| 23 |
+
async_mode='eventlet',
|
| 24 |
+
logger=True,
|
| 25 |
+
engineio_logger=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Store connected users and user counter
|
| 29 |
+
connected_users = {}
|
| 30 |
+
user_counter = 0
|
| 31 |
+
|
| 32 |
+
@app.route('/')
|
| 33 |
+
def index():
|
| 34 |
+
# If user is not logged in, redirect to login
|
| 35 |
+
if 'user_number' not in session:
|
| 36 |
+
return redirect(url_for('login'))
|
| 37 |
+
return render_template('chat.html')
|
| 38 |
+
|
| 39 |
+
@app.route('/login', methods=['GET', 'POST'])
|
| 40 |
+
def login():
|
| 41 |
+
global user_counter
|
| 42 |
+
|
| 43 |
+
if request.method == 'POST':
|
| 44 |
+
# Get username from form
|
| 45 |
+
username = request.form.get('username', '').strip()
|
| 46 |
+
|
| 47 |
+
if username:
|
| 48 |
+
user_counter += 1
|
| 49 |
+
user_number = f"{user_counter:02d}" # Format as two digits (01, 02, etc.)
|
| 50 |
+
|
| 51 |
+
# Store user info in session
|
| 52 |
+
session['user_number'] = user_number
|
| 53 |
+
session['username'] = username
|
| 54 |
+
|
| 55 |
+
return redirect(url_for('index'))
|
| 56 |
+
|
| 57 |
+
return render_template('login.html')
|
| 58 |
+
|
| 59 |
+
@app.route('/logout')
|
| 60 |
+
def logout():
|
| 61 |
+
# Clear session
|
| 62 |
+
session.clear()
|
| 63 |
+
return redirect(url_for('login'))
|
| 64 |
+
|
| 65 |
+
@socketio.on('connect')
|
| 66 |
+
def handle_connect():
|
| 67 |
+
if 'user_number' not in session:
|
| 68 |
+
return False # Reject connection if not logged in
|
| 69 |
+
|
| 70 |
+
user_number = session['user_number']
|
| 71 |
+
username = session['username']
|
| 72 |
+
user_id = request.sid
|
| 73 |
+
|
| 74 |
+
# Add user to connected users
|
| 75 |
+
connected_users[user_id] = {
|
| 76 |
+
'user_number': user_number,
|
| 77 |
+
'username': username,
|
| 78 |
+
'user_id': user_id
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
# Send welcome message to the new user
|
| 82 |
+
emit('user_assigned', {
|
| 83 |
+
'user_number': user_number,
|
| 84 |
+
'username': username,
|
| 85 |
+
'message': f'Welcome {username}! You are user #{user_number}'
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
# Notify all users about the new user
|
| 89 |
+
emit('user_joined', {
|
| 90 |
+
'user_number': user_number,
|
| 91 |
+
'username': username,
|
| 92 |
+
'message': f'{username} (User #{user_number}) has joined the chat!',
|
| 93 |
+
'total_users': len(connected_users)
|
| 94 |
+
}, broadcast=True)
|
| 95 |
+
|
| 96 |
+
print(f"User {username} (#{user_number}) connected")
|
| 97 |
+
|
| 98 |
+
@socketio.on('disconnect')
|
| 99 |
+
def handle_disconnect():
|
| 100 |
+
user_id = request.sid
|
| 101 |
+
if user_id in connected_users:
|
| 102 |
+
user_info = connected_users[user_id]
|
| 103 |
+
user_number = user_info['user_number']
|
| 104 |
+
username = user_info['username']
|
| 105 |
+
del connected_users[user_id]
|
| 106 |
+
|
| 107 |
+
# Notify all users about the user leaving
|
| 108 |
+
emit('user_left', {
|
| 109 |
+
'user_number': user_number,
|
| 110 |
+
'username': username,
|
| 111 |
+
'message': f'{username} (User #{user_number}) has left the chat!',
|
| 112 |
+
'total_users': len(connected_users)
|
| 113 |
+
}, broadcast=True)
|
| 114 |
+
|
| 115 |
+
print(f"User {username} (#{user_number}) disconnected")
|
| 116 |
+
|
| 117 |
+
@socketio.on('send_message')
|
| 118 |
+
def handle_message(data):
|
| 119 |
+
if 'user_number' not in session:
|
| 120 |
+
return
|
| 121 |
+
|
| 122 |
+
user_number = session['user_number']
|
| 123 |
+
username = session['username']
|
| 124 |
+
|
| 125 |
+
# Broadcast message to all users
|
| 126 |
+
emit('new_message', {
|
| 127 |
+
'user_number': user_number,
|
| 128 |
+
'username': username,
|
| 129 |
+
'message': data['message'],
|
| 130 |
+
'timestamp': data.get('timestamp', '')
|
| 131 |
+
}, broadcast=True)
|
| 132 |
+
|
| 133 |
+
@socketio.on('typing')
|
| 134 |
+
def handle_typing(data):
|
| 135 |
+
if 'user_number' not in session:
|
| 136 |
+
return
|
| 137 |
+
|
| 138 |
+
user_number = session['user_number']
|
| 139 |
+
username = session['username']
|
| 140 |
+
|
| 141 |
+
emit('user_typing', {
|
| 142 |
+
'user_number': user_number,
|
| 143 |
+
'username': username
|
| 144 |
+
}, broadcast=True, include_self=False)
|
| 145 |
+
|
| 146 |
+
if __name__ == '__main__':
|
| 147 |
+
print("🚀 Starting Whacky Chat App - Complete Version...")
|
| 148 |
+
print(f"📡 Server running on http://{HOST}:{PORT}")
|
| 149 |
+
print("✅ Ready for Hugging Face Spaces deployment!")
|
| 150 |
+
socketio.run(app, debug=True, host=HOST, port=PORT)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask==2.3.3
|
| 2 |
+
flask-socketio==5.3.6
|
| 3 |
+
python-socketio==5.8.0
|
| 4 |
+
eventlet==0.33.3
|
| 5 |
+
python-dotenv==1.0.0
|