Spaces:
Sleeping
Sleeping
# app.py | |
import gradio as gr | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import io | |
from evo_transformer import EvoTransformer | |
def run_evolution(generations): | |
evo = EvoTransformer() | |
evo.evolve(generations) | |
history = evo.get_history() | |
final_eval = evo.evaluate() | |
df = pd.DataFrame(history) | |
df_display = df.copy() | |
df_display["memory"] = df_display["memory"].apply(lambda x: "Enabled" if x else "Disabled") | |
# Radar chart | |
last_config = history[-1] | |
traits = ["layers", "attention_heads", "ffn_dim", "dropout", "memory"] | |
values = [last_config["layers"], last_config["attention_heads"], | |
last_config["ffn_dim"]/100, last_config["dropout"]*10, int(last_config["memory"])*10] | |
fig, ax = plt.subplots(figsize=(6,6), subplot_kw=dict(polar=True)) | |
angles = [n / float(len(traits)) * 2 * 3.14159 for n in range(len(traits))] | |
values += values[:1] | |
angles += angles[:1] | |
ax.plot(angles, values, linewidth=2) | |
ax.fill(angles, values, alpha=0.3) | |
ax.set_xticks(angles[:-1]) | |
ax.set_xticklabels(traits) | |
ax.set_title("Final Architecture Traits", size=15) | |
# File downloads | |
csv_file = io.StringIO(evo.export_csv()) | |
json_file = io.StringIO(evo.export_json()) | |
return ( | |
df_display, | |
final_eval["accuracy"], | |
final_eval["params"], | |
fig, | |
("evo_history.csv", csv_file.getvalue()), | |
("evo_history.json", json_file.getvalue()) | |
) | |
with gr.Blocks(title="EvoTransformer Demo") as demo: | |
gr.Markdown("# 🧬 EvoTransformer Live Demo") | |
gr.Markdown("This demo evolves a Transformer architecture and displays how traits change over generations.") | |
with gr.Row(): | |
generations = gr.Slider(1, 10, value=5, step=1, label="Generations") | |
run_btn = gr.Button("Evolve Now 🚀") | |
with gr.Row(): | |
acc = gr.Number(label="Estimated Accuracy") | |
params = gr.Number(label="Estimated Params (M)") | |
table = gr.Dataframe(label="Evolution History", wrap=True) | |
plot = gr.Plot(label="Final Architecture (Radar Chart)") | |
with gr.Row(): | |
csv_dl = gr.File(label="Download CSV", file_types=[".csv"], interactive=True) | |
json_dl = gr.File(label="Download JSON", file_types=[".json"], interactive=True) | |
run_btn.click(fn=run_evolution, inputs=generations, outputs=[table, acc, params, plot, csv_dl, json_dl]) | |
demo.launch() | |