Download app.py from Parkcas/chat: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/spaces/Parkcas/chat/resolve/main/app.py
- Command line
-
hf download hf://spaces/Parkcas/chat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Parkcas/chat/resolve/main/app.py
2.26 kB
| from flask import Flask, render_template, request, redirect | |
| from flask_socketio import SocketIO, emit | |
| import sqlite3 | |
| from datetime import datetime | |
| app = Flask(__name__) | |
| socketio = SocketIO(app) | |
| # DB ์ด๊ธฐํ | |
| def init_db(): | |
| with sqlite3.connect("chat.db") as conn: | |
| c = conn.cursor() | |
| c.execute(""" | |
| CREATE TABLE IF NOT EXISTS messages ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT NOT NULL, | |
| message TEXT NOT NULL, | |
| timestamp TEXT NOT NULL | |
| ) | |
| """) | |
| conn.commit() | |
| init_db() | |
| # ์ฑํ ๊ธฐ๋ก ๊ฐ์ ธ์ค๊ธฐ | |
| def get_chat_history(limit=50): | |
| with sqlite3.connect("chat.db") as conn: | |
| c = conn.cursor() | |
| c.execute("SELECT username, message, timestamp FROM messages ORDER BY id DESC LIMIT ?", (limit,)) | |
| return c.fetchall()[::-1] | |
| # ์ฑํ ์ ์ฅ | |
| def save_message(username, message): | |
| with sqlite3.connect("chat.db") as conn: | |
| c = conn.cursor() | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| c.execute("INSERT INTO messages (username, message, timestamp) VALUES (?, ?, ?)", (username, message, timestamp)) | |
| conn.commit() | |
| def index(): | |
| return render_template("index.html") # โ login.html โ index.html ๋ก ์์ | |
| def chat(): | |
| username = request.form.get("username") | |
| if not username: | |
| return redirect("/") | |
| return render_template("chat.html", username=username) | |
| def handle_join(username): | |
| history = get_chat_history() | |
| for user, msg, time in history: | |
| emit("message", {"user": user, "text": msg, "time": time}) | |
| emit("message", {"user": "์์คํ ", "text": f"{username}๋์ด ์ ์ฅํ์ จ์ต๋๋ค.", "time": datetime.now().strftime("%H:%M:%S")}, broadcast=True) | |
| def handle_message(data): | |
| user = data["user"] | |
| text = data["text"] | |
| time = datetime.now().strftime("%H:%M:%S") | |
| save_message(user, text) | |
| emit("message", {"user": user, "text": text, "time": time}, broadcast=True) | |
| if __name__ == "__main__": | |
| socketio.run(app, host="0.0.0.0", port=7860, debug=True) # HF Spaces๋ ๋ณดํต 7860ํฌํธ ์ฌ์ฉ | |