HemanM's picture
Update app.py
0be77ef verified
raw
history blame
2.8 kB
# app.py
import gradio as gr
import json
import csv
import io
from evo_transformer import EvoTransformer
from plot import plot_radar_chart
# Global instance
evo = EvoTransformer()
def evolve_model(num_generations):
evo.reset() # reset before evolution
evo.evolve(num_generations)
final_config = evo.config
history = evo.get_history()
evaluation = evo.evaluate()
# Radar Chart
try:
radar_img = plot_radar_chart(final_config)
except Exception as e:
print("Radar chart error:", e)
radar_img = None
# Prepare history display
history_cards = []
for i, h in enumerate(history):
text = f"Gen {i + 1} Config: {h}"
history_cards.append(text)
# Prepare CSV and JSON
csv_file = io.StringIO()
csv_writer = csv.DictWriter(csv_file, fieldnames=history[0].keys())
csv_writer.writeheader()
csv_writer.writerows(history)
csv_bytes = io.BytesIO(csv_file.getvalue().encode())
json_file = io.StringIO(json.dumps(history, indent=2))
json_bytes = io.BytesIO(json_file.getvalue().encode())
return (
f"{evaluation['accuracy']:.2f}%",
f"{evaluation['params']:.2f}M params",
str(final_config),
radar_img,
None, # placeholder for future diagram
*history_cards,
("evo_history.csv", csv_bytes),
("evo_history.json", json_bytes)
)
with gr.Blocks(title="🧬 EvoTransformer – Evolving Transformer Architectures") as demo:
gr.Markdown("## 🧬 EvoTransformer – Evolving Transformer Architectures\nSimulate trait mutation and adaptive architecture generation.")
with gr.Row():
num_generations = gr.Slider(minimum=1, maximum=10, value=3, label="Number of Generations", step=1)
evolve_btn = gr.Button("🧬 Evolve Architecture")
with gr.Row():
accuracy_text = gr.Textbox(label="Simulated Accuracy")
param_text = gr.Textbox(label="Estimated Parameters")
config_text = gr.Textbox(label="Current Config Summary")
gr.Markdown("## 🧬 Evolution History")
radar_output = gr.Image(label="Final Generation Trait Radar", type="pil", interactive=False)
diagram_output = gr.Image(label="Illustrative Transformer Structure", visible=False)
history_boxes = [gr.Textbox(label=f"Gen {i+1} Config") for i in range(10)]
with gr.Accordion("Downloadable Files", open=True):
csv_out = gr.File(label="Download CSV History")
json_out = gr.File(label="Download JSON History")
evolve_btn.click(
fn=evolve_model,
inputs=[num_generations],
outputs=[
accuracy_text, param_text, config_text,
radar_output, diagram_output,
*history_boxes,
csv_out, json_out
]
)
demo.launch()