Spaces:
Sleeping
Sleeping
Download app.py from FUTUREVIDYABOX/FVB-AI: direct link, hf CLI and curl.
- Browser
- Download file 13.2 kB
-
https://huggingface.co/spaces/FUTUREVIDYABOX/FVB-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/FUTUREVIDYABOX/FVB-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/FUTUREVIDYABOX/FVB-AI/resolve/main/app.py
13.2 kB
| 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 | |
| # ========================================================= | |
| 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 | |
| ) |