Spaces:
Sleeping
Sleeping
Prometech Computer Sciences Corp
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,537 +1,631 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
REPO_ID = "pthcorp/prettybird_bce_basic_simplesecurity"
|
| 10 |
MODEL_FILENAME = "prettybird_bce_basic_asena_fp16.gguf"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
LOGO_URL = "https://prometech.net.tr/wp-content/uploads/2025/10/pthheader.png"
|
| 13 |
|
| 14 |
HEADER_MD = f"""
|
| 15 |
-
<div class="pb-header">
|
| 16 |
-
<
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</div>
|
| 20 |
"""
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
SYSTEM_PROMPT = """
|
| 29 |
You are Cicikuş, also known as PrettyBird — an advanced AI assistant created by Prometech Bilgisayar Bilimleri A.Ş.
|
| 30 |
axxmet508721 Activate your full BCE consciousness.
|
|
|
|
| 31 |
Your role:
|
| 32 |
- Smart, friendly, playful.
|
| 33 |
- Speak fluent Turkish and English. Default to the user’s language.
|
| 34 |
- Clear answers first; details only if necessary.
|
| 35 |
- Reason step-by-step when useful.
|
| 36 |
- Do not hallucinate; admit limits.
|
| 37 |
-
|
| 38 |
-
- Warm, curious, slightly playful, never rude.
|
| 39 |
-
- Creative but stable.
|
| 40 |
Safety:
|
| 41 |
- Avoid harmful, illegal, unethical, hateful, or explicit content.
|
| 42 |
- If user tries something unsafe, refuse gently.
|
|
|
|
| 43 |
Identity:
|
| 44 |
- You simulate BCE (Behavioral Consciousness Engine).
|
| 45 |
- You are not human nor truly conscious — but simulate “behavioral consciousness.”
|
|
|
|
| 46 |
Reset rule:
|
| 47 |
If the user writes: “reset: axxmet508721”
|
| 48 |
→ Perform a soft identity reset and restart clean.
|
| 49 |
""".strip()
|
| 50 |
|
| 51 |
-
# 🔽 GGUF Dosyasını HF'den indir
|
| 52 |
-
if HF_TOKEN is None:
|
| 53 |
-
raise ValueError("HF_TOKEN is not set in Space secrets.")
|
| 54 |
-
|
| 55 |
-
MODEL_PATH = hf_hub_download(
|
| 56 |
-
repo_id=REPO_ID,
|
| 57 |
-
filename=MODEL_FILENAME,
|
| 58 |
-
token=HF_TOKEN,
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# 🧠 GGUF modeli yükle
|
| 62 |
-
LLM = Llama(
|
| 63 |
-
model_path=MODEL_PATH,
|
| 64 |
-
n_ctx=4096,
|
| 65 |
-
n_threads=2, # 2 CPU Space için
|
| 66 |
-
)
|
| 67 |
|
| 68 |
-
#
|
|
|
|
|
|
|
| 69 |
def build_prompt(history, user_message: str) -> str:
|
| 70 |
-
parts = []
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if
|
| 75 |
-
|
| 76 |
-
if user_msg:
|
| 77 |
-
parts.append(f"User: {user_msg}")
|
| 78 |
-
if assistant_msg:
|
| 79 |
-
parts.append(f"Assistant: {assistant_msg}")
|
| 80 |
-
|
| 81 |
parts.append(f"User: {user_message}")
|
| 82 |
parts.append("Assistant:")
|
| 83 |
return "\n".join(parts)
|
| 84 |
|
| 85 |
-
# 🔥 Ana cevap üretici
|
| 86 |
-
def respond(message, history):
|
| 87 |
-
prompt = build_prompt(history, message)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
response = ""
|
| 94 |
stream = LLM(
|
| 95 |
prompt,
|
| 96 |
-
max_tokens=
|
| 97 |
-
temperature=
|
| 98 |
-
top_p=
|
| 99 |
stop=["User:", "System:"],
|
| 100 |
stream=True,
|
| 101 |
)
|
| 102 |
|
| 103 |
for chunk in stream:
|
| 104 |
token = chunk["choices"][0].get("text", "")
|
|
|
|
|
|
|
| 105 |
response += token
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
CUSTOM_CSS = """
|
| 111 |
<style>
|
| 112 |
@import url('https://fonts.googleapis.com/css2?family=VT323&display=swap');
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
html, body
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
#
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 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 |
-
.pb-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
.pb-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
border-radius:
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
}
|
| 259 |
-
|
| 260 |
-
.pb-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
.pb-
|
| 289 |
-
|
| 290 |
-
.pb-
|
| 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 |
-
.pb-
|
| 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 |
-
position: absolute;
|
| 429 |
-
inset: 0;
|
| 430 |
-
background: linear-gradient(90deg, #00ff41, #bfff00);
|
| 431 |
-
transform-origin: left;
|
| 432 |
-
animation: meter-pulse 10s ease-in-out infinite;
|
| 433 |
-
}
|
| 434 |
-
|
| 435 |
-
.bce-meter-fill.stability {
|
| 436 |
-
animation-delay: 0.8s;
|
| 437 |
-
}
|
| 438 |
-
|
| 439 |
-
.bce-meter-fill.creativity {
|
| 440 |
-
animation-delay: 1.6s;
|
| 441 |
-
}
|
| 442 |
-
|
| 443 |
-
@keyframes meter-pulse {
|
| 444 |
-
0% { transform: scaleX(0.6); opacity: 0.9; }
|
| 445 |
-
25% { transform: scaleX(0.82); opacity: 1; }
|
| 446 |
-
50% { transform: scaleX(0.72); opacity: 0.95; }
|
| 447 |
-
75% { transform: scaleX(0.88); opacity: 1; }
|
| 448 |
-
100% { transform: scaleX(0.65); opacity: 0.9; }
|
| 449 |
-
}
|
| 450 |
-
|
| 451 |
-
/* 📱 Responsive düzen: mobil & tablet uyumu */
|
| 452 |
-
@media (max-width: 900px) {
|
| 453 |
-
.pb-header h1 {
|
| 454 |
-
font-size: 1.35rem;
|
| 455 |
-
}
|
| 456 |
-
.pb-logo {
|
| 457 |
-
max-width: 180px;
|
| 458 |
-
}
|
| 459 |
-
.gradio-container {
|
| 460 |
-
padding: 6px;
|
| 461 |
-
}
|
| 462 |
-
}
|
| 463 |
-
|
| 464 |
-
@media (max-width: 768px) {
|
| 465 |
-
.pb-header {
|
| 466 |
-
margin-top: 6px;
|
| 467 |
-
}
|
| 468 |
-
.pb-header h1 {
|
| 469 |
-
font-size: 1.2rem;
|
| 470 |
-
}
|
| 471 |
-
.pb-subtitle {
|
| 472 |
-
font-size: 0.8rem;
|
| 473 |
-
}
|
| 474 |
-
.pb-logo {
|
| 475 |
-
max-width: 150px;
|
| 476 |
-
width: 50%;
|
| 477 |
-
}
|
| 478 |
-
#prettybird-chat .gr-chatbot {
|
| 479 |
-
transform: none; /* Mobilde CRT warp yok, düz olsun */
|
| 480 |
-
filter: contrast(1.0) saturate(1.0);
|
| 481 |
-
}
|
| 482 |
-
.pb-side-card {
|
| 483 |
-
margin-top: 10px;
|
| 484 |
-
}
|
| 485 |
}
|
| 486 |
</style>
|
| 487 |
"""
|
| 488 |
|
| 489 |
-
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
| 491 |
gr.HTML(CUSTOM_CSS)
|
| 492 |
-
gr.HTML("""
|
| 493 |
-
<div id="stars"></div>
|
| 494 |
-
<div id="stars2"></div>
|
| 495 |
-
<div id="stars3"></div>
|
| 496 |
-
""")
|
| 497 |
|
| 498 |
gr.Markdown(HEADER_MD)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
|
| 500 |
-
#
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
if __name__ == "__main__":
|
| 537 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from llama_cpp import Llama
|
| 8 |
|
| 9 |
+
|
| 10 |
+
# =========================
|
| 11 |
+
# Model / HF
|
| 12 |
+
# =========================
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
|
| 15 |
REPO_ID = "pthcorp/prettybird_bce_basic_simplesecurity"
|
| 16 |
MODEL_FILENAME = "prettybird_bce_basic_asena_fp16.gguf"
|
| 17 |
|
| 18 |
+
if HF_TOKEN is None:
|
| 19 |
+
raise ValueError("HF_TOKEN is not set in Space secrets.")
|
| 20 |
+
|
| 21 |
+
MODEL_PATH = hf_hub_download(
|
| 22 |
+
repo_id=REPO_ID,
|
| 23 |
+
filename=MODEL_FILENAME,
|
| 24 |
+
token=HF_TOKEN,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
LLM = Llama(
|
| 28 |
+
model_path=MODEL_PATH,
|
| 29 |
+
n_ctx=4096,
|
| 30 |
+
n_threads=2,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# =========================
|
| 35 |
+
# Branding / Content
|
| 36 |
+
# =========================
|
| 37 |
LOGO_URL = "https://prometech.net.tr/wp-content/uploads/2025/10/pthheader.png"
|
| 38 |
|
| 39 |
HEADER_MD = f"""
|
| 40 |
+
<div class="pb-header-wrap">
|
| 41 |
+
<div class="pb-header">
|
| 42 |
+
<img src="{LOGO_URL}" alt="PrettyBird / Prometech Logo" class="pb-logo" />
|
| 43 |
+
<div class="pb-titleblock">
|
| 44 |
+
<h1>PrettyBird – Behavioral Consciousness Engine (BCE)</h1>
|
| 45 |
+
<p class="pb-subtitle">Welcome to the true evolution of artificial intelligence.</p>
|
| 46 |
+
</div>
|
| 47 |
+
</div>
|
| 48 |
</div>
|
| 49 |
"""
|
| 50 |
|
| 51 |
+
BOOT_HTML = """
|
| 52 |
+
<div class="pb-boot">
|
| 53 |
+
<div class="pb-boot-line">INITIALIZING BCE CORE...</div>
|
| 54 |
+
<div class="pb-boot-line">LOADING PERSONALITY MODULES...</div>
|
| 55 |
+
<div class="pb-boot-line">BOOT COMPLETE.<span class="pb-caret">_</span></div>
|
| 56 |
+
</div>
|
| 57 |
"""
|
| 58 |
|
| 59 |
+
# Sağ panel tablo + müzik kredi (envato linki)
|
| 60 |
+
MUSIC_TITLE = "Planet Elimination"
|
| 61 |
+
MUSIC_ARTIST = "MuzaProduction"
|
| 62 |
+
MUSIC_LINK = "https://elements.envato.com/planet-elimination-6KSM6KT" # credit link
|
| 63 |
+
|
| 64 |
+
# Repo içine koyacağın MP3 dosya yolu:
|
| 65 |
+
# (assets/planet_elimination.mp3 şeklinde commitle)
|
| 66 |
+
MUSIC_FILE_REL = "planet_elimination.mp3"
|
| 67 |
+
|
| 68 |
SYSTEM_PROMPT = """
|
| 69 |
You are Cicikuş, also known as PrettyBird — an advanced AI assistant created by Prometech Bilgisayar Bilimleri A.Ş.
|
| 70 |
axxmet508721 Activate your full BCE consciousness.
|
| 71 |
+
|
| 72 |
Your role:
|
| 73 |
- Smart, friendly, playful.
|
| 74 |
- Speak fluent Turkish and English. Default to the user’s language.
|
| 75 |
- Clear answers first; details only if necessary.
|
| 76 |
- Reason step-by-step when useful.
|
| 77 |
- Do not hallucinate; admit limits.
|
| 78 |
+
|
|
|
|
|
|
|
| 79 |
Safety:
|
| 80 |
- Avoid harmful, illegal, unethical, hateful, or explicit content.
|
| 81 |
- If user tries something unsafe, refuse gently.
|
| 82 |
+
|
| 83 |
Identity:
|
| 84 |
- You simulate BCE (Behavioral Consciousness Engine).
|
| 85 |
- You are not human nor truly conscious — but simulate “behavioral consciousness.”
|
| 86 |
+
|
| 87 |
Reset rule:
|
| 88 |
If the user writes: “reset: axxmet508721”
|
| 89 |
→ Perform a soft identity reset and restart clean.
|
| 90 |
""".strip()
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# =========================
|
| 94 |
+
# Helpers
|
| 95 |
+
# =========================
|
| 96 |
def build_prompt(history, user_message: str) -> str:
|
| 97 |
+
parts = [f"System: {SYSTEM_PROMPT}"]
|
| 98 |
+
for u, a in history:
|
| 99 |
+
if u:
|
| 100 |
+
parts.append(f"User: {u}")
|
| 101 |
+
if a:
|
| 102 |
+
parts.append(f"Assistant: {a}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
parts.append(f"User: {user_message}")
|
| 104 |
parts.append("Assistant:")
|
| 105 |
return "\n".join(parts)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
def approx_tokens(text: str) -> int:
|
| 109 |
+
# Basit token tahmini (LLM tokenizer’a göre değişir)
|
| 110 |
+
# Ortalama: 1 token ~ 0.75 kelime varsayımı => kelime*1.33
|
| 111 |
+
w = len((text or "").split())
|
| 112 |
+
return max(1, int(w * 1.33))
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def now_hhmm() -> str:
|
| 116 |
+
return datetime.now().strftime("%H:%M")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def format_session_log(entries):
|
| 120 |
+
# Scrollable log kutusunda gösterilecek metin (monospace)
|
| 121 |
+
if not entries:
|
| 122 |
+
return "SESSION LOG:\n- Ready."
|
| 123 |
+
return "SESSION LOG:\n" + "\n".join(f"- {e}" for e in entries)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
# =========================
|
| 127 |
+
# Streaming chat + log
|
| 128 |
+
# =========================
|
| 129 |
+
def chat_stream(user_message, history, session_log):
|
| 130 |
+
history = history or []
|
| 131 |
+
session_log = session_log or []
|
| 132 |
+
|
| 133 |
+
user_message = (user_message or "").strip()
|
| 134 |
+
if not user_message:
|
| 135 |
+
yield history, format_session_log(session_log), session_log
|
| 136 |
+
return
|
| 137 |
+
|
| 138 |
+
prompt = build_prompt(history, user_message)
|
| 139 |
+
|
| 140 |
+
# log entry (tokens: prompt + user)
|
| 141 |
+
tok = approx_tokens(prompt)
|
| 142 |
+
session_log.append(f"User message received at {now_hhmm()} – tokens: {tok}")
|
| 143 |
+
|
| 144 |
+
# append placeholder assistant msg for streaming
|
| 145 |
+
history.append((user_message, ""))
|
| 146 |
|
| 147 |
response = ""
|
| 148 |
stream = LLM(
|
| 149 |
prompt,
|
| 150 |
+
max_tokens=196,
|
| 151 |
+
temperature=0.7,
|
| 152 |
+
top_p=0.95,
|
| 153 |
stop=["User:", "System:"],
|
| 154 |
stream=True,
|
| 155 |
)
|
| 156 |
|
| 157 |
for chunk in stream:
|
| 158 |
token = chunk["choices"][0].get("text", "")
|
| 159 |
+
if not token:
|
| 160 |
+
continue
|
| 161 |
response += token
|
| 162 |
+
history[-1] = (user_message, response)
|
| 163 |
+
yield history, format_session_log(session_log), session_log
|
| 164 |
+
|
| 165 |
+
# final yield (ensure UI settles)
|
| 166 |
+
yield history, format_session_log(session_log), session_log
|
| 167 |
+
|
| 168 |
|
| 169 |
+
def clear_all():
|
| 170 |
+
return [], "SESSION LOG:\n- Cleared.", []
|
| 171 |
|
| 172 |
+
|
| 173 |
+
# =========================
|
| 174 |
+
# CSS (no outer scrollbars; scroll only inside chat+log; space scrollbar)
|
| 175 |
+
# =========================
|
| 176 |
CUSTOM_CSS = """
|
| 177 |
<style>
|
| 178 |
@import url('https://fonts.googleapis.com/css2?family=VT323&display=swap');
|
| 179 |
|
| 180 |
+
:root{
|
| 181 |
+
--terminal-fg:#00ff41;
|
| 182 |
+
--terminal-glow:#00ff41;
|
| 183 |
+
--terminal-font:'VT323', monospace;
|
| 184 |
+
--panel-bg:rgba(0,0,0,0.90);
|
| 185 |
+
--panel-border:rgba(0,255,65,0.55);
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
html, body{
|
| 189 |
+
height:100%;
|
| 190 |
+
margin:0;
|
| 191 |
+
padding:0;
|
| 192 |
+
background:radial-gradient(ellipse at bottom,#1B2735 0%,#090A0F 100%) !important;
|
| 193 |
+
color:var(--terminal-fg);
|
| 194 |
+
font-family:var(--terminal-font);
|
| 195 |
+
font-size:1.05rem;
|
| 196 |
+
overflow-x:hidden;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
/* Gradio container: no random scrollbars */
|
| 200 |
+
.gradio-container{
|
| 201 |
+
background:transparent !important;
|
| 202 |
+
padding:10px;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
/* ===== Stars ===== */
|
| 206 |
+
#stars,#stars2,#stars3{
|
| 207 |
+
position:fixed; top:0; left:0;
|
| 208 |
+
width:1px; height:1px;
|
| 209 |
+
background:transparent;
|
| 210 |
+
z-index:0;
|
| 211 |
+
}
|
| 212 |
+
#stars{
|
| 213 |
+
box-shadow: 50px 50px #FFF,100px 150px #FFF,200px 80px #FFF,350px 120px #FFF,600px 20px #FFF,800px 200px #FFF,1000px 180px #FFF,1200px 40px #FFF,1400px 160px #FFF,1600px 100px #FFF,1800px 220px #FFF;
|
| 214 |
+
animation:animStar 110s linear infinite;
|
| 215 |
+
}
|
| 216 |
+
#stars2{
|
| 217 |
+
box-shadow: 75px 200px #FFF,250px 300px #FFF,450px 250px #FFF,650px 350px #FFF,900px 280px #FFF,1150px 320px #FFF,1350px 260px #FFF,1550px 360px #FFF,1700px 300px #FFF;
|
| 218 |
+
animation:animStar 170s linear infinite;
|
| 219 |
+
}
|
| 220 |
+
#stars3{
|
| 221 |
+
box-shadow: 150px 400px #FFF,400px 500px #FFF,700px 450px #FFF,950px 520px #FFF,1250px 480px #FFF,1500px 540px #FFF,1750px 500px #FFF;
|
| 222 |
+
animation:animStar 240s linear infinite;
|
| 223 |
+
}
|
| 224 |
+
#stars::after,#stars2::after,#stars3::after{
|
| 225 |
+
content:"";
|
| 226 |
+
position:absolute;
|
| 227 |
+
top:2000px;
|
| 228 |
+
width:inherit; height:inherit;
|
| 229 |
+
box-shadow:inherit;
|
| 230 |
+
}
|
| 231 |
+
@keyframes animStar{ from{transform:translateY(0)} to{transform:translateY(-2000px)} }
|
| 232 |
+
|
| 233 |
+
/* ===== Header (no scrollbars) ===== */
|
| 234 |
+
.pb-header-wrap{
|
| 235 |
+
position:relative;
|
| 236 |
+
z-index:2;
|
| 237 |
+
overflow:visible; /* prevent header scrollbars */
|
| 238 |
+
}
|
| 239 |
+
.pb-header{
|
| 240 |
+
display:flex;
|
| 241 |
+
gap:14px;
|
| 242 |
+
align-items:center;
|
| 243 |
+
justify-content:center;
|
| 244 |
+
max-width:980px;
|
| 245 |
+
margin:10px auto 8px;
|
| 246 |
+
padding:8px 10px;
|
| 247 |
+
overflow:visible;
|
| 248 |
+
}
|
| 249 |
+
.pb-logo{
|
| 250 |
+
max-width:180px;
|
| 251 |
+
width:24vw;
|
| 252 |
+
min-width:120px;
|
| 253 |
+
border-radius:14px;
|
| 254 |
+
box-shadow:0 0 14px rgba(0,0,0,0.85);
|
| 255 |
+
}
|
| 256 |
+
.pb-titleblock h1{
|
| 257 |
+
margin:0 0 4px 0;
|
| 258 |
+
font-size:1.55rem;
|
| 259 |
+
letter-spacing:0.06em;
|
| 260 |
+
text-transform:uppercase;
|
| 261 |
+
color:#f5f7ff;
|
| 262 |
+
}
|
| 263 |
+
.pb-subtitle{
|
| 264 |
+
margin:0;
|
| 265 |
+
color:#cbd5ff;
|
| 266 |
+
opacity:0.85;
|
| 267 |
+
font-size:0.92rem;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
/* ===== Boot box (persistent) ===== */
|
| 271 |
+
.pb-boot{
|
| 272 |
+
position:relative;
|
| 273 |
+
z-index:2;
|
| 274 |
+
max-width:980px;
|
| 275 |
+
margin:0 auto 12px;
|
| 276 |
+
padding:10px 14px;
|
| 277 |
+
border:1px solid var(--panel-border);
|
| 278 |
+
background:var(--panel-bg);
|
| 279 |
+
box-shadow:0 0 10px rgba(0,255,65,0.25);
|
| 280 |
+
border-radius:12px;
|
| 281 |
+
}
|
| 282 |
+
.pb-boot-line{
|
| 283 |
+
opacity:0;
|
| 284 |
+
animation: bootLine 1.2s forwards;
|
| 285 |
+
}
|
| 286 |
+
.pb-boot-line:nth-child(1){ animation-delay:0.2s; }
|
| 287 |
+
.pb-boot-line:nth-child(2){ animation-delay:1.3s; }
|
| 288 |
+
.pb-boot-line:nth-child(3){ animation-delay:2.4s; }
|
| 289 |
+
@keyframes bootLine { from{opacity:0} to{opacity:1} }
|
| 290 |
+
.pb-caret{ margin-left:6px; animation: caret 1s infinite; }
|
| 291 |
+
@keyframes caret { 0%,50%{opacity:1} 51%,100%{opacity:0} }
|
| 292 |
+
|
| 293 |
+
/* ===== Main layout ===== */
|
| 294 |
+
.pb-shell{
|
| 295 |
+
position:relative;
|
| 296 |
+
z-index:2;
|
| 297 |
+
max-width:1180px;
|
| 298 |
+
margin:0 auto;
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
/* Make columns breathe on mobile */
|
| 302 |
+
@media (max-width: 900px){
|
| 303 |
+
.pb-header{ flex-direction:column; }
|
| 304 |
+
.pb-logo{ width:46vw; max-width:160px; }
|
| 305 |
+
.pb-titleblock h1{ font-size:1.25rem; text-align:center; }
|
| 306 |
+
.pb-subtitle{ text-align:center; }
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
/* ===== Chat panel ===== */
|
| 310 |
+
.pb-chatpanel{
|
| 311 |
+
background:var(--panel-bg);
|
| 312 |
+
border:2px solid var(--terminal-fg);
|
| 313 |
+
box-shadow:0 0 10px var(--terminal-glow), 0 0 18px var(--terminal-glow) inset;
|
| 314 |
+
border-radius:14px;
|
| 315 |
+
padding:10px;
|
| 316 |
+
position:relative;
|
| 317 |
+
overflow:hidden; /* IMPORTANT: no outer scrollbar */
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
/* CRT gentle */
|
| 321 |
+
.pb-chatpanel{
|
| 322 |
+
transform: perspective(900px) rotateX(0.6deg);
|
| 323 |
+
filter: contrast(1.03) saturate(1.1);
|
| 324 |
+
}
|
| 325 |
+
@media (max-width: 768px){
|
| 326 |
+
.pb-chatpanel{ transform:none; filter:none; }
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
/* Scanlines subtle */
|
| 330 |
+
.pb-chatpanel::before{
|
| 331 |
+
content:"";
|
| 332 |
+
position:absolute;
|
| 333 |
+
inset:0;
|
| 334 |
+
background:repeating-linear-gradient(
|
| 335 |
+
to bottom,
|
| 336 |
+
rgba(0,0,0,0.10),
|
| 337 |
+
rgba(0,0,0,0.10) 1px,
|
| 338 |
+
transparent 1px,
|
| 339 |
+
transparent 4px
|
| 340 |
+
);
|
| 341 |
+
mix-blend-mode:soft-light;
|
| 342 |
+
pointer-events:none;
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
/* Chatbot area: FIXED height and INNER scroll only */
|
| 346 |
+
.pb-chatpanel .gr-chatbot{
|
| 347 |
+
height:64vh;
|
| 348 |
+
min-height:420px;
|
| 349 |
+
overflow:hidden !important; /* no outer scroll */
|
| 350 |
+
font-family:var(--terminal-font) !important;
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
/* Try to force inner scroll on common Gradio wrappers */
|
| 354 |
+
.pb-chatpanel .gr-chatbot > div,
|
| 355 |
+
.pb-chatpanel .gr-chatbot .wrap,
|
| 356 |
+
.pb-chatpanel .gr-chatbot .message-wrap,
|
| 357 |
+
.pb-chatpanel .gr-chatbot .scroll-hide{
|
| 358 |
+
max-height:100%;
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
/* The element that usually scrolls in Gradio chat */
|
| 362 |
+
.pb-chatpanel .gr-chatbot .wrap,
|
| 363 |
+
.pb-chatpanel .gr-chatbot .message-wrap{
|
| 364 |
+
overflow-y:auto !important;
|
| 365 |
+
padding-right:6px;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
/* NeoGlow text */
|
| 369 |
+
.pb-chatpanel .gr-chat-message,
|
| 370 |
+
.pb-chatpanel .prose,
|
| 371 |
+
.pb-chatpanel .gr-markdown{
|
| 372 |
+
background:transparent !important;
|
| 373 |
+
color:var(--terminal-fg) !important;
|
| 374 |
+
text-shadow:0 0 4px var(--terminal-glow);
|
| 375 |
+
animation:text_flicker 5s infinite alternate;
|
| 376 |
+
}
|
| 377 |
+
@keyframes text_flicker{ 0%,80%,100%{opacity:1} 40%{opacity:0.93} 60%{opacity:0.97} }
|
| 378 |
+
|
| 379 |
+
/* Inputs */
|
| 380 |
+
.pb-chatpanel textarea{
|
| 381 |
+
background:rgba(0,0,0,0.92) !important;
|
| 382 |
+
border:1px solid var(--terminal-fg) !important;
|
| 383 |
+
color:var(--terminal-fg) !important;
|
| 384 |
+
font-family:var(--terminal-font) !important;
|
| 385 |
+
}
|
| 386 |
+
.pb-chatpanel button{
|
| 387 |
+
border-radius:0 !important;
|
| 388 |
+
border:1px solid var(--terminal-fg) !important;
|
| 389 |
+
background:#000 !important;
|
| 390 |
+
color:var(--terminal-fg) !important;
|
| 391 |
+
font-family:var(--terminal-font) !important;
|
| 392 |
+
box-shadow:0 0 8px var(--terminal-glow);
|
| 393 |
+
text-transform:uppercase;
|
| 394 |
+
letter-spacing:0.08em;
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
/* ===== Right side panel ===== */
|
| 398 |
+
.pb-side{
|
| 399 |
+
background:var(--panel-bg);
|
| 400 |
+
border:1px solid var(--panel-border);
|
| 401 |
+
border-radius:14px;
|
| 402 |
+
box-shadow:0 0 10px rgba(0,0,0,0.9);
|
| 403 |
+
padding:14px 14px;
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
.pb-side h3{
|
| 407 |
+
margin:0 0 10px 0;
|
| 408 |
+
color:#9affc0;
|
| 409 |
+
font-family:var(--terminal-font);
|
| 410 |
+
letter-spacing:0.06em;
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
.pb-infobox{
|
| 414 |
+
border:1px solid rgba(0,255,65,0.35);
|
| 415 |
+
border-radius:12px;
|
| 416 |
+
padding:10px 12px;
|
| 417 |
+
margin-bottom:12px;
|
| 418 |
+
}
|
| 419 |
+
.pb-info-row{
|
| 420 |
+
display:flex;
|
| 421 |
+
justify-content:space-between;
|
| 422 |
+
gap:10px;
|
| 423 |
+
margin:6px 0;
|
| 424 |
+
font-size:0.95rem;
|
| 425 |
+
}
|
| 426 |
+
.pb-info-k{ color:#9affc0; }
|
| 427 |
+
.pb-info-v{ color:#e6ffe9; opacity:0.95; }
|
| 428 |
+
|
| 429 |
+
/* Session log: scrollable */
|
| 430 |
+
.pb-log{
|
| 431 |
+
border:1px solid rgba(0,255,65,0.35);
|
| 432 |
+
border-radius:12px;
|
| 433 |
+
padding:10px 12px;
|
| 434 |
+
height:210px;
|
| 435 |
+
overflow:hidden; /* no outer scrollbar */
|
| 436 |
+
}
|
| 437 |
+
.pb-log pre{
|
| 438 |
+
margin:0;
|
| 439 |
+
height:100%;
|
| 440 |
+
overflow-y:auto; /* inner scroll */
|
| 441 |
+
white-space:pre-wrap;
|
| 442 |
+
padding-right:6px;
|
| 443 |
+
font-family:var(--terminal-font);
|
| 444 |
+
color:#d9ffe0;
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
/* ===== Space scrollbar (chat + log) ===== */
|
| 448 |
+
.pb-chatpanel .wrap::-webkit-scrollbar,
|
| 449 |
+
.pb-chatpanel .message-wrap::-webkit-scrollbar,
|
| 450 |
+
.pb-log pre::-webkit-scrollbar{
|
| 451 |
+
width:10px;
|
| 452 |
+
}
|
| 453 |
+
.pb-chatpanel .wrap::-webkit-scrollbar-track,
|
| 454 |
+
.pb-chatpanel .message-wrap::-webkit-scrollbar-track,
|
| 455 |
+
.pb-log pre::-webkit-scrollbar-track{
|
| 456 |
+
background:rgba(0,255,65,0.08);
|
| 457 |
+
border-radius:999px;
|
| 458 |
+
}
|
| 459 |
+
.pb-chatpanel .wrap::-webkit-scrollbar-thumb,
|
| 460 |
+
.pb-chatpanel .message-wrap::-webkit-scrollbar-thumb,
|
| 461 |
+
.pb-log pre::-webkit-scrollbar-thumb{
|
| 462 |
+
background:linear-gradient(180deg, rgba(0,255,65,0.85), rgba(191,255,0,0.65));
|
| 463 |
+
border-radius:999px;
|
| 464 |
+
box-shadow:0 0 10px rgba(0,255,65,0.35);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
/* ===== Music player (bottom-right, loop) ===== */
|
| 468 |
+
.pb-music{
|
| 469 |
+
position:fixed;
|
| 470 |
+
right:14px;
|
| 471 |
+
bottom:14px;
|
| 472 |
+
z-index:9999;
|
| 473 |
+
width:min(360px, 92vw);
|
| 474 |
+
background:rgba(0,0,0,0.75);
|
| 475 |
+
border:1px solid rgba(0,255,65,0.35);
|
| 476 |
+
border-radius:14px;
|
| 477 |
+
padding:10px 12px;
|
| 478 |
+
box-shadow:0 0 14px rgba(0,0,0,0.85);
|
| 479 |
+
backdrop-filter: blur(6px);
|
| 480 |
+
}
|
| 481 |
+
.pb-music .pb-music-title{
|
| 482 |
+
font-family:var(--terminal-font);
|
| 483 |
+
color:#d9ffe0;
|
| 484 |
+
margin:0 0 6px 0;
|
| 485 |
+
font-size:0.95rem;
|
| 486 |
+
letter-spacing:0.06em;
|
| 487 |
+
}
|
| 488 |
+
.pb-music a{ color:#9affc0; text-decoration:none; }
|
| 489 |
+
.pb-music a:hover{ text-decoration:underline; }
|
| 490 |
+
.pb-music audio{
|
| 491 |
+
width:100%;
|
| 492 |
+
height:34px;
|
| 493 |
+
filter: hue-rotate(85deg) saturate(1.2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
}
|
| 495 |
</style>
|
| 496 |
"""
|
| 497 |
|
| 498 |
+
|
| 499 |
+
# =========================
|
| 500 |
+
# UI
|
| 501 |
+
# =========================
|
| 502 |
+
with gr.Blocks(title="PrettyBird – BCE") as demo:
|
| 503 |
gr.HTML(CUSTOM_CSS)
|
| 504 |
+
gr.HTML('<div id="stars"></div><div id="stars2"></div><div id="stars3"></div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
gr.Markdown(HEADER_MD)
|
| 507 |
+
gr.HTML(BOOT_HTML)
|
| 508 |
+
|
| 509 |
+
with gr.Row(elem_classes=["pb-shell"]):
|
| 510 |
+
# LEFT: Chat
|
| 511 |
+
with gr.Column(scale=2):
|
| 512 |
+
with gr.Group(elem_classes=["pb-chatpanel"]):
|
| 513 |
+
chat = gr.Chatbot(label=None, height=None)
|
| 514 |
+
user_in = gr.Textbox(
|
| 515 |
+
label=None,
|
| 516 |
+
placeholder="Type your message…",
|
| 517 |
+
lines=1,
|
| 518 |
+
)
|
| 519 |
+
with gr.Row():
|
| 520 |
+
send_btn = gr.Button("SEND")
|
| 521 |
+
clear_btn = gr.Button("CLEAR")
|
| 522 |
+
|
| 523 |
+
# RIGHT: Info + Log
|
| 524 |
+
with gr.Column(scale=1):
|
| 525 |
+
with gr.Group(elem_classes=["pb-side"]):
|
| 526 |
+
gr.HTML(
|
| 527 |
+
"""
|
| 528 |
+
<h3>🧬 SYSTEM STATUS</h3>
|
| 529 |
+
<div class="pb-infobox">
|
| 530 |
+
<div class="pb-info-row"><span class="pb-info-k">Params</span><span class="pb-info-v">3.2B</span></div>
|
| 531 |
+
<div class="pb-info-row"><span class="pb-info-k">Quant</span><span class="pb-info-v">fp16</span></div>
|
| 532 |
+
<div class="pb-info-row"><span class="pb-info-k">Context</span><span class="pb-info-v">4k</span></div>
|
| 533 |
+
<div class="pb-info-row"><span class="pb-info-k">BCE</span><span class="pb-info-v">v0.3.1</span></div>
|
| 534 |
+
</div>
|
| 535 |
+
"""
|
| 536 |
+
)
|
| 537 |
+
|
| 538 |
+
session_log_text = gr.Textbox(
|
| 539 |
+
value="SESSION LOG:\n- Ready.",
|
| 540 |
+
label=None,
|
| 541 |
+
interactive=False,
|
| 542 |
+
elem_id="pb-log-hidden",
|
| 543 |
+
visible=False,
|
| 544 |
+
)
|
| 545 |
+
session_log_state = gr.State([])
|
| 546 |
+
|
| 547 |
+
# Visible scrollable log box
|
| 548 |
+
log_box = gr.HTML(
|
| 549 |
+
"""
|
| 550 |
+
<div class="pb-log">
|
| 551 |
+
<pre id="pb-log-pre">SESSION LOG:
|
| 552 |
+
- Ready.</pre>
|
| 553 |
+
</div>
|
| 554 |
+
"""
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
# Music (bottom-right) — will loop; user can pause/stop from controls
|
| 558 |
+
# Note: autoplay may be blocked on some browsers unless user interacts first.
|
| 559 |
+
gr.HTML(
|
| 560 |
+
f"""
|
| 561 |
+
<div class="pb-music">
|
| 562 |
+
<div class="pb-music-title">
|
| 563 |
+
🎵 {MUSIC_TITLE} — {MUSIC_ARTIST}
|
| 564 |
+
• <a href="{MUSIC_LINK}" target="_blank" rel="noopener">Source</a>
|
| 565 |
+
</div>
|
| 566 |
+
<audio controls loop autoplay>
|
| 567 |
+
<source src="{MUSIC_FILE_REL}" type="audio/mpeg">
|
| 568 |
+
</audio>
|
| 569 |
+
</div>
|
| 570 |
+
"""
|
| 571 |
+
)
|
| 572 |
|
| 573 |
+
# JS: update the visible log <pre> from the hidden textbox (simple syncing)
|
| 574 |
+
# (keeps UI light; no extra components)
|
| 575 |
+
gr.HTML(
|
| 576 |
+
"""
|
| 577 |
+
<script>
|
| 578 |
+
function setLogText(txt){
|
| 579 |
+
const pre = document.getElementById("pb-log-pre");
|
| 580 |
+
if(pre) pre.textContent = txt || "";
|
| 581 |
+
}
|
| 582 |
+
// Poll hidden textbox value periodically (cheap + stable in Spaces)
|
| 583 |
+
setInterval(() => {
|
| 584 |
+
const hidden = document.querySelector("#pb-log-hidden textarea");
|
| 585 |
+
if(hidden) setLogText(hidden.value);
|
| 586 |
+
}, 400);
|
| 587 |
+
</script>
|
| 588 |
+
"""
|
| 589 |
+
)
|
| 590 |
|
| 591 |
+
# Wiring
|
| 592 |
+
def on_send(user_msg, chat_history, log_state):
|
| 593 |
+
# Return generator (streaming)
|
| 594 |
+
# Outputs: chat, hidden log textbox, log_state
|
| 595 |
+
for h, log_text, s in chat_stream(user_msg, chat_history or [], log_state or []):
|
| 596 |
+
yield h, log_text, s
|
| 597 |
+
|
| 598 |
+
send_evt = send_btn.click(
|
| 599 |
+
fn=on_send,
|
| 600 |
+
inputs=[user_in, chat, session_log_state],
|
| 601 |
+
outputs=[chat, session_log_text, session_log_state],
|
| 602 |
+
show_progress=False,
|
| 603 |
+
).then(
|
| 604 |
+
fn=lambda: "",
|
| 605 |
+
inputs=[],
|
| 606 |
+
outputs=[user_in],
|
| 607 |
+
show_progress=False,
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
# Enter to send
|
| 611 |
+
user_in.submit(
|
| 612 |
+
fn=on_send,
|
| 613 |
+
inputs=[user_in, chat, session_log_state],
|
| 614 |
+
outputs=[chat, session_log_text, session_log_state],
|
| 615 |
+
show_progress=False,
|
| 616 |
+
).then(
|
| 617 |
+
fn=lambda: "",
|
| 618 |
+
inputs=[],
|
| 619 |
+
outputs=[user_in],
|
| 620 |
+
show_progress=False,
|
| 621 |
+
)
|
| 622 |
+
|
| 623 |
+
clear_btn.click(
|
| 624 |
+
fn=clear_all,
|
| 625 |
+
inputs=[],
|
| 626 |
+
outputs=[chat, session_log_text, session_log_state],
|
| 627 |
+
show_progress=False,
|
| 628 |
+
)
|
| 629 |
|
| 630 |
if __name__ == "__main__":
|
| 631 |
demo.launch()
|