import gradio as gr import datetime import json # --- KONFIGURASI --- MAX_HISTORY = 50 # Simpan 50 pesan terakhir MESSAGES = [] # RAM Storage # Filter Kata Kasar (Basic) BAD_WORDS = ["anjing", "babi", "kontol", "memek", "jembut", "fuck", "shit", "bitch", "nigger", "nigga"] def filter_text(text): text_lower = text.lower() for bad in BAD_WORDS: if bad in text_lower: # Sensor dengan tanda bintang return "*" * len(text) return text def add_message(username, text): """Menerima pesan baru dan menyimpannya ke RAM""" global MESSAGES if not username or not text: return MESSAGES # Cek Admin role = "user" if username == "C0LA21": role = "admin" # Bersihkan input clean_text = filter_text(text) timestamp = datetime.datetime.now().strftime("%H:%M") # Buat Object Pesan new_msg = { "id": len(MESSAGES) + 1, "user": username, "text": clean_text, "time": timestamp, "role": role } # Masukkan ke list MESSAGES.append(new_msg) # Hapus pesan lama jika melebihi batas if len(MESSAGES) > MAX_HISTORY: MESSAGES = MESSAGES[-MAX_HISTORY:] return MESSAGES def get_messages(): """Mengambil semua pesan untuk ditampilkan di Frontend""" return MESSAGES def clear_chat(secret_key): """Fitur Admin untuk menghapus semua chat""" global MESSAGES if secret_key == "P3PSI21": # Password Admin kamu MESSAGES = [] return "Chat Cleared" return "Wrong Key" # --- API INTERFACE --- with gr.Blocks() as app: gr.Markdown("# SODA GLOBAL CHAT SERVER") # Inputs username_in = gr.Textbox(label="Username") msg_in = gr.Textbox(label="Message") secret_in = gr.Textbox(label="Admin Key") # Output JSON out_json = gr.JSON() # API Routes # 1. Kirim Pesan btn_send = gr.Button("Send") btn_send.click(add_message, [username_in, msg_in], out_json, api_name="send") # 2. Ambil Pesan (Refresh) btn_get = gr.Button("Get") btn_get.click(get_messages, None, out_json, api_name="get_chats") # 3. Hapus Chat (Admin Only) btn_clear = gr.Button("Clear") btn_clear.click(clear_chat, [secret_in], out_json, api_name="clear_chats") app.queue().launch(server_name="0.0.0.0")