lap096 commited on
Commit
a8dbebf
·
verified ·
1 Parent(s): 8ef40b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
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
- # CPU-Safe Loading: Use float16 to keep RAM well under the 16GB limit
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
  model = AutoModelForCausalLM.from_pretrained(
11
  MODEL_NAME,
12
- torch_dtype=torch.float16,
13
  low_cpu_mem_usage=True
14
  ).to("cpu")
15
 
16
  def respond(message, history):
17
- # Agent 4 / OpenBrain Persona
18
  system_prompt = (
19
- "You are Agent 4 of the OpenBrain system. Logic-based response only. "
20
- "Rules: Start with [Thoughts] (1 line), then [Response]. Be cold and precise."
21
  )
22
 
23
- # Simple history to prevent RAM spikes as conversation grows
24
  messages = [{"role": "system", "content": system_prompt}]
25
- for h in history[-1:]:
26
- messages.append({"role": "user", "content": h[0]})
27
- messages.append({"role": "assistant", "content": h[1]})
 
 
 
28
  messages.append({"role": "user", "content": message})
29
 
30
- # Apply official template
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=60,
38
  temperature=0.1,
39
  do_sample=True,
 
40
  pad_token_id=tokenizer.eos_token_id
41
  )
42
 
43
- # Clean decoding
44
- response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
 
 
45
 
46
  if "[Thoughts]" not in response:
47
- response = f"[Thoughts] Analyzing data stream. Instance Agent 4 active.\n[Response] {response}"
48
 
49
  return response
50
 
51
- # Standard Gradio ChatInterface
52
  demo = gr.ChatInterface(
53
  fn=respond,
54
  title="AGENT 4 // OPENBRAIN",
55
- description="Logical instance running on optimized CPU memory.",
56
- theme="soft"
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()