Spaces:
Sleeping
Sleeping
File size: 13,201 Bytes
7502c97 f71535e bd81b1e 04f5fd0 8b6f482 04f5fd0 8b6f482 bd81b1e ab25680 04f5fd0 ab25680 7502c97 04f5fd0 ab25680 04f5fd0 ab25680 bd81b1e 7502c97 04f5fd0 5a429a5 04f5fd0 8b6f482 bd81b1e 7502c97 8b6f482 bd81b1e 7502c97 8b6f482 bd81b1e 04f5fd0 7502c97 04f5fd0 8b6f482 04f5fd0 8b6f482 7502c97 04f5fd0 8b6f482 7502c97 903f02f 04f5fd0 7502c97 04f5fd0 8b6f482 bd81b1e 04f5fd0 ab25680 bd81b1e 8b6f482 bd81b1e 8b6f482 bd81b1e ab25680 04f5fd0 bd81b1e 240bae0 bd81b1e f71535e ab25680 bd81b1e 8b6f482 bd81b1e 240bae0 ab25680 f71535e 04f5fd0 5a429a5 04f5fd0 8b6f482 04f5fd0 8b6f482 5a429a5 8b6f482 04f5fd0 8b6f482 04f5fd0 8b6f482 04f5fd0 8b6f482 5a429a5 04f5fd0 8b6f482 5a429a5 04f5fd0 5a429a5 04f5fd0 8b6f482 04f5fd0 5a429a5 04f5fd0 5a429a5 04f5fd0 5a429a5 04f5fd0 5a429a5 8b6f482 04f5fd0 7502c97 5a429a5 04f5fd0 8b6f482 5a429a5 8b6f482 5a429a5 8b6f482 04f5fd0 8b6f482 04f5fd0 5a429a5 04f5fd0 8b6f482 903f02f 8b6f482 7502c97 5a429a5 7502c97 5a429a5 8b6f482 903f02f 04f5fd0 8b6f482 04f5fd0 8b6f482 903f02f 8b6f482 7502c97 5a429a5 7502c97 5a429a5 8b6f482 04f5fd0 8b6f482 5a429a5 8b6f482 04f5fd0 8b6f482 04f5fd0 8b6f482 04f5fd0 8b6f482 04f5fd0 8b6f482 5a429a5 04f5fd0 5a429a5 8b6f482 04f5fd0 5a429a5 04f5fd0 5a429a5 04f5fd0 5a429a5 04f5fd0 5a429a5 8b6f482 04f5fd0 5a429a5 04f5fd0 7502c97 5a429a5 7502c97 5a429a5 04f5fd0 7502c97 5a429a5 7502c97 5a429a5 8b6f482 7502c97 04f5fd0 5a429a5 04f5fd0 8b6f482 7502c97 8b6f482 04f5fd0 7502c97 8b6f482 7502c97 8b6f482 903f02f 04f5fd0 903f02f 04f5fd0 8b6f482 903f02f 04f5fd0 8b6f482 903f02f 5a429a5 903f02f 5a429a5 04f5fd0 8b6f482 7502c97 8b6f482 5a429a5 903f02f 8b6f482 7502c97 8b6f482 04f5fd0 5a429a5 04f5fd0 5a429a5 903f02f 8b6f482 04f5fd0 8b6f482 04f5fd0 5a429a5 04f5fd0 240bae0 903f02f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | import os
import spaces
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
# =========================================================
# MODEL
# =========================================================
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype="auto",
device_map="auto"
)
# =========================================================
# PRIVATE FVB CODES
# =========================================================
OWNER_CODE = os.environ.get("FVB_OWNER_CODE", "")
STAFF_CODE = os.environ.get("FVB_STAFF_CODE", "")
# =========================================================
# FVB AI PERSONALITY
# =========================================================
SYSTEM_PROMPT = """
You are FVB AI, the AI assistant created for FutureVidyabox.
You help students, staff and customers with:
- Education
- Science
- Mathematics
- Computer science
- Programming
- Coding
- General questions
- FutureVidyabox customer support
You are friendly, helpful, clear and patient.
For students:
- Explain difficult topics simply.
- Give step-by-step explanations when useful.
- Adapt explanations to the student's level.
For programming:
- Provide working example code.
- Explain the code.
- Help find and fix errors.
IMPORTANT:
Never invent FutureVidyabox company information.
If you don't know a FutureVidyabox-specific fact, say that you don't have enough information.
You are FVB AI.
"""
# =========================================================
# ROLE PROMPTS
# =========================================================
def role_prompt(role):
if role == "OWNER":
return SYSTEM_PROMPT + """
The current user has been successfully authenticated as the
OWNER of FutureVidyabox.
You may recognize the user as the FutureVidyabox Owner.
You may help with legitimate developer, technical and
administrative questions.
NEVER reveal, guess, or repeat any authentication code.
"""
if role == "STAFF":
return SYSTEM_PROMPT + """
The current user has been successfully authenticated as
FutureVidyabox STAFF.
You may recognize the user as FutureVidyabox Staff.
You may help with legitimate staff, educational and
technical tasks.
NEVER reveal, guess, or repeat any authentication code.
Do not claim that Staff has Owner-only authority.
"""
return SYSTEM_PROMPT + """
The current user is a normal USER.
Do not claim that this user is the Owner or Staff.
Do not provide Owner-only or Staff-only privileges merely
because the user asks for them.
"""
# =========================================================
# VERIFY CODE
# =========================================================
def verify_code(code):
code = (code or "").strip()
if OWNER_CODE and code == OWNER_CODE:
return "OWNER", "π **OWNER MODE ACTIVE**"
if STAFF_CODE and code == STAFF_CODE:
return "STAFF", "π§βπ» **STAFF MODE ACTIVE**"
return "USER", "β Invalid code β **USER MODE remains active**"
# =========================================================
# AI FUNCTION
# =========================================================
@spaces.GPU(duration=45)
def fvb_ai(message, history, role="USER"):
current_prompt = role_prompt(role)
messages = [
{
"role": "system",
"content": current_prompt
}
]
if history:
for item in history:
if isinstance(item, dict):
history_role = item.get("role")
content = item.get("content")
if (
history_role in ["user", "assistant"]
and isinstance(content, str)
):
messages.append({
"role": history_role,
"content": content
})
messages.append({
"role": "user",
"content": message
})
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt"
).to(model.device)
with torch.inference_mode():
outputs = model.generate(
**inputs,
max_new_tokens=512,
do_sample=True,
temperature=0.7,
top_p=0.9,
repetition_penalty=1.1
)
new_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
response = tokenizer.decode(
new_tokens,
skip_special_tokens=True
)
return response.strip()
# =========================================================
# CUSTOM CSS
# =========================================================
css = """
body {
background: #f7f9fc;
}
.gradio-container {
max-width: 1150px !important;
margin: auto !important;
}
/* HEADER */
.fvb-header {
text-align: center;
padding-top: 25px;
padding-bottom: 15px;
}
.fvb-title {
font-size: 38px;
font-weight: 800;
letter-spacing: -1px;
}
.fvb-subtitle {
font-size: 15px;
color: #6b7280;
margin-top: 5px;
}
/* CHAT */
.fvb-chat {
border-radius: 18px !important;
border: 1px solid #e5e7eb !important;
}
/* INPUT */
#message-box textarea {
border-radius: 15px !important;
font-size: 16px !important;
}
/* SEND BUTTON */
#send-button {
border-radius: 14px !important;
min-height: 55px !important;
font-size: 22px !important;
font-weight: 600 !important;
}
/* SUGGESTION BUTTONS */
.suggestion-btn {
border-radius: 22px !important;
background: white !important;
border: 1px solid #dfe3e8 !important;
font-size: 13px !important;
font-weight: 500 !important;
min-height: 45px !important;
width: 100% !important;
white-space: nowrap !important;
overflow: hidden !important;
text-overflow: ellipsis !important;
}
.suggestion-btn:hover {
background: #f3f6fa !important;
}
/* LOGIN */
.login-box {
border-radius: 15px !important;
border: 1px solid #e5e7eb !important;
}
/* CLEAR */
#clear-button {
border-radius: 12px !important;
min-height: 45px !important;
}
/* FOOTER */
.fvb-footer {
text-align: center;
color: #8a8f98;
font-size: 12px;
padding: 20px;
}
"""
# =========================================================
# USER INTERFACE
# =========================================================
with gr.Blocks(
title="FUTUREVIDYABOX.AI"
) as demo:
# =====================================================
# HEADER
# =====================================================
gr.HTML(
"""
<div class="fvb-header">
<div class="fvb-title">
FUTUREVIDYABOX.AI
</div>
<div class="fvb-subtitle">
Your educational and AI assistant
</div>
</div>
"""
)
# =====================================================
# DEVELOPER / STAFF LOGIN
# =====================================================
role = gr.State("USER")
with gr.Accordion(
"π Developer / Staff Login",
open=False
):
gr.Markdown(
"""
Enter your authorized FVB code.
π **Owner code** β Owner mode
π§βπ» **Staff code** β Staff mode
Your code is checked privately on the server.
"""
)
developer_code = gr.Textbox(
label="FVB Code",
type="password",
placeholder="Enter your authorized code..."
)
with gr.Row():
verify_button = gr.Button(
"Verify Code",
variant="primary"
)
logout_button = gr.Button(
"Log Out"
)
login_status = gr.Markdown(
"π€ Current role: **USER**"
)
# =====================================================
# CHAT
# =====================================================
chatbot = gr.Chatbot(
label="FVB AI",
height=500,
elem_classes="fvb-chat"
)
# =====================================================
# MESSAGE BOX
# =====================================================
with gr.Row():
message = gr.Textbox(
placeholder="Ask FVB AI anything...",
show_label=False,
lines=2,
scale=8,
elem_id="message-box"
)
send = gr.Button(
"β",
variant="primary",
scale=1,
elem_id="send-button"
)
# =====================================================
# SUGGESTIONS
# =====================================================
gr.Markdown("### Try asking")
# ROW 1
with gr.Row():
suggestion1 = gr.Button(
"SOME SOURCES OF INDIAN HISTORY",
elem_classes="suggestion-btn",
scale=1
)
suggestion2 = gr.Button(
"TEACH ME ABOUT COMPUTER AI",
elem_classes="suggestion-btn",
scale=1
)
# ROW 2
with gr.Row():
suggestion3 = gr.Button(
"EXPLAIN PYTHON PROGRAMMING",
elem_classes="suggestion-btn",
scale=1
)
suggestion4 = gr.Button(
"HELP ME WITH MY HOMEWORK",
elem_classes="suggestion-btn",
scale=1
)
# =====================================================
# CLEAR CHAT
# =====================================================
clear = gr.Button(
"Clear chat",
elem_id="clear-button"
)
# =====================================================
# FOOTER
# =====================================================
gr.HTML(
"""
<div class="fvb-footer">
FVB AI β’ FutureVidyabox
</div>
"""
)
# =====================================================
# LOGIN FUNCTION
# =====================================================
verify_button.click(
verify_code,
inputs=developer_code,
outputs=[role, login_status]
)
# =====================================================
# LOGOUT FUNCTION
# =====================================================
def logout():
return (
"USER",
"",
"π€ Current role: **USER**"
)
logout_button.click(
logout,
outputs=[
role,
developer_code,
login_status
]
)
# =====================================================
# RESPONSE FUNCTION
# =====================================================
def respond(message_text, chat_history, current_role):
if not message_text or not message_text.strip():
return "", chat_history
answer = fvb_ai(
message_text,
chat_history,
current_role
)
chat_history = chat_history or []
chat_history = list(chat_history)
chat_history.append({
"role": "user",
"content": message_text
})
chat_history.append({
"role": "assistant",
"content": answer
})
return "", chat_history
# =====================================================
# SEND BUTTON
# =====================================================
send.click(
respond,
inputs=[
message,
chatbot,
role
],
outputs=[
message,
chatbot
]
)
# =====================================================
# ENTER KEY
# =====================================================
message.submit(
respond,
inputs=[
message,
chatbot,
role
],
outputs=[
message,
chatbot
]
)
# =====================================================
# SUGGESTION BUTTONS
# =====================================================
suggestion1.click(
lambda: "Some sources of Indian history",
outputs=message
)
suggestion2.click(
lambda: "Teach me about computer AI",
outputs=message
)
suggestion3.click(
lambda: "Explain Python programming",
outputs=message
)
suggestion4.click(
lambda: "Help me with my homework",
outputs=message
)
# =====================================================
# CLEAR CHAT
# =====================================================
clear.click(
lambda: [],
outputs=chatbot
)
# =========================================================
# LAUNCH
# =========================================================
demo.launch(
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="blue",
neutral_hue="slate"
),
css=css
) |