Spaces:
Sleeping
Sleeping
Update app.py
Browse files22 unmodified lines23242526272829303132333435363738394041424322 unmodified lines"Use lowercase, slang, and act like a real person chatting.")def respond(message, history):messages = [{"role": "system", "content": SYSTEM_PROMPT}]for h in history:messages.append(h)messages.append({"role": "user", "content": message})try:# Get the formatted prompt as a string firstprompt = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,)# Then tokenize separately to get a clean tensorinputs = tokenizer(prompt, return_tensors="pt").to(model.device)streamer = TextIteratorStreamer(22 unmodified lines23242526272829303132333435363738394041424344454647484950515222 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("ACCESS_TOKEN", "")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"returnmessages = [{"role": "system", "content": SYSTEM_PROMPT}]for h in history:messages.append(h)messages.append({"role": "user", "content": message})try:prompt = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,)inputs = tokenizer(prompt, return_tensors="pt").to(model.device)streamer = TextIteratorStreamer(
app.py+13-4