Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,114 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from evo_transformer import EvoTransformer
|
| 3 |
-
from plots import plot_radar_chart
|
| 4 |
-
from diagrams import get_transformer_diagram
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
-
import
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
return (
|
| 30 |
-
f"{
|
| 31 |
-
f"{
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
json_path
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
current_config = gr.Textbox(label="Current Config Summary", lines=5)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
diagram_output = gr.Image(label="Illustrative Transformer Structure", height=300)
|
| 57 |
-
history_group = gr.Group()
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
evolve_btn.click(
|
| 64 |
-
fn=
|
| 65 |
-
inputs=[
|
| 66 |
outputs=[
|
| 67 |
accuracy_output,
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
]
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import json
|
| 4 |
+
import os
|
| 5 |
+
from evo_transformer import EvoTransformer
|
| 6 |
+
from plot import plot_radar_chart
|
| 7 |
+
from diagram import plot_architecture_diagram
|
| 8 |
|
| 9 |
+
# === Initialize ===
|
| 10 |
+
evo = EvoTransformer()
|
| 11 |
|
| 12 |
+
# === Gradio Output Placeholders ===
|
| 13 |
+
accuracy_output = gr.Text(label="Simulated Accuracy")
|
| 14 |
+
params_output = gr.Text(label="Estimated Parameters")
|
| 15 |
+
current_config_output = gr.Text(label="Current Config Summary")
|
| 16 |
+
radar_image = gr.Image(label="Final Generation Trait Radar")
|
| 17 |
+
diagram_image = gr.Image(label="Illustrative Transformer Structure")
|
| 18 |
+
history_outputs = []
|
| 19 |
|
| 20 |
+
# === Core Evolution Function ===
|
| 21 |
+
def evolve_model(generations):
|
| 22 |
+
evo.__init__() # Reset EvoTransformer
|
| 23 |
+
evo.evolve(generations)
|
| 24 |
+
score = evo.evaluate()
|
| 25 |
+
radar_path = "radar_chart.png"
|
| 26 |
+
diagram_path = "architecture_diagram.png"
|
| 27 |
|
| 28 |
+
# Plot radar chart
|
| 29 |
+
try:
|
| 30 |
+
plot_radar_chart(evo.config, radar_path)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print("Radar chart error:", e)
|
| 33 |
+
radar_path = None
|
| 34 |
+
|
| 35 |
+
# Plot architecture diagram
|
| 36 |
+
diagram_bytes = None
|
| 37 |
+
try:
|
| 38 |
+
plot_architecture_diagram(evo.config)
|
| 39 |
+
if os.path.exists(diagram_path):
|
| 40 |
+
with open(diagram_path, "rb") as f:
|
| 41 |
+
diagram_bytes = f.read()
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print("Diagram plot error:", e)
|
| 44 |
+
|
| 45 |
+
# History cards
|
| 46 |
+
history = evo.get_history()
|
| 47 |
+
history_cards = [f"Gen {i+1} Config: {h}" for i, h in enumerate(history)]
|
| 48 |
|
| 49 |
+
# History downloadables
|
| 50 |
+
df = pd.DataFrame(history)
|
| 51 |
+
df_csv_path = "evo_history.csv"
|
| 52 |
+
df.to_csv(df_csv_path, index=False)
|
| 53 |
|
| 54 |
+
json_path = "evo_history.json"
|
| 55 |
+
with open(json_path, "w") as f:
|
| 56 |
+
json.dump(history, f, indent=2)
|
| 57 |
|
| 58 |
return (
|
| 59 |
+
f"{score['accuracy']*100:.2f}%",
|
| 60 |
+
f"{score['params']:.2f}M params",
|
| 61 |
+
str(evo.config),
|
| 62 |
+
radar_path,
|
| 63 |
+
diagram_bytes,
|
| 64 |
+
*history_cards,
|
| 65 |
+
df_csv_path,
|
| 66 |
json_path
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# === Gradio Interface ===
|
| 70 |
+
generations_input = gr.Slider(1, 10, value=3, step=1, label="Number of Generations")
|
| 71 |
+
evolve_btn = gr.Button("\U0001F9EC Evolve Architecture")
|
| 72 |
|
| 73 |
+
with gr.Blocks(title="EvoTransformer") as demo:
|
| 74 |
+
gr.Markdown("""
|
| 75 |
+
# 🧬 EvoTransformer – Evolving Transformer Architectures
|
| 76 |
+
Simulate trait mutation and adaptive architecture generation.
|
| 77 |
+
""")
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
+
generations_input.render()
|
| 81 |
+
evolve_btn.render()
|
|
|
|
| 82 |
|
| 83 |
+
accuracy_output.render()
|
| 84 |
+
params_output.render()
|
| 85 |
+
current_config_output.render()
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
gr.Markdown("## 🧬 Evolution History")
|
| 88 |
+
radar_image.render()
|
| 89 |
+
diagram_image.render()
|
| 90 |
+
|
| 91 |
+
with gr.Accordion("Downloadable Files", open=True):
|
| 92 |
+
csv_file = gr.File(label="Download CSV History")
|
| 93 |
+
json_file = gr.File(label="Download JSON History")
|
| 94 |
+
|
| 95 |
+
for _ in range(10):
|
| 96 |
+
card = gr.Textbox(label="", interactive=False)
|
| 97 |
+
history_outputs.append(card)
|
| 98 |
|
| 99 |
evolve_btn.click(
|
| 100 |
+
fn=evolve_model,
|
| 101 |
+
inputs=[generations_input],
|
| 102 |
outputs=[
|
| 103 |
accuracy_output,
|
| 104 |
+
params_output,
|
| 105 |
+
current_config_output,
|
| 106 |
+
radar_image,
|
| 107 |
+
diagram_image,
|
| 108 |
+
*history_outputs,
|
| 109 |
+
csv_file,
|
| 110 |
+
json_file
|
| 111 |
+
]
|
| 112 |
)
|
| 113 |
|
| 114 |
+
demo.launch()
|
|
|