Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,107 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
-
import json
|
4 |
-
import csv
|
5 |
-
import io
|
6 |
from evo_transformer import EvoTransformer
|
7 |
from plot import plot_radar_chart
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
evo = EvoTransformer()
|
11 |
|
12 |
-
def evolve_model(
|
13 |
-
evo.reset() #
|
14 |
-
evo.
|
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 |
|
42 |
return (
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
radar_img,
|
47 |
-
None,
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
|
|
|
53 |
with gr.Blocks(title="🧬 EvoTransformer – Evolving Transformer Architectures") as demo:
|
54 |
gr.Markdown("## 🧬 EvoTransformer – Evolving Transformer Architectures\nSimulate trait mutation and adaptive architecture generation.")
|
55 |
|
56 |
with gr.Row():
|
57 |
-
|
58 |
evolve_btn = gr.Button("🧬 Evolve Architecture")
|
59 |
|
60 |
with gr.Row():
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
gr.Markdown("## 🧬 Evolution History")
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
history_boxes = [gr.Textbox(label=f"Gen {i+1} Config") for i in range(10)]
|
71 |
|
72 |
-
|
73 |
-
csv_out = gr.File(label="Download CSV History")
|
74 |
-
json_out = gr.File(label="Download JSON History")
|
75 |
|
76 |
evolve_btn.click(
|
77 |
-
|
78 |
-
inputs=[
|
79 |
outputs=[
|
80 |
-
|
81 |
radar_output, diagram_output,
|
82 |
-
*
|
83 |
-
|
84 |
]
|
85 |
)
|
86 |
|
87 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
from evo_transformer import EvoTransformer
|
3 |
from plot import plot_radar_chart
|
4 |
+
import tempfile
|
5 |
+
import json
|
6 |
+
import csv
|
7 |
+
from PIL import Image
|
8 |
|
9 |
+
# Initialize EvoTransformer
|
10 |
evo = EvoTransformer()
|
11 |
|
12 |
+
def evolve_model(generations):
|
13 |
+
evo.reset() # Reset state
|
14 |
+
evo.run_evolution(generations)
|
15 |
+
|
16 |
+
# Get final config
|
17 |
+
final_config = evo.history[-1]
|
18 |
+
summary = f"layers: {final_config['layers']}\nattention_heads: {final_config['attention_heads']}\n"
|
19 |
+
summary += f"ffn_dim: {final_config['ffn_dim']}\ndropout: {final_config['dropout']}\n"
|
20 |
+
summary += f"memory: {final_config['memory']}"
|
21 |
+
|
22 |
+
# Simulate results
|
23 |
+
accuracy = round(final_config['layers'] * 1.23 + final_config['dropout'] * 10, 2)
|
24 |
+
params = round(final_config['layers'] * final_config['ffn_dim'] * final_config['attention_heads'] / 1000, 2)
|
25 |
+
accuracy_str = f"{accuracy:.2f}%"
|
26 |
+
params_str = f"{params:.2f}M params"
|
27 |
+
|
28 |
+
# Radar chart
|
29 |
+
radar_img = plot_radar_chart(final_config)
|
30 |
+
|
31 |
+
# History logs
|
32 |
+
gen_configs = []
|
33 |
+
for i, cfg in enumerate(evo.history):
|
34 |
+
gen_configs.append(f"Gen {i+1} Config: {cfg}")
|
35 |
+
|
36 |
+
# Write CSV
|
37 |
+
csv_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode='w', newline='')
|
38 |
+
writer = csv.DictWriter(csv_file, fieldnames=final_config.keys())
|
39 |
+
writer.writeheader()
|
40 |
+
writer.writerows(evo.history)
|
41 |
+
csv_file.close()
|
42 |
+
|
43 |
+
# Write JSON
|
44 |
+
json_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode='w')
|
45 |
+
json.dump(evo.history, json_file)
|
46 |
+
json_file.close()
|
47 |
+
|
48 |
+
# Pad history to 10 generations
|
49 |
+
while len(gen_configs) < 10:
|
50 |
+
gen_configs.append("")
|
51 |
|
52 |
return (
|
53 |
+
accuracy_str, # 1
|
54 |
+
params_str, # 2
|
55 |
+
summary, # 3
|
56 |
+
radar_img, # 4
|
57 |
+
None, # 5 (diagram placeholder)
|
58 |
+
gen_configs[0], # 6
|
59 |
+
gen_configs[1], # 7
|
60 |
+
gen_configs[2], # 8
|
61 |
+
gen_configs[3], # 9
|
62 |
+
gen_configs[4], # 10
|
63 |
+
gen_configs[5], # 11
|
64 |
+
gen_configs[6], # 12
|
65 |
+
gen_configs[7], # 13
|
66 |
+
gen_configs[8], # 14
|
67 |
+
gen_configs[9], # 15
|
68 |
+
csv_file.name, # 16
|
69 |
+
json_file.name # 17
|
70 |
)
|
71 |
|
72 |
+
# Gradio UI
|
73 |
with gr.Blocks(title="🧬 EvoTransformer – Evolving Transformer Architectures") as demo:
|
74 |
gr.Markdown("## 🧬 EvoTransformer – Evolving Transformer Architectures\nSimulate trait mutation and adaptive architecture generation.")
|
75 |
|
76 |
with gr.Row():
|
77 |
+
generations_input = gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Number of Generations")
|
78 |
evolve_btn = gr.Button("🧬 Evolve Architecture")
|
79 |
|
80 |
with gr.Row():
|
81 |
+
accuracy_output = gr.Textbox(label="Simulated Accuracy")
|
82 |
+
params_output = gr.Textbox(label="Estimated Parameters")
|
83 |
+
summary_output = gr.Textbox(label="Current Config Summary")
|
84 |
|
85 |
gr.Markdown("## 🧬 Evolution History")
|
86 |
+
radar_output = gr.Image(label="Final Generation Trait Radar", type="pil")
|
87 |
+
diagram_output = gr.Image(label="Illustrative Transformer Structure") # Not used, just placeholder
|
88 |
|
89 |
+
with gr.Accordion("Downloadable Files", open=False):
|
90 |
+
csv_output = gr.File(label="Download CSV History")
|
91 |
+
json_output = gr.File(label="Download JSON History")
|
|
|
92 |
|
93 |
+
gen_outputs = [gr.Textbox(label=f"Gen {i+1} Config") for i in range(10)]
|
|
|
|
|
94 |
|
95 |
evolve_btn.click(
|
96 |
+
evolve_model,
|
97 |
+
inputs=[generations_input],
|
98 |
outputs=[
|
99 |
+
accuracy_output, params_output, summary_output,
|
100 |
radar_output, diagram_output,
|
101 |
+
*gen_outputs,
|
102 |
+
csv_output, json_output
|
103 |
]
|
104 |
)
|
105 |
|
106 |
+
if __name__ == "__main__":
|
107 |
+
demo.launch()
|