Spaces:
Running
Running
import matplotlib.pyplot as plt | |
import firebase_admin | |
from firebase_admin import credentials, firestore | |
import io | |
from PIL import Image | |
import os | |
import json | |
# Initialize Firebase if not already initialized | |
if not firebase_admin._apps: | |
cred = credentials.Certificate("firebase_key.json") | |
firebase_admin.initialize_app(cred) | |
db = firestore.client() | |
# β Feedback Summary Bar Chart | |
def update_dashboard_plot(): | |
logs_ref = db.collection("evo_feedback") | |
docs = logs_ref.stream() | |
count_1 = 0 | |
count_2 = 0 | |
for doc in docs: | |
data = doc.to_dict() | |
winner = data.get("winner", "") | |
if winner == "1": | |
count_1 += 1 | |
elif winner == "2": | |
count_2 += 1 | |
# Generate a bar chart | |
fig, ax = plt.subplots() | |
ax.bar(["Solution 1", "Solution 2"], [count_1, count_2], color=["blue", "green"]) | |
ax.set_ylabel("Votes") | |
ax.set_title("π³οΈ EvoTransformer Feedback Summary") | |
plt.tight_layout() | |
# Convert matplotlib figure to PIL Image | |
buf = io.BytesIO() | |
plt.savefig(buf, format="png") | |
buf.seek(0) | |
img = Image.open(buf) | |
return img | |
# β Accuracy vs Generation with Architecture Tooltips | |
def evolution_accuracy_plot(): | |
try: | |
log_path = "trained_model/evolution_log.json" | |
if not os.path.exists(log_path): | |
fig, ax = plt.subplots() | |
ax.text(0.5, 0.5, "No evolution log found", ha="center", va="center") | |
plt.tight_layout() | |
buf = io.BytesIO() | |
plt.savefig(buf, format="png") | |
buf.seek(0) | |
return Image.open(buf) | |
with open(log_path, "r") as f: | |
log_data = json.load(f) | |
if not log_data: | |
fig, ax = plt.subplots() | |
ax.text(0.5, 0.5, "Evolution log is empty", ha="center", va="center") | |
plt.tight_layout() | |
buf = io.BytesIO() | |
plt.savefig(buf, format="png") | |
buf.seek(0) | |
return Image.open(buf) | |
generations = list(range(1, len(log_data) + 1)) | |
accuracies = [round(entry.get("accuracy", 0), 4) for entry in log_data] | |
tooltips = [ | |
f"L: {entry.get('num_layers', '?')}, H: {entry.get('num_heads', '?')}, FFN: {entry.get('ffn_dim', '?')}, Mem: {entry.get('use_memory', '?')}" | |
for entry in log_data | |
] | |
fig, ax = plt.subplots(figsize=(6, 4)) | |
ax.plot(generations, accuracies, marker='o', label="Accuracy", color="purple") | |
for i, tooltip in enumerate(tooltips): | |
ax.annotate(tooltip, (generations[i], accuracies[i]), fontsize=7, xytext=(0, 6), textcoords='offset points') | |
ax.set_xlabel("Generation") | |
ax.set_ylabel("Accuracy") | |
ax.set_title("π EvoTransformer Evolution Accuracy") | |
ax.set_ylim([0, 1.05]) | |
ax.grid(True) | |
plt.tight_layout() | |
buf = io.BytesIO() | |
plt.savefig(buf, format="png") | |
buf.seek(0) | |
return Image.open(buf) | |
except Exception as e: | |
fig, ax = plt.subplots() | |
ax.text(0.5, 0.5, f"Error: {e}", ha="center", va="center") | |
plt.tight_layout() | |
buf = io.BytesIO() | |
plt.savefig(buf, format="png") | |
buf.seek(0) | |
return Image.open(buf) | |