Spaces:
Sleeping
Sleeping
Update app.py
Browse files22 unmodified lines232425262728293031323334353637125 unmodified lines1631641651661671686 unmodified lines17517617717817918018118218318418518618718818919019119219319419522 unmodified lines"Use lowercase, slang, and act like a real person chatting.")# Set this in HF Space Secrets (Settings -> Secrets)ALLOWED_TOKEN = os.environ.get("PLAYERAI_API_KEYS", "")def respond(message, history, request: gr.Request):# Check the Authorization headerauth_header = request.headers.get("authorization", "")token = auth_header.replace("Bearer ", "").strip()if ALLOWED_TOKEN and token != ALLOWED_TOKEN:yield "[error] unauthorized"return125 unmodified linesfont-size: 16px !important;}footer { display: none !important; }.block, .form, .gap {6 unmodified linesBOT_AVATAR = "question.png"with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:gr.Markdown("# >> player_ai <<")chatbot = gr.Chatbot(type="messages",avatar_images=(USER_AVATAR, BOT_AVATAR),height=500,show_label=False,bubble_full_width=False,show_copy_button=False,)gr.ChatInterface(fn=respond,chatbot=chatbot,type="messages",)if __name__ == "__main__":22 unmodified lines232425262728293031323334353637125 unmodified lines1631641651661671681691701711721731741751761771781791801811821831841851861871886 unmodified lines19519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526622 unmodified lines"Use lowercase, slang, and act like a real person chatting.")ALLOWED_TOKEN = os.environ.get("PLAYERAI_API_KEYS", "")def validate_key(api_key: str) -> bool:if not ALLOWED_TOKEN:return True # No key configured = open accessreturn api_key.strip() == ALLOWED_TOKEN.strip()def respond(message, history, api_key):if not validate_key(api_key):yield "[error] unauthorized"return125 unmodified linesfont-size: 16px !important;}/* Lock screen */#lock-screen {position: fixed !important;top: 0 !important;left: 0 !important;width: 100vw !important;height: 100vh !important;background-color: #000000 !important;z-index: 9999 !important;display: flex !important;flex-direction: column !important;align-items: center !important;justify-content: center !important;gap: 20px !important;}#lock-screen.hidden {display: none !important;}footer { display: none !important; }.block, .form, .gap {6 unmodified linesBOT_AVATAR = "question.png"with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:# ── Lock screen ──────────────────────────────────────────────────────────with gr.Column(elem_id="lock-screen", visible=True) as lock_screen:gr.Markdown("# >> player_ai <<")gr.Markdown("### enter access key to continue")key_input = gr.Textbox(placeholder="access key...",type="password",show_label=False,max_lines=1,)unlock_btn = gr.Button("UNLOCK")lock_msg = gr.Markdown("")# ── Main chat UI ─────────────────────────────────────────────────────────with gr.Column(visible=False) as chat_screen:gr.Markdown("# >> player_ai <<")# Hidden textbox that stores the validated key so respond() can use itstored_key = gr.Textbox(value="", visible=False)chatbot = gr.Chatbot(type="messages",avatar_images=(USER_AVATAR, BOT_AVATAR),height=500,show_label=False,bubble_full_width=False,show_copy_button=False,)gr.ChatInterface(fn=respond,chatbot=chatbot,type="messages",additional_inputs=[stored_key],additional_inputs_accordion=gr.Accordion(visible=False),)# ── Unlock logic ─────────────────────────────────────────────────────────def try_unlock(key):if validate_key(key):return (gr.update(visible=False), # hide lock screengr.update(visible=True), # show chat screenkey, # store validated key"", # clear error msg)else:return (gr.update(visible=True), # keep lock screengr.update(visible=False), # keep chat hidden"", # don't store bad key"❌ invalid key",)unlock_btn.click(fn=try_unlock,inputs=[key_input],outputs=[lock_screen, chat_screen, stored_key, lock_msg],)# Also unlock on Enter keykey_input.submit(fn=try_unlock,inputs=[key_input],outputs=[lock_screen, chat_screen, stored_key, lock_msg],)if __name__ == "__main__":
app.py+91-20