Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,6 +31,15 @@ CREATE TABLE IF NOT EXISTS messages(
|
|
| 31 |
''')
|
| 32 |
conn.commit()
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# ---------------- Utility ----------------
|
| 35 |
def hash_password(password):
|
| 36 |
return hashlib.sha256(password.encode()).hexdigest()
|
|
@@ -53,42 +62,43 @@ def login(username, password):
|
|
| 53 |
else:
|
| 54 |
return "Login failed!", gr.update(visible=False), None
|
| 55 |
|
| 56 |
-
def send_message(sender_id,
|
| 57 |
-
|
| 58 |
-
receiver = cursor.fetchone()
|
| 59 |
-
if not receiver:
|
| 60 |
-
return "Receiver not found!"
|
| 61 |
-
receiver_id = receiver[0]
|
| 62 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 63 |
cursor.execute("INSERT INTO messages(sender_id,receiver_id,message,timestamp) VALUES (?,?,?,?)",
|
| 64 |
(sender_id, receiver_id, message, timestamp))
|
| 65 |
conn.commit()
|
| 66 |
-
return "
|
| 67 |
-
|
| 68 |
-
def
|
| 69 |
-
cursor.execute("SELECT id FROM users WHERE username=?", (other_username,))
|
| 70 |
-
other = cursor.fetchone()
|
| 71 |
-
if not other:
|
| 72 |
-
return "No such user!"
|
| 73 |
-
other_id = other[0]
|
| 74 |
cursor.execute("""
|
| 75 |
SELECT sender_id, message, timestamp FROM messages
|
| 76 |
WHERE (sender_id=? AND receiver_id=?) OR (sender_id=? AND receiver_id=?)
|
| 77 |
ORDER BY id ASC
|
| 78 |
-
""", (user_id,
|
| 79 |
msgs = cursor.fetchall()
|
| 80 |
result = ""
|
| 81 |
for s_id, msg, ts in msgs:
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
if not result:
|
| 85 |
return "No messages yet."
|
| 86 |
return result
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# ---------------- Gradio ----------------
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
-
gr.Markdown("## 💬
|
| 91 |
-
|
| 92 |
# Login/Register
|
| 93 |
with gr.Row():
|
| 94 |
username = gr.Textbox(label="Username")
|
|
@@ -96,22 +106,45 @@ with gr.Blocks() as demo:
|
|
| 96 |
login_btn = gr.Button("Login")
|
| 97 |
register_btn = gr.Button("Register")
|
| 98 |
login_status = gr.Label("")
|
| 99 |
-
|
| 100 |
chat_ui = gr.Column(visible=False)
|
| 101 |
current_user_id = gr.State(value=None)
|
| 102 |
-
|
| 103 |
login_btn.click(login, [username, password], [login_status, chat_ui, current_user_id])
|
| 104 |
register_btn.click(register, [username, password], login_status)
|
| 105 |
-
|
| 106 |
-
# Chat UI
|
| 107 |
with chat_ui:
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
send_btn = gr.Button("Send")
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
|
|
|
| 31 |
''')
|
| 32 |
conn.commit()
|
| 33 |
|
| 34 |
+
# ---------------- Default Admin ----------------
|
| 35 |
+
DEFAULT_ADMIN = "admin"
|
| 36 |
+
DEFAULT_PASSWORD = "1234"
|
| 37 |
+
cursor.execute("SELECT * FROM users WHERE username=?", (DEFAULT_ADMIN,))
|
| 38 |
+
if not cursor.fetchone():
|
| 39 |
+
cursor.execute("INSERT INTO users(username,password_hash) VALUES (?,?)",
|
| 40 |
+
(DEFAULT_ADMIN, hashlib.sha256(DEFAULT_PASSWORD.encode()).hexdigest()))
|
| 41 |
+
conn.commit()
|
| 42 |
+
|
| 43 |
# ---------------- Utility ----------------
|
| 44 |
def hash_password(password):
|
| 45 |
return hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
| 62 |
else:
|
| 63 |
return "Login failed!", gr.update(visible=False), None
|
| 64 |
|
| 65 |
+
def send_message(sender_id, receiver_id, message):
|
| 66 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
cursor.execute("INSERT INTO messages(sender_id,receiver_id,message,timestamp) VALUES (?,?,?,?)",
|
| 68 |
(sender_id, receiver_id, message, timestamp))
|
| 69 |
conn.commit()
|
| 70 |
+
return ""
|
| 71 |
+
|
| 72 |
+
def get_messages_html(user_id, receiver_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
cursor.execute("""
|
| 74 |
SELECT sender_id, message, timestamp FROM messages
|
| 75 |
WHERE (sender_id=? AND receiver_id=?) OR (sender_id=? AND receiver_id=?)
|
| 76 |
ORDER BY id ASC
|
| 77 |
+
""", (user_id, receiver_id, receiver_id, user_id))
|
| 78 |
msgs = cursor.fetchall()
|
| 79 |
result = ""
|
| 80 |
for s_id, msg, ts in msgs:
|
| 81 |
+
if s_id == user_id:
|
| 82 |
+
# Your messages on the right
|
| 83 |
+
result += f'<div style="text-align:right; margin:5px;"><span style="background:#DCF8C6; padding:8px; border-radius:10px; display:inline-block;">{msg} <br><small>{ts}</small></span></div>'
|
| 84 |
+
else:
|
| 85 |
+
# Friend messages on the left
|
| 86 |
+
result += f'<div style="text-align:left; margin:5px;"><span style="background:#FFF; padding:8px; border-radius:10px; display:inline-block;">{msg} <br><small>{ts}</small></span></div>'
|
| 87 |
if not result:
|
| 88 |
return "No messages yet."
|
| 89 |
return result
|
| 90 |
|
| 91 |
+
def get_all_users(exclude_id):
|
| 92 |
+
cursor.execute("SELECT username FROM users WHERE id!=?", (exclude_id,))
|
| 93 |
+
users = [u[0] for u in cursor.fetchall()]
|
| 94 |
+
if not users:
|
| 95 |
+
users = ["No other users"]
|
| 96 |
+
return gr.Dropdown.update(choices=users)
|
| 97 |
+
|
| 98 |
# ---------------- Gradio ----------------
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
+
gr.Markdown("## 💬 WhatsApp-Style Prototype")
|
| 101 |
+
|
| 102 |
# Login/Register
|
| 103 |
with gr.Row():
|
| 104 |
username = gr.Textbox(label="Username")
|
|
|
|
| 106 |
login_btn = gr.Button("Login")
|
| 107 |
register_btn = gr.Button("Register")
|
| 108 |
login_status = gr.Label("")
|
| 109 |
+
|
| 110 |
chat_ui = gr.Column(visible=False)
|
| 111 |
current_user_id = gr.State(value=None)
|
| 112 |
+
|
| 113 |
login_btn.click(login, [username, password], [login_status, chat_ui, current_user_id])
|
| 114 |
register_btn.click(register, [username, password], login_status)
|
| 115 |
+
|
| 116 |
+
# ---------------- Chat UI ----------------
|
| 117 |
with chat_ui:
|
| 118 |
+
with gr.Row():
|
| 119 |
+
receiver_dropdown = gr.Dropdown(label="Select User")
|
| 120 |
+
message_input = gr.Textbox(label="Message", lines=2)
|
| 121 |
send_btn = gr.Button("Send")
|
| 122 |
+
chat_display = gr.HTML(label="Chat")
|
| 123 |
+
|
| 124 |
+
# Update dropdown dynamically
|
| 125 |
+
receiver_dropdown.change(get_all_users, current_user_id, receiver_dropdown)
|
| 126 |
+
|
| 127 |
+
# Send message
|
| 128 |
+
def send_and_refresh(sender_id, receiver, msg):
|
| 129 |
+
if not receiver or not msg:
|
| 130 |
+
return ""
|
| 131 |
+
cursor.execute("SELECT id FROM users WHERE username=?", (receiver,))
|
| 132 |
+
r = cursor.fetchone()
|
| 133 |
+
if not r:
|
| 134 |
+
return "Receiver not found."
|
| 135 |
+
send_message(sender_id, r[0], msg)
|
| 136 |
+
return get_messages_html(sender_id, r[0])
|
| 137 |
+
|
| 138 |
+
send_btn.click(send_and_refresh, [current_user_id, receiver_dropdown, message_input], chat_display)
|
| 139 |
+
|
| 140 |
+
# Real-time refresh
|
| 141 |
+
def refresh(user_id, receiver):
|
| 142 |
+
if not receiver:
|
| 143 |
+
return ""
|
| 144 |
+
cursor.execute("SELECT id FROM users WHERE username=?", (receiver,))
|
| 145 |
+
r = cursor.fetchone()
|
| 146 |
+
if not r:
|
| 147 |
+
return "Receiver not found."
|
| 148 |
+
return get_messages_html(user_id, r[0])
|
| 149 |
|
| 150 |
+
chat_display.render(refresh, [current_user_id, receiver_dropdown], chat_display, every=2)
|