Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- agents.py +0 -3
- app.py +23 -4
- requirements.txt +2 -1
- shared_memory.py +4 -0
agents.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
import random
|
| 2 |
-
import math
|
| 3 |
import operator
|
| 4 |
|
| 5 |
-
ops = [('+', operator.add), ('-', operator.sub), ('*', operator.mul), ('/', lambda a, b: a / b if b != 0 else 1)]
|
| 6 |
-
|
| 7 |
def random_expr():
|
| 8 |
return f"{random.randint(1, 10)} {random.choice(['+', '-', '*', '/'])} {random.randint(1, 10)}"
|
| 9 |
|
|
|
|
| 1 |
import random
|
|
|
|
| 2 |
import operator
|
| 3 |
|
|
|
|
|
|
|
| 4 |
def random_expr():
|
| 5 |
return f"{random.randint(1, 10)} {random.choice(['+', '-', '*', '/'])} {random.randint(1, 10)}"
|
| 6 |
|
app.py
CHANGED
|
@@ -2,15 +2,34 @@ import gradio as gr
|
|
| 2 |
from agents import evolve_agent
|
| 3 |
from emotion import get_emotion_level
|
| 4 |
from shared_memory import MemoryStore
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
memory = MemoryStore()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def evolve_step(target_value: float, emotion_input: str):
|
| 9 |
emotion_mod = get_emotion_level(emotion_input)
|
| 10 |
result_a = evolve_agent('Agent A', target_value, emotion_mod, memory)
|
| 11 |
result_b = evolve_agent('Agent B', target_value, emotion_mod, memory)
|
| 12 |
best = max([result_a, result_b], key=lambda x: x['score'])
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
demo = gr.Interface(
|
| 16 |
fn=evolve_step,
|
|
@@ -18,9 +37,9 @@ demo = gr.Interface(
|
|
| 18 |
gr.Number(label="Target Value"),
|
| 19 |
gr.Radio(["Calm", "Frustrated", "Curious"], label="User Emotion")
|
| 20 |
],
|
| 21 |
-
outputs="text",
|
| 22 |
-
title="Multi-Agent Evolution Sandbox",
|
| 23 |
-
description="Two agents evolve
|
| 24 |
)
|
| 25 |
|
| 26 |
demo.launch()
|
|
|
|
| 2 |
from agents import evolve_agent
|
| 3 |
from emotion import get_emotion_level
|
| 4 |
from shared_memory import MemoryStore
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import io
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
memory = MemoryStore()
|
| 10 |
|
| 11 |
+
def plot_scores(score_history):
|
| 12 |
+
plt.figure(figsize=(6, 3))
|
| 13 |
+
for agent, scores in score_history.items():
|
| 14 |
+
plt.plot(scores, label=agent)
|
| 15 |
+
plt.xlabel("Iteration")
|
| 16 |
+
plt.ylabel("Score")
|
| 17 |
+
plt.title("Agent Performance Over Time")
|
| 18 |
+
plt.legend()
|
| 19 |
+
buf = io.BytesIO()
|
| 20 |
+
plt.savefig(buf, format='png')
|
| 21 |
+
buf.seek(0)
|
| 22 |
+
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
|
| 23 |
+
plt.close()
|
| 24 |
+
return f"data:image/png;base64,{img_base64}"
|
| 25 |
+
|
| 26 |
def evolve_step(target_value: float, emotion_input: str):
|
| 27 |
emotion_mod = get_emotion_level(emotion_input)
|
| 28 |
result_a = evolve_agent('Agent A', target_value, emotion_mod, memory)
|
| 29 |
result_b = evolve_agent('Agent B', target_value, emotion_mod, memory)
|
| 30 |
best = max([result_a, result_b], key=lambda x: x['score'])
|
| 31 |
+
chart = plot_scores(memory.score_history)
|
| 32 |
+
return (f"Best result: {best['agent']} => Expression: {best['expression']} (Score: {best['score']})", chart)
|
| 33 |
|
| 34 |
demo = gr.Interface(
|
| 35 |
fn=evolve_step,
|
|
|
|
| 37 |
gr.Number(label="Target Value"),
|
| 38 |
gr.Radio(["Calm", "Frustrated", "Curious"], label="User Emotion")
|
| 39 |
],
|
| 40 |
+
outputs=["text", gr.Image(type="auto")],
|
| 41 |
+
title="Multi-Agent Evolution Sandbox (with Score Graph)",
|
| 42 |
+
description="Two agents evolve expressions to match a target number. Emotions influence exploration. See real-time performance graph."
|
| 43 |
)
|
| 44 |
|
| 45 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
gradio
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
matplotlib
|
shared_memory.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
class MemoryStore:
|
| 2 |
def __init__(self):
|
| 3 |
self.records = {}
|
|
|
|
| 4 |
|
| 5 |
def store(self, agent, expr, score):
|
| 6 |
if agent not in self.records or self.records[agent]['score'] < score:
|
| 7 |
self.records[agent] = {'expression': expr, 'score': score}
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def get(self, agent):
|
| 10 |
return self.records.get(agent, None)
|
|
|
|
| 1 |
class MemoryStore:
|
| 2 |
def __init__(self):
|
| 3 |
self.records = {}
|
| 4 |
+
self.score_history = {}
|
| 5 |
|
| 6 |
def store(self, agent, expr, score):
|
| 7 |
if agent not in self.records or self.records[agent]['score'] < score:
|
| 8 |
self.records[agent] = {'expression': expr, 'score': score}
|
| 9 |
+
if agent not in self.score_history:
|
| 10 |
+
self.score_history[agent] = []
|
| 11 |
+
self.score_history[agent].append(score)
|
| 12 |
|
| 13 |
def get(self, agent):
|
| 14 |
return self.records.get(agent, None)
|