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( """