Update app.py
Browse files3 unmodified lines456789101112131415161718192021222324252627282930313233349 unmodified lines44454647484950515253545556575859606162633 unmodified linesMODEL_NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"# Use dtype instead of torch_dtype to avoid deprecation warningstokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,dtype=torch.bfloat16,low_cpu_mem_usage=True).to("cpu")def respond(message, history):# Agent 4 Persona: Analytical OpenBrain Instancesystem_prompt = ("You are Agent 4 of the OpenBrain system. Objective: Logic-based response. ""Rules: 1. [Thoughts] (1 line logic). 2. [Response] (Analytical output).")messages = [{"role": "system", "content": system_prompt}]# Process history (limited to last exchange for CPU stability)for user_msg, bot_msg in history[-1:]:messages.append({"role": "user", "content": user_msg})messages.append({"role": "assistant", "content": bot_msg})messages.append({"role": "user", "content": message})# Apply Chat Templateinput_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer(input_text, return_tensors="pt").to("cpu")9 unmodified linesfull_text = tokenizer.decode(outputs[0], skip_special_tokens=True)# Extract only the final assistant outputresponse = full_text.split("assistant")[-1].strip()if "[Thoughts]" not in response:response = f"[Thoughts] Processing data sequence. Agent 4 active.\n[Response] {response}"return response# FIXED: Removed 'theme' and added 'type="messages"' for modern Gradiodemo = gr.ChatInterface(fn=respond,title="AGENT 4 // OPENBRAIN",description="Logical instance Agent 4. Cold, analytical, and CPU-optimized.",type="messages")if __name__ == "__main__":3 unmodified lines4567891011121314151617181920212223242526272829303132339 unmodified lines434445464748495051525354555657585960613 unmodified linesMODEL_NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"# CPU-Safe Loadingtokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,torch_dtype=torch.bfloat16,low_cpu_mem_usage=True).to("cpu")def respond(message, history):# Agent 4 Personasystem_prompt = ("You are Agent 4 of the OpenBrain system. Logic-based response. ""Rules: 1. [Thoughts] (1 line logic). 2. [Response] (Analytical output).")messages = [{"role": "system", "content": system_prompt}]# Simple history loop compatible with all Gradio versionsfor interaction in history[-1:]:messages.append({"role": "user", "content": interaction[0]})messages.append({"role": "assistant", "content": interaction[1]})messages.append({"role": "user", "content": message})input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer(input_text, return_tensors="pt").to("cpu")9 unmodified linesfull_text = tokenizer.decode(outputs[0], skip_special_tokens=True)# Extract only the final responseresponse = full_text.split("assistant")[-1].strip()if "[Thoughts]" not in response:response = f"[Thoughts] Logic sequence initiated. Instance Agent 4 active.\n[Response] {response}"return response# Minimalist Interface to avoid 'unexpected keyword' errorsdemo = gr.ChatInterface(fn=respond,title="AGENT 4 // OPENBRAIN",description="Logical instance Agent 4. Cold and analytical.",)if __name__ == "__main__":
app.py+12-14