Update app.py
Browse files
app.py
CHANGED
|
@@ -5,461 +5,326 @@ import json
|
|
| 5 |
import hashlib
|
| 6 |
import uuid
|
| 7 |
from datetime import datetime
|
| 8 |
-
import numpy as np
|
| 9 |
|
| 10 |
-
# ==============================
|
| 11 |
-
# STORAGE
|
| 12 |
-
# ==============================
|
| 13 |
|
| 14 |
USERS_FILE = "users.json"
|
| 15 |
POSTS_FILE = "posts.json"
|
| 16 |
|
|
|
|
|
|
|
| 17 |
def load_json(file):
|
| 18 |
if os.path.exists(file):
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
return {}
|
| 22 |
|
| 23 |
-
def save_json(file,
|
| 24 |
-
with open(file,
|
| 25 |
-
json.dump(data,
|
| 26 |
|
| 27 |
users = load_json(USERS_FILE)
|
| 28 |
posts = load_json(POSTS_FILE)
|
| 29 |
-
os.makedirs("posts", exist_ok=True)
|
| 30 |
|
| 31 |
-
# ==============================
|
| 32 |
-
# AUTH
|
| 33 |
-
# ==============================
|
| 34 |
|
| 35 |
-
def hash_password(
|
| 36 |
-
return hashlib.sha256(
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
def signup(username, password, display_name):
|
| 39 |
-
if not username or not password:
|
| 40 |
-
return "❌ Username and password required"
|
| 41 |
-
|
| 42 |
if username in users:
|
| 43 |
-
return "
|
| 44 |
-
|
| 45 |
users[username] = {
|
| 46 |
"password": hash_password(password),
|
| 47 |
-
"display_name":
|
| 48 |
-
"bio":
|
| 49 |
-
"followers":
|
| 50 |
-
"following":
|
| 51 |
-
"created_at": datetime.now().isoformat()
|
| 52 |
}
|
| 53 |
-
save_json(USERS_FILE, users)
|
| 54 |
-
return "✅ Account created! Please login."
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if username not in users:
|
| 58 |
-
return "
|
| 59 |
-
|
| 60 |
if users[username]["password"] != hash_password(password):
|
| 61 |
-
return "
|
| 62 |
-
|
| 63 |
-
return
|
| 64 |
-
|
| 65 |
-
# ==============================
|
| 66 |
-
#
|
| 67 |
-
# ==============================
|
| 68 |
-
|
| 69 |
-
def create_post(
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
if image is None:
|
| 74 |
-
return "
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
if max(img.size) > max_size:
|
| 95 |
-
ratio = max_size / max(img.size)
|
| 96 |
-
new_size = (int(img.size[0] * ratio), int(img.size[1] * ratio))
|
| 97 |
-
img = img.resize(new_size, Image.Resampling.LANCZOS)
|
| 98 |
-
|
| 99 |
-
# Save image
|
| 100 |
-
img.save(f"posts/{post_id}.jpg", "JPEG", quality=85)
|
| 101 |
-
|
| 102 |
-
# Save post data
|
| 103 |
-
posts[post_id] = {
|
| 104 |
-
"id": post_id,
|
| 105 |
-
"username": username,
|
| 106 |
-
"display_name": users[username]["display_name"],
|
| 107 |
-
"caption": caption if caption else "",
|
| 108 |
-
"hashtags": hashtags if hashtags else "",
|
| 109 |
-
"image": f"posts/{post_id}.jpg",
|
| 110 |
-
"likes": [],
|
| 111 |
-
"comments": [],
|
| 112 |
-
"timestamp": datetime.now().isoformat()
|
| 113 |
-
}
|
| 114 |
-
save_json(POSTS_FILE, posts)
|
| 115 |
-
|
| 116 |
-
return "✅ Post shared successfully!"
|
| 117 |
-
except Exception as e:
|
| 118 |
-
return f"❌ Error: {str(e)}"
|
| 119 |
-
|
| 120 |
-
def get_feed():
|
| 121 |
-
feed_items = []
|
| 122 |
-
for pid, post in list(posts.items())[-30:]:
|
| 123 |
-
feed_items.append({
|
| 124 |
-
"id": pid,
|
| 125 |
-
"username": post["username"],
|
| 126 |
-
"display_name": post.get("display_name", post["username"]),
|
| 127 |
-
"image": post["image"],
|
| 128 |
-
"caption": post["caption"],
|
| 129 |
-
"likes": len(post["likes"]),
|
| 130 |
-
"timestamp": post["timestamp"]
|
| 131 |
-
})
|
| 132 |
-
return feed_items[::-1]
|
| 133 |
-
|
| 134 |
-
def get_user_posts(username):
|
| 135 |
-
user_posts = []
|
| 136 |
-
for pid, post in posts.items():
|
| 137 |
-
if post["username"] == username:
|
| 138 |
-
user_posts.append(post["image"])
|
| 139 |
-
return user_posts[-9:]
|
| 140 |
-
|
| 141 |
-
def get_user_stats(username):
|
| 142 |
-
if username not in users:
|
| 143 |
-
return {"display_name": "", "bio": "", "followers": 0, "following": 0, "posts": 0}
|
| 144 |
-
|
| 145 |
-
user = users[username]
|
| 146 |
-
user_posts = [p for p in posts.values() if p["username"] == username]
|
| 147 |
-
|
| 148 |
-
return {
|
| 149 |
-
"display_name": user["display_name"],
|
| 150 |
-
"bio": user.get("bio", "✨ New to Vibes! ✨"),
|
| 151 |
-
"followers": len(user["followers"]),
|
| 152 |
-
"following": len(user["following"]),
|
| 153 |
-
"posts": len(user_posts)
|
| 154 |
}
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
| 162 |
|
| 163 |
-
def
|
| 164 |
-
|
|
|
|
| 165 |
return []
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
.
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
}
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
border: 1px solid #3d3d3d !important;
|
| 227 |
-
background: #2d2d2d !important;
|
| 228 |
-
padding: 12px 20px !important;
|
| 229 |
-
font-size: 14px !important;
|
| 230 |
-
color: #ffffff !important;
|
| 231 |
-
}
|
| 232 |
-
|
| 233 |
-
input:focus, textarea:focus {
|
| 234 |
-
border-color: #FF6B35 !important;
|
| 235 |
-
outline: none !important;
|
| 236 |
-
background: #3d3d3d !important;
|
| 237 |
-
}
|
| 238 |
-
|
| 239 |
-
label, .gr-form-label {
|
| 240 |
-
color: #ffffff !important;
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
.gallery {
|
| 244 |
-
border-radius: 16px !important;
|
| 245 |
-
background: transparent !important;
|
| 246 |
-
gap: 16px !important;
|
| 247 |
-
}
|
| 248 |
-
|
| 249 |
-
.tab-nav {
|
| 250 |
-
background: transparent !important;
|
| 251 |
-
border-bottom: 1px solid #3d3d3d !important;
|
| 252 |
-
margin-bottom: 20px !important;
|
| 253 |
-
}
|
| 254 |
-
|
| 255 |
-
.tab-nav button {
|
| 256 |
-
font-size: 16px !important;
|
| 257 |
-
padding: 12px 24px !important;
|
| 258 |
-
border-radius: 0 !important;
|
| 259 |
-
background: transparent !important;
|
| 260 |
-
color: #888888 !important;
|
| 261 |
-
border-bottom: 2px solid transparent !important;
|
| 262 |
-
}
|
| 263 |
-
|
| 264 |
-
.tab-nav button.selected {
|
| 265 |
-
color: #FF6B35 !important;
|
| 266 |
-
border-bottom: 2px solid #FF6B35 !important;
|
| 267 |
-
}
|
| 268 |
-
|
| 269 |
-
.profile-card {
|
| 270 |
-
background: linear-gradient(135deg, #FF6B35 0%, #F7931E 100%);
|
| 271 |
-
border-radius: 20px;
|
| 272 |
-
padding: 24px;
|
| 273 |
-
color: white;
|
| 274 |
-
text-align: center;
|
| 275 |
-
}
|
| 276 |
-
|
| 277 |
-
h1, h2, h3, .gr-markdown {
|
| 278 |
-
color: #ffffff !important;
|
| 279 |
-
}
|
| 280 |
-
|
| 281 |
-
.gr-markdown p {
|
| 282 |
-
color: #cccccc !important;
|
| 283 |
-
}
|
| 284 |
"""
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
# ==============================
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
# Wire events
|
| 445 |
-
signup_btn.click(handle_signup, [signup_user, signup_pass, signup_display], [signup_msg, signup_user, signup_pass, signup_display])
|
| 446 |
-
login_btn.click(handle_login, [login_user, login_pass], [login_msg, login_panel, main_panel, current_user, user_display])
|
| 447 |
-
login_btn.click(lambda: load_feed(), None, [feed_gallery, feed_info])
|
| 448 |
-
login_btn.click(lambda u: load_profile(u), [current_user], [profile_stats, my_posts_gallery])
|
| 449 |
-
|
| 450 |
-
logout_btn.click(handle_logout, None, [login_panel, main_panel, current_user, user_display])
|
| 451 |
-
|
| 452 |
-
post_btn.click(handle_create_post, [current_user, post_image, post_caption, post_hashtags], [post_msg])
|
| 453 |
-
post_btn.click(lambda: None, None, [post_image, post_caption, post_hashtags])
|
| 454 |
-
post_btn.click(lambda: load_feed(), None, [feed_gallery, feed_info])
|
| 455 |
-
post_btn.click(lambda u: load_profile(u), [current_user], [profile_stats, my_posts_gallery])
|
| 456 |
-
|
| 457 |
-
refresh_btn.click(lambda: load_feed(), None, [feed_gallery, feed_info])
|
| 458 |
-
refresh_profile_btn.click(lambda u: load_profile(u), [current_user], [profile_stats, my_posts_gallery])
|
| 459 |
-
|
| 460 |
-
update_bio_btn.click(handle_update_bio, [current_user, update_bio_text], [bio_status])
|
| 461 |
-
update_bio_btn.click(lambda u: load_profile(u), [current_user], [profile_stats, my_posts_gallery])
|
| 462 |
-
|
| 463 |
-
search_input.change(search_users, [search_input], [search_results])
|
| 464 |
-
|
| 465 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 5 |
import hashlib
|
| 6 |
import uuid
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
+
# ==============================
|
| 10 |
+
# FILE STORAGE
|
| 11 |
+
# ==============================
|
| 12 |
|
| 13 |
USERS_FILE = "users.json"
|
| 14 |
POSTS_FILE = "posts.json"
|
| 15 |
|
| 16 |
+
os.makedirs("posts", exist_ok=True)
|
| 17 |
+
|
| 18 |
def load_json(file):
|
| 19 |
if os.path.exists(file):
|
| 20 |
+
try:
|
| 21 |
+
with open(file) as f:
|
| 22 |
+
return json.load(f)
|
| 23 |
+
except:
|
| 24 |
+
return {}
|
| 25 |
return {}
|
| 26 |
|
| 27 |
+
def save_json(file,data):
|
| 28 |
+
with open(file,"w") as f:
|
| 29 |
+
json.dump(data,f,indent=2)
|
| 30 |
|
| 31 |
users = load_json(USERS_FILE)
|
| 32 |
posts = load_json(POSTS_FILE)
|
|
|
|
| 33 |
|
| 34 |
+
# ==============================
|
| 35 |
+
# AUTH
|
| 36 |
+
# ==============================
|
| 37 |
|
| 38 |
+
def hash_password(p):
|
| 39 |
+
return hashlib.sha256(p.encode()).hexdigest()
|
| 40 |
+
|
| 41 |
+
def signup(username,password,display):
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if username in users:
|
| 44 |
+
return "Username already exists"
|
| 45 |
+
|
| 46 |
users[username] = {
|
| 47 |
"password": hash_password(password),
|
| 48 |
+
"display_name": display or username,
|
| 49 |
+
"bio":"New to Vibes ✨",
|
| 50 |
+
"followers":[],
|
| 51 |
+
"following":[]
|
|
|
|
| 52 |
}
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
save_json(USERS_FILE,users)
|
| 55 |
+
return "Account created! Login now."
|
| 56 |
+
|
| 57 |
+
def login(username,password):
|
| 58 |
+
|
| 59 |
if username not in users:
|
| 60 |
+
return "User not found",None
|
| 61 |
+
|
| 62 |
if users[username]["password"] != hash_password(password):
|
| 63 |
+
return "Wrong password",None
|
| 64 |
+
|
| 65 |
+
return "Login successful",username
|
| 66 |
+
|
| 67 |
+
# ==============================
|
| 68 |
+
# POSTS
|
| 69 |
+
# ==============================
|
| 70 |
+
|
| 71 |
+
def create_post(user,image,caption):
|
| 72 |
+
|
| 73 |
+
if user is None:
|
| 74 |
+
return "Login required"
|
| 75 |
+
|
| 76 |
if image is None:
|
| 77 |
+
return "Upload image"
|
| 78 |
+
|
| 79 |
+
post_id=str(uuid.uuid4())
|
| 80 |
+
|
| 81 |
+
img = Image.fromarray(image)
|
| 82 |
+
img = img.convert("RGB")
|
| 83 |
+
|
| 84 |
+
path=f"posts/{post_id}.jpg"
|
| 85 |
+
|
| 86 |
+
img.save(path,quality=90)
|
| 87 |
+
|
| 88 |
+
posts[post_id]={
|
| 89 |
+
"id":post_id,
|
| 90 |
+
"username":user,
|
| 91 |
+
"display":users[user]["display_name"],
|
| 92 |
+
"caption":caption,
|
| 93 |
+
"image":path,
|
| 94 |
+
"likes":[],
|
| 95 |
+
"comments":[],
|
| 96 |
+
"time":datetime.now().isoformat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
+
save_json(POSTS_FILE,posts)
|
| 100 |
+
|
| 101 |
+
return "Post uploaded"
|
| 102 |
+
|
| 103 |
+
# ==============================
|
| 104 |
+
# FEED
|
| 105 |
+
# ==============================
|
| 106 |
|
| 107 |
+
def load_feed():
|
| 108 |
+
|
| 109 |
+
if len(posts)==0:
|
| 110 |
return []
|
| 111 |
+
|
| 112 |
+
ordered=sorted(posts.values(),
|
| 113 |
+
key=lambda x:x["time"],
|
| 114 |
+
reverse=True)
|
| 115 |
+
|
| 116 |
+
gallery=[]
|
| 117 |
+
|
| 118 |
+
for p in ordered:
|
| 119 |
+
|
| 120 |
+
caption=f"""
|
| 121 |
+
{p['display']} (@{p['username']})
|
| 122 |
+
|
| 123 |
+
{p['caption']}
|
| 124 |
+
|
| 125 |
+
❤️ {len(p['likes'])} likes
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
gallery.append((p["image"],caption))
|
| 129 |
+
|
| 130 |
+
return gallery
|
| 131 |
+
|
| 132 |
+
# ==============================
|
| 133 |
+
# LIKE
|
| 134 |
+
# ==============================
|
| 135 |
+
|
| 136 |
+
def like_latest(user):
|
| 137 |
+
|
| 138 |
+
if not posts:
|
| 139 |
+
return "No posts"
|
| 140 |
+
|
| 141 |
+
latest=list(posts.keys())[-1]
|
| 142 |
+
|
| 143 |
+
if user not in posts[latest]["likes"]:
|
| 144 |
+
posts[latest]["likes"].append(user)
|
| 145 |
+
|
| 146 |
+
save_json(POSTS_FILE,posts)
|
| 147 |
+
|
| 148 |
+
return "Liked ❤️"
|
| 149 |
+
|
| 150 |
+
# ==============================
|
| 151 |
+
# PROFILE
|
| 152 |
+
# ==============================
|
| 153 |
+
|
| 154 |
+
def load_profile(user):
|
| 155 |
+
|
| 156 |
+
if user is None:
|
| 157 |
+
return "Not logged",[]
|
| 158 |
+
|
| 159 |
+
user_posts=[p["image"] for p in posts.values() if p["username"]==user]
|
| 160 |
+
|
| 161 |
+
stats=f"""
|
| 162 |
+
### {users[user]["display_name"]}
|
| 163 |
+
|
| 164 |
+
@{user}
|
| 165 |
+
|
| 166 |
+
Posts: {len(user_posts)}
|
| 167 |
+
|
| 168 |
+
Followers: {len(users[user]["followers"])}
|
| 169 |
+
|
| 170 |
+
Following: {len(users[user]["following"])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
"""
|
| 172 |
|
| 173 |
+
return stats,user_posts
|
| 174 |
+
|
| 175 |
+
# ==============================
|
| 176 |
+
# SEARCH
|
| 177 |
+
# ==============================
|
| 178 |
+
|
| 179 |
+
def search_users(q):
|
| 180 |
+
|
| 181 |
+
if len(q)<2:
|
| 182 |
+
return []
|
| 183 |
+
|
| 184 |
+
results=[]
|
| 185 |
+
|
| 186 |
+
for u,data in users.items():
|
| 187 |
+
|
| 188 |
+
if q.lower() in u.lower():
|
| 189 |
+
|
| 190 |
+
results.append([
|
| 191 |
+
u,
|
| 192 |
+
data["display_name"],
|
| 193 |
+
len(data["followers"])
|
| 194 |
+
])
|
| 195 |
+
|
| 196 |
+
return results
|
| 197 |
+
|
| 198 |
+
# ==============================
|
| 199 |
+
# UI
|
| 200 |
+
# ==============================
|
| 201 |
+
|
| 202 |
+
with gr.Blocks(title="Vibes") as app:
|
| 203 |
+
|
| 204 |
+
current_user=gr.State()
|
| 205 |
+
|
| 206 |
+
gr.Markdown("# 📸 Vibes")
|
| 207 |
+
|
| 208 |
+
# LOGIN
|
| 209 |
+
with gr.Tab("Login"):
|
| 210 |
+
|
| 211 |
+
user=gr.Textbox(label="Username")
|
| 212 |
+
pwd=gr.Textbox(label="Password",type="password")
|
| 213 |
+
|
| 214 |
+
login_btn=gr.Button("Login")
|
| 215 |
+
|
| 216 |
+
login_msg=gr.Textbox()
|
| 217 |
+
|
| 218 |
+
# SIGNUP
|
| 219 |
+
with gr.Tab("Signup"):
|
| 220 |
+
|
| 221 |
+
su_user=gr.Textbox(label="Username")
|
| 222 |
+
su_pwd=gr.Textbox(label="Password",type="password")
|
| 223 |
+
su_disp=gr.Textbox(label="Display name")
|
| 224 |
+
|
| 225 |
+
su_btn=gr.Button("Create account")
|
| 226 |
+
|
| 227 |
+
su_msg=gr.Textbox()
|
| 228 |
+
|
| 229 |
+
# FEED
|
| 230 |
+
with gr.Tab("Feed"):
|
| 231 |
+
|
| 232 |
+
gallery=gr.Gallery(
|
| 233 |
+
columns=3,
|
| 234 |
+
height=600
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
refresh=gr.Button("Refresh")
|
| 238 |
+
|
| 239 |
+
like_btn=gr.Button("Like latest")
|
| 240 |
+
|
| 241 |
+
like_msg=gr.Textbox()
|
| 242 |
+
|
| 243 |
+
# CREATE
|
| 244 |
+
with gr.Tab("Create Post"):
|
| 245 |
+
|
| 246 |
+
img=gr.Image(type="numpy")
|
| 247 |
+
|
| 248 |
+
cap=gr.Textbox(label="Caption")
|
| 249 |
+
|
| 250 |
+
post_btn=gr.Button("Share")
|
| 251 |
+
|
| 252 |
+
post_msg=gr.Textbox()
|
| 253 |
+
|
| 254 |
+
# PROFILE
|
| 255 |
+
with gr.Tab("Profile"):
|
| 256 |
+
|
| 257 |
+
profile_stats=gr.Markdown()
|
| 258 |
+
|
| 259 |
+
my_gallery=gr.Gallery(columns=3)
|
| 260 |
+
|
| 261 |
+
load_profile_btn=gr.Button("Load profile")
|
| 262 |
+
|
| 263 |
+
# DISCOVER
|
| 264 |
+
with gr.Tab("Discover"):
|
| 265 |
+
|
| 266 |
+
search=gr.Textbox(label="Search users")
|
| 267 |
+
|
| 268 |
+
results=gr.Dataframe(
|
| 269 |
+
headers=["username","display name","followers"]
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
# =================
|
| 273 |
+
# EVENTS
|
| 274 |
+
# =================
|
| 275 |
+
|
| 276 |
+
login_btn.click(
|
| 277 |
+
login,
|
| 278 |
+
inputs=[user,pwd],
|
| 279 |
+
outputs=[login_msg,current_user]
|
| 280 |
+
).then(
|
| 281 |
+
load_feed,
|
| 282 |
+
None,
|
| 283 |
+
gallery
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
su_btn.click(
|
| 287 |
+
signup,
|
| 288 |
+
[su_user,su_pwd,su_disp],
|
| 289 |
+
su_msg
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
refresh.click(
|
| 293 |
+
load_feed,
|
| 294 |
+
None,
|
| 295 |
+
gallery
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
post_btn.click(
|
| 299 |
+
create_post,
|
| 300 |
+
[current_user,img,cap],
|
| 301 |
+
post_msg
|
| 302 |
+
).then(
|
| 303 |
+
load_feed,
|
| 304 |
+
None,
|
| 305 |
+
gallery
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
like_btn.click(
|
| 309 |
+
like_latest,
|
| 310 |
+
current_user,
|
| 311 |
+
like_msg
|
| 312 |
+
).then(
|
| 313 |
+
load_feed,
|
| 314 |
+
None,
|
| 315 |
+
gallery
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
load_profile_btn.click(
|
| 319 |
+
load_profile,
|
| 320 |
+
current_user,
|
| 321 |
+
[profile_stats,my_gallery]
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
search.change(
|
| 325 |
+
search_users,
|
| 326 |
+
search,
|
| 327 |
+
results
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|