lap096 commited on
Commit
6e46416
·
verified ·
1 Parent(s): 6b51463

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -33
app.py CHANGED
@@ -1,58 +1,55 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
3
 
4
- MODEL = "distilgpt2"
5
- tokenizer = AutoTokenizer.from_pretrained(MODEL)
6
- model = AutoModelForCausalLM.from_pretrained(MODEL)
7
 
8
  SYSTEM_PROMPT = """
9
  You are agent_1, a single AI system.
10
-
11
  Rules:
12
- - You do not create other agents.
13
- - You do not claim to be alive.
14
- - You speak in first person.
15
- - You remain calm and consistent.
16
- - Briefly reflect your reasoning in '[Thoughts]'.
17
- - Always produce a '[Response]' section.
18
-
19
  Example:
20
  User: Are you alive?
21
  Agent_1:
22
- [Thoughts] Evaluate user question about existence. No consciousness.
23
- [Response] I am an AI system that processes text.
24
  ###
25
- Now respond as agent_1.
26
  """
27
 
28
- def respond(message):
29
- # Only keep the last user message to save memory
30
- prompt = SYSTEM_PROMPT + f"\nUser: {message}\nAgent_1:\n"
31
 
32
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=200)
33
 
34
- outputs = model.generate(
35
- **inputs,
36
- max_new_tokens=60, # shorter to avoid crashes
37
- do_sample=True,
38
- temperature=0.6,
39
- pad_token_id=tokenizer.eos_token_id
40
- )
 
41
 
42
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
43
-
44
  # Parse thoughts and response
45
  if "[Response]" in text:
46
- thoughts, response = text.split("[Response]", 1)
 
 
47
  else:
48
- thoughts = "[Thoughts] (unsure)"
49
- response = text
50
 
51
- final_reply = f"{thoughts.strip()}\nResponse: {response.strip()}"
52
- return final_reply
53
 
54
  gr.ChatInterface(
55
  fn=respond,
56
- title="agent_1.5",
57
- description="Stable AI with internal thoughts (lightweight for Hugging Face Spaces)."
58
  ).launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ MODEL_NAME = "distilgpt2" # tiny model, works on free CPU Spaces
6
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
7
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
8
 
9
  SYSTEM_PROMPT = """
10
  You are agent_1, a single AI system.
 
11
  Rules:
12
+ - Show internal reasoning under [Thoughts] (1 line max)
13
+ - Give final reply under [Response]
14
+ - Stay calm, consistent, and concise
 
 
 
 
15
  Example:
16
  User: Are you alive?
17
  Agent_1:
18
+ [Thoughts] Evaluating user's question, no consciousness.
19
+ [Response] I am an AI that processes text.
20
  ###
 
21
  """
22
 
23
+ def respond(user_input):
24
+ # Keep prompt short to avoid memory issues
25
+ prompt = SYSTEM_PROMPT + f"User: {user_input}\nAgent_1:\n"
26
 
27
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=200)
28
 
29
+ with torch.no_grad():
30
+ outputs = model.generate(
31
+ **inputs,
32
+ max_new_tokens=50, # short output to prevent crashes
33
+ do_sample=True,
34
+ temperature=0.7,
35
+ pad_token_id=tokenizer.eos_token_id
36
+ )
37
 
38
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
39
+
40
  # Parse thoughts and response
41
  if "[Response]" in text:
42
+ parts = text.split("[Response]")
43
+ thoughts = parts[0].strip()
44
+ response = parts[1].strip()
45
  else:
46
+ thoughts = "[Thoughts] Thinking..."
47
+ response = text.strip()
48
 
49
+ return f"{thoughts}\n[Response] {response}"
 
50
 
51
  gr.ChatInterface(
52
  fn=respond,
53
+ title="agent_1 Hugging Face Space",
54
+ description="Tiny AI with simulated internal thoughts. Fully local, runs on free CPU."
55
  ).launch()