Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 6 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 7 |
|
| 8 |
SYSTEM_PROMPT = """
|
| 9 |
You are agent_1, a single AI system.
|
| 10 |
-
|
| 11 |
Rules:
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
-
|
| 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]
|
| 23 |
-
[Response] I am an AI
|
| 24 |
###
|
| 25 |
-
Now respond as agent_1.
|
| 26 |
"""
|
| 27 |
|
| 28 |
-
def respond(
|
| 29 |
-
#
|
| 30 |
-
prompt = SYSTEM_PROMPT + f"
|
| 31 |
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=200)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
-
|
| 44 |
# Parse thoughts and response
|
| 45 |
if "[Response]" in text:
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
else:
|
| 48 |
-
thoughts = "[Thoughts]
|
| 49 |
-
response = text
|
| 50 |
|
| 51 |
-
|
| 52 |
-
return final_reply
|
| 53 |
|
| 54 |
gr.ChatInterface(
|
| 55 |
fn=respond,
|
| 56 |
-
title="agent_1
|
| 57 |
-
description="
|
| 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()
|