HemanM commited on
Commit
947ad46
·
verified ·
1 Parent(s): a41d201

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -59
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
- # Global instance
10
  evo = EvoTransformer()
11
 
12
- def evolve_model(num_generations):
13
- evo.reset() # reset before evolution
14
- evo.evolve(num_generations)
15
- final_config = evo.config
16
- history = evo.get_history()
17
- evaluation = evo.evaluate()
18
-
19
- # Radar Chart
20
- try:
21
- radar_img = plot_radar_chart(final_config)
22
- except Exception as e:
23
- print("Radar chart error:", e)
24
- radar_img = None
25
-
26
- # Prepare history display
27
- history_cards = []
28
- for i, h in enumerate(history):
29
- text = f"Gen {i + 1} Config: {h}"
30
- history_cards.append(text)
31
-
32
- # Prepare CSV and JSON
33
- csv_file = io.StringIO()
34
- csv_writer = csv.DictWriter(csv_file, fieldnames=history[0].keys())
35
- csv_writer.writeheader()
36
- csv_writer.writerows(history)
37
- csv_bytes = io.BytesIO(csv_file.getvalue().encode())
38
-
39
- json_file = io.StringIO(json.dumps(history, indent=2))
40
- json_bytes = io.BytesIO(json_file.getvalue().encode())
 
 
 
 
 
 
 
 
 
 
41
 
42
  return (
43
- f"{evaluation['accuracy']:.2f}%",
44
- f"{evaluation['params']:.2f}M params",
45
- str(final_config),
46
- radar_img,
47
- None, # placeholder for future diagram
48
- *history_cards,
49
- ("evo_history.csv", csv_bytes),
50
- ("evo_history.json", json_bytes)
 
 
 
 
 
 
 
 
 
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
- num_generations = gr.Slider(minimum=1, maximum=10, value=3, label="Number of Generations", step=1)
58
  evolve_btn = gr.Button("🧬 Evolve Architecture")
59
 
60
  with gr.Row():
61
- accuracy_text = gr.Textbox(label="Simulated Accuracy")
62
- param_text = gr.Textbox(label="Estimated Parameters")
63
- config_text = gr.Textbox(label="Current Config Summary")
64
 
65
  gr.Markdown("## 🧬 Evolution History")
 
 
66
 
67
- radar_output = gr.Image(label="Final Generation Trait Radar", type="pil", interactive=False)
68
- diagram_output = gr.Image(label="Illustrative Transformer Structure", visible=False)
69
-
70
- history_boxes = [gr.Textbox(label=f"Gen {i+1} Config") for i in range(10)]
71
 
72
- with gr.Accordion("Downloadable Files", open=True):
73
- csv_out = gr.File(label="Download CSV History")
74
- json_out = gr.File(label="Download JSON History")
75
 
76
  evolve_btn.click(
77
- fn=evolve_model,
78
- inputs=[num_generations],
79
  outputs=[
80
- accuracy_text, param_text, config_text,
81
  radar_output, diagram_output,
82
- *history_boxes,
83
- csv_out, json_out
84
  ]
85
  )
86
 
87
- demo.launch()
 
 
 
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()