stopping editet
#29
by ismailhakki37 - opened
346 unmodified lines34734834935035135235335435535635735835936036136236336435 unmodified lines400401402403404405235 unmodified lines641642643644346 unmodified linesprompt, our_chatbot.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(our_chatbot.model.device)# Set up stopping criteriastop_str = (our_chatbot.conversation.sepif our_chatbot.conversation.sep_style != SeparatorStyle.TWOelse our_chatbot.conversation.sep2)keywords = [stop_str]stopping_criteria = KeywordsStoppingCriteria(keywords, our_chatbot.tokenizer, input_ids)# Generate responsewith torch.no_grad():outputs = our_chatbot.model.generate(inputs=input_ids,35 unmodified linesour_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], response)print(f"[DEBUG] Generated response length: {len(response)}")except Exception as e:print(f"[DEBUG] Response decoding error: {str(e)}")return {"error": f"Response decoding failed: {str(e)}"}235 unmodified linesprint("Handler module loaded successfully!")print("This handler is now ready for Hugging Face endpoints.")print("Use the 'query' function as the main endpoint.")print("Or use EndpointHandler class for Hugging Face compatibility.")346 unmodified lines34734834935035135235335435535635735835936036136236336436536636736836937037137237337437535 unmodified lines411412413414415416417418419235 unmodified lines655656657658346 unmodified linesprompt, our_chatbot.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(our_chatbot.model.device)# Set up stopping criteria - make it more flexiblestop_str = (our_chatbot.conversation.sepif our_chatbot.conversation.sep_style != SeparatorStyle.TWOelse our_chatbot.conversation.sep2)print(f"[DEBUG] Original stop_str: {stop_str}")# Use more flexible stopping criteria to allow longer responseskeywords = [stop_str] if stop_str else []if not keywords:# If no separator, use common end tokenskeywords = ["</s>", "<s>", "Human:", "Assistant:"]print(f"[DEBUG] Using keywords for stopping: {keywords}")stopping_criteria = KeywordsStoppingCriteria(keywords, our_chatbot.tokenizer, input_ids)# Generate responseprint(f"[DEBUG] Generating with max_new_tokens: {max_output_tokens}")print(f"[DEBUG] Stopping criteria: {stop_str}")with torch.no_grad():outputs = our_chatbot.model.generate(inputs=input_ids,35 unmodified linesour_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], response)print(f"[DEBUG] Generated response length: {len(response)}")print(f"[DEBUG] Response word count: {len(response.split())}")print(f"[DEBUG] Response preview: {response[:100]}...")print(f"[DEBUG] Response ends with: {response[-50:] if len(response) > 50 else response}")except Exception as e:print(f"[DEBUG] Response decoding error: {str(e)}")return {"error": f"Response decoding failed: {str(e)}"}235 unmodified linesprint("Handler module loaded successfully!")print("This handler is now ready for Hugging Face endpoints.")print("Use the 'query' function as the main endpoint.")print("Or use EndpointHandler class for Hugging Face compatibility.")
handler.py+17-3