Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from evo_transformer import EvoTransformer
|
| 3 |
from plot import plot_radar_chart
|
| 4 |
-
import
|
| 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()
|
| 14 |
-
evo.
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
summary
|
| 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(
|
| 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 |
-
#
|
| 44 |
-
|
| 45 |
-
json.
|
| 46 |
-
json_file.close()
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
gen_configs.append("")
|
| 51 |
|
| 52 |
return (
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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 |
-
|
| 73 |
-
|
| 74 |
-
gr.Markdown("
|
| 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.
|
| 86 |
-
|
| 87 |
-
diagram_output = gr.Image(label="Illustrative Transformer Structure") # Not used, just placeholder
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
evolve_model,
|
| 97 |
-
inputs=[
|
| 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 |
-
|
| 107 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from evo_transformer import EvoTransformer
|
| 3 |
from plot import plot_radar_chart
|
| 4 |
+
import io
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
|
|
|
| 7 |
evo = EvoTransformer()
|
| 8 |
|
| 9 |
def evolve_model(generations):
|
| 10 |
+
evo.reset()
|
| 11 |
+
evo.evolve(generations)
|
| 12 |
|
| 13 |
+
best_config = evo.get_best_config()
|
| 14 |
+
accuracy = f"{best_config['accuracy']:.2f}%"
|
| 15 |
+
params = f"{best_config['parameters']:.2f}M params"
|
| 16 |
+
summary = str(best_config['traits'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Radar chart
|
| 19 |
+
radar_img = plot_radar_chart(best_config['traits'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Files
|
| 22 |
+
csv_file = ("evo_history.csv", io.BytesIO(evo.export_csv().encode()))
|
| 23 |
+
json_file = ("evo_history.json", io.BytesIO(evo.export_json().encode()))
|
|
|
|
| 24 |
|
| 25 |
+
# History
|
| 26 |
+
history_text = [f"Gen {i+1} Config: {c['traits']}" for i, c in enumerate(evo.history)]
|
|
|
|
| 27 |
|
| 28 |
return (
|
| 29 |
+
accuracy, params, summary,
|
| 30 |
+
radar_img,
|
| 31 |
+
*history_text,
|
| 32 |
+
csv_file, json_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
+
with gr.Blocks(title="EvoTransformer – Evolving Transformer Architectures") as demo:
|
| 36 |
+
gr.Markdown("🧬 **EvoTransformer – Evolving Transformer Architectures**")
|
| 37 |
+
gr.Markdown("Simulate trait mutation and adaptive architecture generation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
generations = gr.Slider(minimum=1, maximum=20, step=1, label="Number of Generations", value=5)
|
| 40 |
+
run_btn = gr.Button("🧬 Evolve Architecture")
|
|
|
|
| 41 |
|
| 42 |
+
acc = gr.Textbox(label="Simulated Accuracy")
|
| 43 |
+
params = gr.Textbox(label="Estimated Parameters")
|
| 44 |
+
summary = gr.Textbox(label="Current Config Summary")
|
| 45 |
+
radar = gr.Image(label="Final Generation Trait Radar")
|
| 46 |
|
| 47 |
+
history_outputs = [gr.Textbox(label=f"Gen {i+1} Config") for i in range(10)]
|
| 48 |
+
csv_download = gr.File(label="Download CSV History")
|
| 49 |
+
json_download = gr.File(label="Download JSON History")
|
| 50 |
|
| 51 |
+
run_btn.click(
|
| 52 |
+
fn=evolve_model,
|
| 53 |
+
inputs=[generations],
|
| 54 |
+
outputs=[acc, params, summary, radar] + history_outputs + [csv_download, json_download]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
+
demo.launch()
|
|
|