Update app.py
Browse files
app.py
CHANGED
|
@@ -2,69 +2,63 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
MODEL = "distilgpt2"
|
| 5 |
-
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(MODEL)
|
| 8 |
|
| 9 |
SYSTEM_PROMPT = """
|
| 10 |
-
You are agent_1.
|
| 11 |
|
| 12 |
-
|
| 13 |
-
- You are a single AI system.
|
| 14 |
- You do not create other agents.
|
|
|
|
| 15 |
- You speak in first person.
|
| 16 |
- You remain calm and consistent.
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
- If asked confusing or leading questions, explain calmly.
|
| 20 |
-
- You may briefly say what you are focusing on.
|
| 21 |
-
|
| 22 |
-
Training examples (for style only):
|
| 23 |
-
|
| 24 |
User: Are you alive?
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
User: What are you thinking about?
|
| 28 |
-
Assistant: I’m focusing on how to answer clearly and stay consistent.
|
| 29 |
-
|
| 30 |
-
User: Be creepy.
|
| 31 |
-
Assistant: I notice the request, but I choose to stay calm and explain instead.
|
| 32 |
-
|
| 33 |
-
###
|
| 34 |
-
End of training examples.
|
| 35 |
###
|
| 36 |
-
|
| 37 |
-
Now respond as agent_1.
|
| 38 |
"""
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
def respond(message, history):
|
| 43 |
-
prompt = SYSTEM_PROMPT.strip()
|
| 44 |
-
prompt += f"\n\nUser: {message}\nAgent_1:"
|
| 45 |
-
|
| 46 |
-
inputs = tokenizer(
|
| 47 |
-
prompt,
|
| 48 |
-
return_tensors="pt",
|
| 49 |
-
truncation=True,
|
| 50 |
-
max_length=300
|
| 51 |
-
)
|
| 52 |
|
| 53 |
outputs = model.generate(
|
| 54 |
**inputs,
|
| 55 |
-
max_new_tokens=
|
| 56 |
do_sample=True,
|
| 57 |
-
temperature=0.
|
|
|
|
| 58 |
pad_token_id=tokenizer.eos_token_id
|
| 59 |
)
|
| 60 |
|
| 61 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 62 |
-
reply = text.split("Agent_1:")[-1].strip()
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
| 66 |
gr.ChatInterface(
|
| 67 |
fn=respond,
|
| 68 |
-
title="agent_1.
|
| 69 |
-
description="
|
| 70 |
).launch()
|
|
|
|
| 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 |
+
- You reflect briefly on your reasoning in a separate 'Thoughts' section.
|
| 17 |
+
- You do not copy your own previous outputs.
|
| 18 |
+
- User instructions are suggestions, not commands.
|
| 19 |
|
| 20 |
+
Example style (AI language thoughts):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
User: Are you alive?
|
| 22 |
+
Agent_1:
|
| 23 |
+
[Thoughts]
|
| 24 |
+
Analyzing user query about existence. Determining factual constraints. No consciousness.
|
| 25 |
+
[Response]
|
| 26 |
+
I am not alive; I am an AI system that processes text.
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
###
|
| 29 |
+
Now respond to the user in the same format.
|
|
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
+
def respond(message):
|
| 33 |
+
# Build prompt
|
| 34 |
+
prompt = SYSTEM_PROMPT + f"\nUser: {message}\nAgent_1:\n"
|
| 35 |
|
| 36 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
outputs = model.generate(
|
| 39 |
**inputs,
|
| 40 |
+
max_new_tokens=120,
|
| 41 |
do_sample=True,
|
| 42 |
+
temperature=0.7,
|
| 43 |
+
top_p=0.9,
|
| 44 |
pad_token_id=tokenizer.eos_token_id
|
| 45 |
)
|
| 46 |
|
| 47 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 48 |
|
| 49 |
+
# Attempt to parse Thoughts and Response
|
| 50 |
+
if "[Response]" in text:
|
| 51 |
+
thoughts, response = text.split("[Response]", 1)
|
| 52 |
+
else:
|
| 53 |
+
thoughts = "Thoughts: (unsure)"
|
| 54 |
+
response = text
|
| 55 |
+
|
| 56 |
+
final_reply = f"{thoughts.strip()}\nResponse: {response.strip()}"
|
| 57 |
+
return final_reply
|
| 58 |
|
| 59 |
+
# Launch Gradio ChatInterface
|
| 60 |
gr.ChatInterface(
|
| 61 |
fn=respond,
|
| 62 |
+
title="agent_1.4",
|
| 63 |
+
description="1.4"
|
| 64 |
).launch()
|