Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,38 +36,45 @@ 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(
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
conn.commit()
|
| 42 |
|
| 43 |
-
# ---------------- Utility ----------------
|
| 44 |
def hash_password(password):
|
| 45 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 46 |
|
| 47 |
def register(username, password):
|
| 48 |
try:
|
| 49 |
-
cursor.execute(
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
conn.commit()
|
| 52 |
return "Account created successfully!"
|
| 53 |
except sqlite3.IntegrityError:
|
| 54 |
return "Username already exists!"
|
| 55 |
|
| 56 |
def login(username, password):
|
| 57 |
-
cursor.execute(
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
user = cursor.fetchone()
|
| 60 |
if user:
|
| 61 |
-
return "Login successful!", gr.update(visible=True), user[0]
|
| 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(
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
conn.commit()
|
| 70 |
-
return ""
|
| 71 |
|
| 72 |
def get_messages_html(user_id, receiver_id):
|
| 73 |
cursor.execute("""
|
|
@@ -79,25 +86,21 @@ def get_messages_html(user_id, receiver_id):
|
|
| 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 |
-
|
| 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
|
| 97 |
|
| 98 |
-
# ---------------- Gradio ----------------
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
-
gr.Markdown("## 💬 WhatsApp-Style
|
| 101 |
|
| 102 |
# Login/Register
|
| 103 |
with gr.Row():
|
|
@@ -110,10 +113,11 @@ with gr.Blocks() as demo:
|
|
| 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
|
| 117 |
with chat_ui:
|
| 118 |
with gr.Row():
|
| 119 |
receiver_dropdown = gr.Dropdown(label="Select User")
|
|
@@ -121,23 +125,29 @@ with gr.Blocks() as demo:
|
|
| 121 |
send_btn = gr.Button("Send")
|
| 122 |
chat_display = gr.HTML(label="Chat")
|
| 123 |
|
| 124 |
-
# Update dropdown dynamically
|
| 125 |
-
|
|
|
|
|
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
# Real-time refresh
|
| 141 |
def refresh(user_id, receiver):
|
| 142 |
if not receiver:
|
| 143 |
return ""
|
|
@@ -147,4 +157,6 @@ with gr.Blocks() as demo:
|
|
| 147 |
return "Receiver not found."
|
| 148 |
return get_messages_html(user_id, r[0])
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
| 36 |
DEFAULT_PASSWORD = "1234"
|
| 37 |
cursor.execute("SELECT * FROM users WHERE username=?", (DEFAULT_ADMIN,))
|
| 38 |
if not cursor.fetchone():
|
| 39 |
+
cursor.execute(
|
| 40 |
+
"INSERT INTO users(username,password_hash) VALUES (?,?)",
|
| 41 |
+
(DEFAULT_ADMIN, hashlib.sha256(DEFAULT_PASSWORD.encode()).hexdigest())
|
| 42 |
+
)
|
| 43 |
conn.commit()
|
| 44 |
|
| 45 |
+
# ---------------- Utility Functions ----------------
|
| 46 |
def hash_password(password):
|
| 47 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 48 |
|
| 49 |
def register(username, password):
|
| 50 |
try:
|
| 51 |
+
cursor.execute(
|
| 52 |
+
"INSERT INTO users(username,password_hash) VALUES (?,?)",
|
| 53 |
+
(username, hash_password(password))
|
| 54 |
+
)
|
| 55 |
conn.commit()
|
| 56 |
return "Account created successfully!"
|
| 57 |
except sqlite3.IntegrityError:
|
| 58 |
return "Username already exists!"
|
| 59 |
|
| 60 |
def login(username, password):
|
| 61 |
+
cursor.execute(
|
| 62 |
+
"SELECT * FROM users WHERE username=? AND password_hash=?",
|
| 63 |
+
(username, hash_password(password))
|
| 64 |
+
)
|
| 65 |
user = cursor.fetchone()
|
| 66 |
if user:
|
| 67 |
+
return "Login successful!", gr.update(visible=True), gr.State.update(value=user[0])
|
| 68 |
else:
|
| 69 |
+
return "Login failed!", gr.update(visible=False), gr.State.update(value=None)
|
| 70 |
|
| 71 |
def send_message(sender_id, receiver_id, message):
|
| 72 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 73 |
+
cursor.execute(
|
| 74 |
+
"INSERT INTO messages(sender_id,receiver_id,message,timestamp) VALUES (?,?,?,?)",
|
| 75 |
+
(sender_id, receiver_id, message, timestamp)
|
| 76 |
+
)
|
| 77 |
conn.commit()
|
|
|
|
| 78 |
|
| 79 |
def get_messages_html(user_id, receiver_id):
|
| 80 |
cursor.execute("""
|
|
|
|
| 86 |
result = ""
|
| 87 |
for s_id, msg, ts in msgs:
|
| 88 |
if s_id == user_id:
|
|
|
|
| 89 |
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>'
|
| 90 |
else:
|
|
|
|
| 91 |
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>'
|
| 92 |
+
return result if result else "No messages yet."
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def get_all_users(exclude_id):
|
| 95 |
cursor.execute("SELECT username FROM users WHERE id!=?", (exclude_id,))
|
| 96 |
users = [u[0] for u in cursor.fetchall()]
|
| 97 |
if not users:
|
| 98 |
users = ["No other users"]
|
| 99 |
+
return users
|
| 100 |
|
| 101 |
+
# ---------------- Gradio App ----------------
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("## 💬 WhatsApp-Style Chat App")
|
| 104 |
|
| 105 |
# Login/Register
|
| 106 |
with gr.Row():
|
|
|
|
| 113 |
chat_ui = gr.Column(visible=False)
|
| 114 |
current_user_id = gr.State(value=None)
|
| 115 |
|
| 116 |
+
# Login/Register events
|
| 117 |
login_btn.click(login, [username, password], [login_status, chat_ui, current_user_id])
|
| 118 |
register_btn.click(register, [username, password], login_status)
|
| 119 |
|
| 120 |
+
# ---------------- Chat Interface ----------------
|
| 121 |
with chat_ui:
|
| 122 |
with gr.Row():
|
| 123 |
receiver_dropdown = gr.Dropdown(label="Select User")
|
|
|
|
| 125 |
send_btn = gr.Button("Send")
|
| 126 |
chat_display = gr.HTML(label="Chat")
|
| 127 |
|
| 128 |
+
# Update dropdown dynamically after login
|
| 129 |
+
def update_dropdown(user_id):
|
| 130 |
+
return gr.Dropdown.update(choices=get_all_users(user_id))
|
| 131 |
+
current_user_id.change(update_dropdown, current_user_id, receiver_dropdown)
|
| 132 |
|
| 133 |
+
# Send message + refresh
|
| 134 |
def send_and_refresh(sender_id, receiver, msg):
|
| 135 |
if not receiver or not msg:
|
| 136 |
+
return "", gr.update(value="")
|
| 137 |
cursor.execute("SELECT id FROM users WHERE username=?", (receiver,))
|
| 138 |
r = cursor.fetchone()
|
| 139 |
if not r:
|
| 140 |
+
return "Receiver not found.", gr.update(value="")
|
| 141 |
send_message(sender_id, r[0], msg)
|
| 142 |
+
return get_messages_html(sender_id, r[0]), gr.update(value="")
|
| 143 |
|
| 144 |
+
send_btn.click(
|
| 145 |
+
send_and_refresh,
|
| 146 |
+
[current_user_id, receiver_dropdown, message_input],
|
| 147 |
+
[chat_display, message_input]
|
| 148 |
+
)
|
| 149 |
|
| 150 |
+
# Real-time refresh with Timer
|
| 151 |
def refresh(user_id, receiver):
|
| 152 |
if not receiver:
|
| 153 |
return ""
|
|
|
|
| 157 |
return "Receiver not found."
|
| 158 |
return get_messages_html(user_id, r[0])
|
| 159 |
|
| 160 |
+
gr.Timer(interval=2, run=refresh, inputs=[current_user_id, receiver_dropdown], outputs=chat_display)
|
| 161 |
+
|
| 162 |
+
demo.launch()
|