Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,75 @@
|
|
1 |
import gradio as gr
|
2 |
-
from evo_transformer import EvoTransformer
|
3 |
-
import pandas as pd
|
4 |
import json
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
eval_result = model.evaluate()
|
42 |
-
|
43 |
-
summary = "\n".join([f"{k}: {v}" for k, v in history[-1].items()])
|
44 |
-
history_txt = [json.dumps(h, indent=2) for h in history]
|
45 |
-
while len(history_txt) < 10:
|
46 |
-
history_txt.append("")
|
47 |
-
|
48 |
-
# Save CSV and JSON
|
49 |
-
df = pd.DataFrame(history)
|
50 |
-
csv_path = tempfile.NamedTemporaryFile(suffix=".csv", delete=False).name
|
51 |
-
json_path = tempfile.NamedTemporaryFile(suffix=".json", delete=False).name
|
52 |
-
df.to_csv(csv_path, index=False)
|
53 |
-
with open(json_path, "w") as jf:
|
54 |
-
json.dump(history, jf, indent=2)
|
55 |
-
|
56 |
-
radar_img = plot_radar(history)
|
57 |
-
|
58 |
-
return (
|
59 |
-
f"{eval_result['accuracy']*100:.2f}%",
|
60 |
-
f"{eval_result['params']:.2f}M params",
|
61 |
-
summary,
|
62 |
-
*history_txt,
|
63 |
-
radar_img,
|
64 |
-
csv_path,
|
65 |
-
json_path,
|
66 |
-
)
|
67 |
-
except Exception as e:
|
68 |
-
print("🚨 ERROR during evolution:", str(e))
|
69 |
-
import traceback
|
70 |
-
traceback.print_exc()
|
71 |
-
|
72 |
-
return (
|
73 |
-
"Error", "Error", "Error",
|
74 |
-
*["Error"] * 10,
|
75 |
-
None, None, None
|
76 |
-
)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
gr.Markdown("
|
|
|
|
|
|
|
81 |
|
82 |
with gr.Row():
|
83 |
-
|
84 |
-
evolve_btn = gr.Button("
|
85 |
|
86 |
with gr.Row():
|
87 |
-
acc_out = gr.
|
88 |
-
param_out = gr.
|
89 |
|
90 |
summary_out = gr.Textbox(label="Current Config Summary", lines=5)
|
91 |
|
92 |
-
|
93 |
-
|
|
|
94 |
|
95 |
-
|
|
|
96 |
|
97 |
with gr.Row():
|
98 |
-
|
99 |
-
|
100 |
|
101 |
evolve_btn.click(
|
102 |
-
|
103 |
-
inputs=[
|
104 |
-
outputs=[
|
105 |
-
acc_out,
|
106 |
-
param_out,
|
107 |
-
summary_out,
|
108 |
-
*hist_outputs,
|
109 |
-
radar_plot,
|
110 |
-
csv_out,
|
111 |
-
json_out
|
112 |
-
],
|
113 |
)
|
114 |
|
115 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import json
|
3 |
+
import pandas as pd
|
4 |
+
from evo_transformer import EvoTransformer
|
5 |
+
from plots import plot_radar
|
6 |
+
from diagrams import draw_transformer_diagram
|
7 |
+
|
8 |
+
# Initialize EvoTransformer
|
9 |
+
evo = EvoTransformer()
|
10 |
+
|
11 |
+
def evolve_transformer(generations):
|
12 |
+
evo = EvoTransformer()
|
13 |
+
evo.evolve(generations)
|
14 |
+
history = evo.get_history()
|
15 |
+
final_config = history[-1]
|
16 |
+
eval_result = evo.evaluate()
|
17 |
+
|
18 |
+
radar_path = plot_radar(final_config)
|
19 |
+
diagram_path = draw_transformer_diagram()
|
20 |
+
|
21 |
+
csv_path = "history.csv"
|
22 |
+
json_path = "history.json"
|
23 |
+
pd.DataFrame(history).to_csv(csv_path, index=False)
|
24 |
+
with open(json_path, "w") as f:
|
25 |
+
json.dump(history, f, indent=2)
|
26 |
+
|
27 |
+
# Format config display
|
28 |
+
summary = "\n".join(f"{k}: {v}" for k, v in final_config.items())
|
29 |
+
|
30 |
+
return (
|
31 |
+
f"{eval_result['accuracy']*100:.2f}%",
|
32 |
+
f"{eval_result['params']:.2f}M params",
|
33 |
+
summary,
|
34 |
+
radar_path,
|
35 |
+
diagram_path,
|
36 |
+
csv_path,
|
37 |
+
json_path
|
38 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Gradio Interface
|
41 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
42 |
+
gr.Markdown("""
|
43 |
+
# 🧬 EvoTransformer – Evolving Transformer Architectures
|
44 |
+
Simulate trait mutation and adaptive architecture generation.
|
45 |
+
""")
|
46 |
|
47 |
with gr.Row():
|
48 |
+
gens = gr.Slider(1, 10, value=3, step=1, label="Number of Generations")
|
49 |
+
evolve_btn = gr.Button("\ud83e\uddec Evolve Architecture")
|
50 |
|
51 |
with gr.Row():
|
52 |
+
acc_out = gr.Textbox(label="Simulated Accuracy")
|
53 |
+
param_out = gr.Textbox(label="Estimated Parameters")
|
54 |
|
55 |
summary_out = gr.Textbox(label="Current Config Summary", lines=5)
|
56 |
|
57 |
+
gr.Markdown("""
|
58 |
+
## 🧬 Evolution History
|
59 |
+
""")
|
60 |
|
61 |
+
radar_img = gr.Image(label="Final Generation Trait Radar")
|
62 |
+
diagram_img = gr.Image(label="Transformer Architecture")
|
63 |
|
64 |
with gr.Row():
|
65 |
+
csv_dl = gr.File(label="Download CSV History")
|
66 |
+
json_dl = gr.File(label="Download JSON History")
|
67 |
|
68 |
evolve_btn.click(
|
69 |
+
fn=evolve_transformer,
|
70 |
+
inputs=[gens],
|
71 |
+
outputs=[acc_out, param_out, summary_out, radar_img, diagram_img, csv_dl, json_dl]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
)
|
73 |
|
74 |
+
if __name__ == "__main__":
|
75 |
+
demo.launch()
|