Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# =========================
|
| 7 |
+
# ADVANCED SECURITY CONFIG
|
| 8 |
+
# =========================
|
| 9 |
+
|
| 10 |
+
# User database: format {user_id: password}
|
| 11 |
+
users_db = {
|
| 12 |
+
"user1": "pass123",
|
| 13 |
+
"user2": "abc456"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Blocked users set
|
| 17 |
+
blocked_users = set()
|
| 18 |
+
|
| 19 |
+
# Activity log
|
| 20 |
+
activity_log = pd.DataFrame(columns=["timestamp", "user_id", "message", "status"])
|
| 21 |
+
|
| 22 |
+
# Rate limiter tracking
|
| 23 |
+
user_activity = {}
|
| 24 |
+
|
| 25 |
+
# Load Hugging Face Toxicity Model
|
| 26 |
+
moderator = pipeline("text-classification", model="unitary/toxic-bert", top_k=None)
|
| 27 |
+
|
| 28 |
+
# =========================
|
| 29 |
+
# SECURITY FUNCTIONS
|
| 30 |
+
# =========================
|
| 31 |
+
|
| 32 |
+
def authenticate(user_id, password):
|
| 33 |
+
return users_db.get(user_id) == password
|
| 34 |
+
|
| 35 |
+
def is_user_allowed(user_id):
|
| 36 |
+
return user_id not in blocked_users
|
| 37 |
+
|
| 38 |
+
def can_user_send(user_id):
|
| 39 |
+
now = time.time()
|
| 40 |
+
if user_id not in user_activity:
|
| 41 |
+
user_activity[user_id] = []
|
| 42 |
+
|
| 43 |
+
# Keep only last 60 seconds
|
| 44 |
+
user_activity[user_id] = [t for t in user_activity[user_id] if now - t < 60]
|
| 45 |
+
|
| 46 |
+
if len(user_activity[user_id]) >= 5: # limit 5 messages/min
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
user_activity[user_id].append(now)
|
| 50 |
+
return True
|
| 51 |
+
|
| 52 |
+
def is_text_safe(text):
|
| 53 |
+
result = moderator(text)[0]
|
| 54 |
+
for label in result:
|
| 55 |
+
if label["label"] == "TOXIC" and label["score"] > 0.7:
|
| 56 |
+
return False
|
| 57 |
+
return True
|
| 58 |
+
|
| 59 |
+
def log_activity(user_id, message, status):
|
| 60 |
+
global activity_log
|
| 61 |
+
activity_log = pd.concat([activity_log, pd.DataFrame([{
|
| 62 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 63 |
+
"user_id": user_id,
|
| 64 |
+
"message": message,
|
| 65 |
+
"status": status
|
| 66 |
+
}])], ignore_index=True)
|
| 67 |
+
|
| 68 |
+
# =========================
|
| 69 |
+
# MAIN APP FUNCTIONS
|
| 70 |
+
# =========================
|
| 71 |
+
|
| 72 |
+
def secure_chat(user_id, password, message):
|
| 73 |
+
if not authenticate(user_id, password):
|
| 74 |
+
log_activity(user_id, message, "Failed Authentication")
|
| 75 |
+
return "β Invalid user ID or password."
|
| 76 |
+
|
| 77 |
+
if not is_user_allowed(user_id):
|
| 78 |
+
log_activity(user_id, message, "Blocked User")
|
| 79 |
+
return "π« You are blocked from using this app."
|
| 80 |
+
|
| 81 |
+
if not can_user_send(user_id):
|
| 82 |
+
log_activity(user_id, message, "Rate Limited")
|
| 83 |
+
return "β³ Too many requests. Wait 1 minute."
|
| 84 |
+
|
| 85 |
+
if not is_text_safe(message):
|
| 86 |
+
blocked_users.add(user_id)
|
| 87 |
+
log_activity(user_id, message, "Blocked for Toxicity")
|
| 88 |
+
return "β οΈ Inappropriate content detected. You are now blocked."
|
| 89 |
+
|
| 90 |
+
log_activity(user_id, message, "Accepted")
|
| 91 |
+
return f"β
Message Accepted: {message}"
|
| 92 |
+
|
| 93 |
+
# Admin panel functions
|
| 94 |
+
def unblock_user(admin_password, user_id):
|
| 95 |
+
if admin_password != "admin123": # simple admin password
|
| 96 |
+
return "β Invalid admin password."
|
| 97 |
+
|
| 98 |
+
blocked_users.discard(user_id)
|
| 99 |
+
return f"β
User {user_id} unblocked."
|
| 100 |
+
|
| 101 |
+
def view_logs(admin_password):
|
| 102 |
+
if admin_password != "admin123":
|
| 103 |
+
return "β Invalid admin password."
|
| 104 |
+
return activity_log.tail(20).to_string(index=False)
|
| 105 |
+
|
| 106 |
+
# =========================
|
| 107 |
+
# GRADIO UI
|
| 108 |
+
# =========================
|
| 109 |
+
|
| 110 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 111 |
+
gr.Markdown("## π Advanced Secure AI App")
|
| 112 |
+
gr.Markdown("This app includes advanced security features with AI moderation, login, and admin dashboard.")
|
| 113 |
+
|
| 114 |
+
with gr.Tab("User Interface"):
|
| 115 |
+
user_id_input = gr.Textbox(label="User ID")
|
| 116 |
+
password_input = gr.Textbox(label="Password", type="password")
|
| 117 |
+
message_input = gr.Textbox(label="Enter your message")
|
| 118 |
+
output = gr.Textbox(label="Response")
|
| 119 |
+
send_btn = gr.Button("Send")
|
| 120 |
+
send_btn.click(secure_chat, inputs=[user_id_input, password_input, message_input], outputs=output)
|
| 121 |
+
|
| 122 |
+
with gr.Tab("Admin Panel"):
|
| 123 |
+
admin_pass_input = gr.Textbox(label="Admin Password", type="password")
|
| 124 |
+
unblock_user_input = gr.Textbox(label="User ID to Unblock")
|
| 125 |
+
unblock_output = gr.Textbox(label="Admin Response")
|
| 126 |
+
unblock_btn = gr.Button("Unblock User")
|
| 127 |
+
unblock_btn.click(unblock_user, inputs=[admin_pass_input, unblock_user_input], outputs=unblock_output)
|
| 128 |
+
|
| 129 |
+
log_output = gr.Textbox(label="Recent Activity Logs")
|
| 130 |
+
log_btn = gr.Button("View Logs")
|
| 131 |
+
log_btn.click(view_logs, inputs=[admin_pass_input], outputs=log_output)
|
| 132 |
+
|
| 133 |
+
app.launch()
|
| 134 |
+
|