Spaces:
Sleeping
Sleeping
Jn-Huang commited on
Commit ·
1a77428
1
Parent(s): f6fde6f
Fix bugs: use token param, apply Llama 3.1 chat template, decode only new tokens
Browse files36 unmodified lines37383940414243446 unmodified lines5152535455565758592 unmodified lines62636465666768697071727374757677787980818283848586878889909136 unmodified linesif USE_PEFT:try:_ = PeftConfig.from_pretrained(PEFT_MODEL_ID, use_auth_token=HF_TOKEN)model = PeftModel.from_pretrained(base, PEFT_MODEL_ID, use_auth_token=HF_TOKEN)print(f"[INFO] Loaded PEFT adapter: {PEFT_MODEL_ID}")return model, tokexcept Exception as e:6 unmodified lines@spaces.GPU@torch.inference_mode()def generate_response(prompt: str, max_new_tokens=512, temperature=0.7, top_p=0.9) -> str:enc = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)enc = {k: v.to(DEVICE) for k, v in enc.items()}out = model.generate(**enc,max_new_tokens=max_new_tokens,2 unmodified linestop_p=top_p,pad_token_id=tokenizer.eos_token_id,)return tokenizer.decode(out[0], skip_special_tokens=True)def chat_fn(message, history, system_prompt, max_new_tokens, temperature, top_p):# Build a simple conversation stringconv = []if system_prompt:conv.append(f"system: {system_prompt}")for u, a in (history or []):if u:conv.append(f"user: {u}")if a:conv.append(f"assistant: {a}")if message:conv.append(f"user: {message}")prompt = "\n".join(conv) + "\nassistant:"reply = generate_response(prompt,max_new_tokens=max_new_tokens,temperature=temperature,top_p=top_p,)# Strip trailingif "assistant:" in reply:reply = reply.split("assistant:")[-1].strip()return replydemo = gr.ChatInterface(36 unmodified lines37383940414243446 unmodified lines51525354555657585960616263646566672 unmodified lines70717273747576777879808182838485868788899091929394959697989936 unmodified linesif USE_PEFT:try:_ = PeftConfig.from_pretrained(PEFT_MODEL_ID, token=HF_TOKEN)model = PeftModel.from_pretrained(base, PEFT_MODEL_ID, token=HF_TOKEN)print(f"[INFO] Loaded PEFT adapter: {PEFT_MODEL_ID}")return model, tokexcept Exception as e:6 unmodified lines@spaces.GPU@torch.inference_mode()def generate_response(messages, max_new_tokens=512, temperature=0.7, top_p=0.9) -> str:# Apply Llama 3.1 chat templateprompt = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True)enc = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)enc = {k: v.to(DEVICE) for k, v in enc.items()}input_length = enc['input_ids'].shape[1]out = model.generate(**enc,max_new_tokens=max_new_tokens,2 unmodified linestop_p=top_p,pad_token_id=tokenizer.eos_token_id,)# Decode only the newly generated tokensreturn tokenizer.decode(out[0][input_length:], skip_special_tokens=True)def chat_fn(message, history, system_prompt, max_new_tokens, temperature, top_p):# Build conversation in Llama 3.1 chat formatmessages = []if system_prompt:messages.append({"role": "system", "content": system_prompt})for user_msg, assistant_msg in (history or []):if user_msg:messages.append({"role": "user", "content": user_msg})if assistant_msg:messages.append({"role": "assistant", "content": assistant_msg})if message:messages.append({"role": "user", "content": message})reply = generate_response(messages,max_new_tokens=max_new_tokens,temperature=temperature,top_p=top_p,)return replydemo = gr.ChatInterface(
app.py+26-18