Spaces:
Sleeping
Sleeping
Update app.py
Browse files115 unmodified lines11611711811912012112212312412512612712812913013113213313413513613713838 unmodified lines17717817918018118218318418518618718818919019126 unmodified lines218219220221222223224225226115 unmodified linesreturn []def format_conversation(history, system_prompt, tokenizer):if hasattr(tokenizer, "chat_template") and tokenizer.chat_template:if len(history) > 0:messages = [{"role": "system", "content": system_prompt.strip()}] + historyelse:messages = [{"role": "system", "content": system_prompt.strip()}]return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=True)else:# Fallback for base LMs without chat templateprompt = system_prompt.strip() + "\n"if len(history) > 0:for msg in history:if msg['role'] == 'user':prompt += "User: " + msg['content'].strip() + "\n"elif msg['role'] == 'assistant':prompt += "Assistant: " + msg['content'].strip() + "\n"if not prompt.strip().endswith("Assistant:"):prompt += "Assistant: "return promptdef chat_response(user_msg, chat_history, system_prompt,38 unmodified linesenriched = system_promptpipe = load_pipeline(model_name)# TODO:debug += "\nLOAD MODEL:\n" + model_nameprompt = format_conversation(history, enriched, pipe["tokenizer"])# TODO:debug += "\nPROMPT:\n" + promptprompt_debug = f"\n\n--- Prompt Preview ---\n```\n{prompt}\n```"streamer = TextIterStreamer(pipe["tokenizer"],skip_prompt=True,26 unmodified linesbreaktext = chunk# TODO:debug += "\nRESPONSE:\n" + text# Detect start of thinkingif not in_thought and '<think>' in text:in_thought = True115 unmodified lines11611711811912012112212312412512612712812913013113213313413513613738 unmodified lines17617717817918018118226 unmodified lines209210211212213214115 unmodified linesreturn []def format_conversation(history, system_prompt, tokenizer):if history is None:history = []if hasattr(tokenizer, "chat_template") and tokenizer.chat_template:messages = [{"role": "system", "content": system_prompt.strip()}] + historyreturn tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=True)else:# Fallback for base LMs without chat templateprompt = system_prompt.strip() + "\n"for msg in history:if msg['role'] == 'user':prompt += "User: " + msg['content'].strip() + "\n"elif msg['role'] == 'assistant':prompt += "Assistant: " + msg['content'].strip() + "\n"if not prompt.strip().endswith("Assistant:"):prompt += "Assistant: "return promptdef chat_response(user_msg, chat_history, system_prompt,38 unmodified linesenriched = system_promptpipe = load_pipeline(model_name)prompt = format_conversation(history, enriched, pipe["tokenizer"])prompt_debug = f"\n\n--- Prompt Preview ---\n```\n{prompt}\n```"streamer = TextIterStreamer(pipe["tokenizer"],skip_prompt=True,26 unmodified linesbreaktext = chunk# Detect start of thinkingif not in_thought and '<think>' in text:in_thought = True
app.py+11-23