Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,11 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 5 |
from minimal_self_full import MinimalSelf, MovingObstacle, SocialEntity, run_simulation, compute_phi
|
| 6 |
|
| 7 |
-
plt.switch_backend("Agg") #
|
| 8 |
|
| 9 |
def run_agent(
|
| 10 |
steps=500,
|
|
@@ -46,9 +48,10 @@ def run_agent(
|
|
| 46 |
# Plot metrics
|
| 47 |
fig1, axes = plt.subplots(4, 1, figsize=(10, 9), sharex=True)
|
| 48 |
metrics = ["predictive_rate", "C_min", "body_bit_strength", "reward"]
|
|
|
|
| 49 |
for i, m in enumerate(metrics):
|
| 50 |
if m in df.columns:
|
| 51 |
-
axes[i].plot(df["t"], df[m], label=m, color=[
|
| 52 |
axes[i].set_ylabel(m)
|
| 53 |
axes[i].grid(True)
|
| 54 |
axes[i].legend()
|
|
@@ -58,7 +61,7 @@ def run_agent(
|
|
| 58 |
fig1.suptitle("Metrics over time")
|
| 59 |
fig1.tight_layout()
|
| 60 |
|
| 61 |
-
# Plot path
|
| 62 |
fig2, ax = plt.subplots(figsize=(6, 6))
|
| 63 |
ax.set_title("Agent and environment paths")
|
| 64 |
ax.set_xlabel("X")
|
|
@@ -88,14 +91,16 @@ def run_agent(
|
|
| 88 |
|
| 89 |
ax.legend()
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
return (
|
| 95 |
gr.Plot(fig1),
|
| 96 |
gr.Plot(fig2),
|
| 97 |
f"{final_phi:.2f}",
|
| 98 |
-
|
| 99 |
)
|
| 100 |
|
| 101 |
with gr.Blocks(title="RFT Minimal Self: 3×3 Agent") as demo:
|
|
@@ -117,7 +122,7 @@ with gr.Blocks(title="RFT Minimal Self: 3×3 Agent") as demo:
|
|
| 117 |
metrics_plot = gr.Plot(label="Metrics over time")
|
| 118 |
path_plot = gr.Plot(label="Paths in 3×3 world")
|
| 119 |
final_phi = gr.Textbox(label="Final Φ_min (toy measure)", interactive=False)
|
| 120 |
-
csv_out = gr.File(label="Download results.csv",
|
| 121 |
|
| 122 |
run_btn.click(
|
| 123 |
fn=run_agent,
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
from minimal_self_full import MinimalSelf, MovingObstacle, SocialEntity, run_simulation, compute_phi
|
| 8 |
|
| 9 |
+
plt.switch_backend("Agg") # ensure headless plotting
|
| 10 |
|
| 11 |
def run_agent(
|
| 12 |
steps=500,
|
|
|
|
| 48 |
# Plot metrics
|
| 49 |
fig1, axes = plt.subplots(4, 1, figsize=(10, 9), sharex=True)
|
| 50 |
metrics = ["predictive_rate", "C_min", "body_bit_strength", "reward"]
|
| 51 |
+
colors = ["#2b8", "#06c", "#a5a", "#e67"]
|
| 52 |
for i, m in enumerate(metrics):
|
| 53 |
if m in df.columns:
|
| 54 |
+
axes[i].plot(df["t"], df[m], label=m, color=colors[i])
|
| 55 |
axes[i].set_ylabel(m)
|
| 56 |
axes[i].grid(True)
|
| 57 |
axes[i].legend()
|
|
|
|
| 61 |
fig1.suptitle("Metrics over time")
|
| 62 |
fig1.tight_layout()
|
| 63 |
|
| 64 |
+
# Plot path
|
| 65 |
fig2, ax = plt.subplots(figsize=(6, 6))
|
| 66 |
ax.set_title("Agent and environment paths")
|
| 67 |
ax.set_xlabel("X")
|
|
|
|
| 91 |
|
| 92 |
ax.legend()
|
| 93 |
|
| 94 |
+
# Save CSV to a temporary file and return path
|
| 95 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 96 |
+
df.to_csv(tmp.name, index=False)
|
| 97 |
+
tmp.close()
|
| 98 |
|
| 99 |
return (
|
| 100 |
gr.Plot(fig1),
|
| 101 |
gr.Plot(fig2),
|
| 102 |
f"{final_phi:.2f}",
|
| 103 |
+
tmp.name # return path, not bytes
|
| 104 |
)
|
| 105 |
|
| 106 |
with gr.Blocks(title="RFT Minimal Self: 3×3 Agent") as demo:
|
|
|
|
| 122 |
metrics_plot = gr.Plot(label="Metrics over time")
|
| 123 |
path_plot = gr.Plot(label="Paths in 3×3 world")
|
| 124 |
final_phi = gr.Textbox(label="Final Φ_min (toy measure)", interactive=False)
|
| 125 |
+
csv_out = gr.File(label="Download results.csv", file_types=[".csv"])
|
| 126 |
|
| 127 |
run_btn.click(
|
| 128 |
fn=run_agent,
|