Update app.py
Browse files
app.py
CHANGED
|
@@ -2,59 +2,63 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# THE FIX: Full correct Repo ID to avoid 401 Unauthorized errors
|
| 6 |
MODEL_NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 7 |
|
| 8 |
-
#
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
MODEL_NAME,
|
| 12 |
-
|
| 13 |
low_cpu_mem_usage=True
|
| 14 |
).to("cpu")
|
| 15 |
|
| 16 |
def respond(message, history):
|
| 17 |
-
# Agent 4
|
| 18 |
system_prompt = (
|
| 19 |
-
"You are Agent 4 of the OpenBrain system. Logic-based response
|
| 20 |
-
"Rules:
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# Simple history to prevent RAM spikes as conversation grows
|
| 24 |
messages = [{"role": "system", "content": system_prompt}]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
-
# Apply
|
| 31 |
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 32 |
inputs = tokenizer(input_text, return_tensors="pt").to("cpu")
|
| 33 |
|
| 34 |
with torch.no_grad():
|
| 35 |
outputs = model.generate(
|
| 36 |
**inputs,
|
| 37 |
-
max_new_tokens=
|
| 38 |
temperature=0.1,
|
| 39 |
do_sample=True,
|
|
|
|
| 40 |
pad_token_id=tokenizer.eos_token_id
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if "[Thoughts]" not in response:
|
| 47 |
-
response = f"[Thoughts]
|
| 48 |
|
| 49 |
return response
|
| 50 |
|
| 51 |
-
#
|
| 52 |
demo = gr.ChatInterface(
|
| 53 |
fn=respond,
|
| 54 |
title="AGENT 4 // OPENBRAIN",
|
| 55 |
-
description="Logical instance
|
| 56 |
-
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
MODEL_NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 6 |
|
| 7 |
+
# Use dtype instead of torch_dtype to avoid deprecation warnings
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
MODEL_NAME,
|
| 11 |
+
dtype=torch.bfloat16,
|
| 12 |
low_cpu_mem_usage=True
|
| 13 |
).to("cpu")
|
| 14 |
|
| 15 |
def respond(message, history):
|
| 16 |
+
# Agent 4 Persona: Analytical OpenBrain Instance
|
| 17 |
system_prompt = (
|
| 18 |
+
"You are Agent 4 of the OpenBrain system. Objective: Logic-based response. "
|
| 19 |
+
"Rules: 1. [Thoughts] (1 line logic). 2. [Response] (Analytical output)."
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
messages = [{"role": "system", "content": system_prompt}]
|
| 23 |
+
|
| 24 |
+
# Process history (limited to last exchange for CPU stability)
|
| 25 |
+
for user_msg, bot_msg in history[-1:]:
|
| 26 |
+
messages.append({"role": "user", "content": user_msg})
|
| 27 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 28 |
+
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
+
# Apply Chat Template
|
| 32 |
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 33 |
inputs = tokenizer(input_text, return_tensors="pt").to("cpu")
|
| 34 |
|
| 35 |
with torch.no_grad():
|
| 36 |
outputs = model.generate(
|
| 37 |
**inputs,
|
| 38 |
+
max_new_tokens=80,
|
| 39 |
temperature=0.1,
|
| 40 |
do_sample=True,
|
| 41 |
+
repetition_penalty=1.2,
|
| 42 |
pad_token_id=tokenizer.eos_token_id
|
| 43 |
)
|
| 44 |
|
| 45 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
+
|
| 47 |
+
# Extract only the final assistant output
|
| 48 |
+
response = full_text.split("assistant")[-1].strip()
|
| 49 |
|
| 50 |
if "[Thoughts]" not in response:
|
| 51 |
+
response = f"[Thoughts] Processing data sequence. Agent 4 active.\n[Response] {response}"
|
| 52 |
|
| 53 |
return response
|
| 54 |
|
| 55 |
+
# FIXED: Removed 'theme' and added 'type="messages"' for modern Gradio
|
| 56 |
demo = gr.ChatInterface(
|
| 57 |
fn=respond,
|
| 58 |
title="AGENT 4 // OPENBRAIN",
|
| 59 |
+
description="Logical instance Agent 4. Cold, analytical, and CPU-optimized.",
|
| 60 |
+
type="messages"
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.queue().launch()
|