Download app.py from OzoneAsai/GChat: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/spaces/OzoneAsai/GChat/resolve/main/app.py
- Command line
-
hf download hf://spaces/OzoneAsai/GChat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/OzoneAsai/GChat/resolve/main/app.py
2.25 kB
| import streamlit as st | |
| import hashlib | |
| import datetime | |
| # Streamlitの設定 | |
| st.title("Simple IRC with Streamlit") | |
| # チャットメッセージと入力欄のコンポーネントを設定 | |
| messages = st.empty() | |
| user_input = st.chat_input("Your message", key="message_input") | |
| # サーバーでメッセージを保存するためのリスト | |
| message_history = [] | |
| # Socket.IOを模した簡単なメッセージングシステムの実装 | |
| class MessageHandler: | |
| def __init__(self): | |
| self.connected_users = set() | |
| def add_user(self, user_id): | |
| self.connected_users.add(user_id) | |
| def remove_user(self, user_id): | |
| self.connected_users.remove(user_id) | |
| def send_message(self, user_id, message): | |
| timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| formatted_message = f"{timestamp} - {user_id}: {message}" | |
| message_history.append(formatted_message) | |
| self.refresh_messages() | |
| def refresh_messages(self): | |
| messages.markdown("\n".join(message_history), unsafe_allow_html=True) | |
| # メッセージハンドラの初期化 | |
| message_handler = MessageHandler() | |
| # ユーザーのIPアドレスをハッシュ化して名前として使用 | |
| user_id = hashlib.sha256(st.report_thread.get_report_ctx().session.request.ip.encode()).hexdigest() | |
| # ユーザーをサーバーに追加 | |
| message_handler.add_user(user_id) | |
| # Streamlit chat inputのコールバック | |
| def on_submit(message): | |
| # メッセージを送信 | |
| message_handler.send_message(user_id, message) | |
| # Streamlit chat inputのイベントハンドラの設定 | |
| user_input.on_submit(on_submit) | |
| # Streamlitアプリの実行 | |
| if __name__ == "__main__": | |
| st.set_page_config(page_title="IRC with Streamlit") | |
| # ユーザーがアクセスするたびに新しいセッションが作成されるようにする | |
| st.experimental_singleton(message_handler) | |
| # チャットメッセージの更新を定期的に行う | |
| st.text("Connected Users: " + ", ".join(message_handler.connected_users)) | |
| st.text("Refreshed every 5 seconds") | |
| st.text("Your hashed name: " + user_id) | |
| # Streamlitアプリを実行 | |
| st.experimental_run_with_streamlit_app(main) | |