File size: 4,512 Bytes
7dd02a3
cb9a19a
b512755
cb9a19a
 
 
 
 
b7194b1
 
 
 
696d4a2
 
 
 
 
 
b7194b1
cf8af88
 
 
 
 
b512755
 
 
 
 
 
7dd02a3
696d4a2
 
 
 
 
 
 
b7194b1
2fce029
 
b512755
b7194b1
 
 
 
 
 
c615588
b512755
 
488dba6
eed17f4
 
 
 
 
 
488dba6
b512755
fc4c293
b512755
82af73f
846bcad
b512755
 
 
fc4c293
 
c615588
b512755
f8c1a4c
b512755
 
f8c1a4c
c615588
82af73f
 
488dba6
b512755
 
 
488dba6
d9fdd26
b512755
 
696d4a2
b512755
 
 
6a4410c
b512755
909e881
b512755
 
 
 
b279d56
b512755
 
 
 
b279d56
b512755
 
 
 
 
df8494a
b512755
3095d43
eed17f4
b512755
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os

# βš™οΈ Ensure model is saved before import
if not os.path.exists("trained_model/config.json"):
    print("βš™οΈ No model found. Initializing and saving EvoTransformer...")
    from init_model import initialize_and_save_model
    initialize_and_save_model()

import gradio as gr
import random
from inference import generate_response
from logger import log_user_feedback
from dashboard import (
    update_dashboard_plot,
    evolution_accuracy_plot,
    leaderboard_plot,
    get_vote_counts,
)
from watchdog import retrain_model
from init_model import load_model

model = load_model()

def get_architecture_summary(model):
    return "\n".join([
        f"Layers: {getattr(model, 'num_layers', 'N/A')}",
        f"Attention Heads: {getattr(model, 'num_heads', 'N/A')}",
        f"FFN Dim: {getattr(model, 'ffn_dim', 'N/A')}",
        f"Memory Enabled: {getattr(model, 'use_memory', 'N/A')}",
    ])

def update_vote_counter():
    votes = get_vote_counts()
    s1 = votes.get("1", 0)
    s2 = votes.get("2", 0)
    total = s1 + s2
    return f"πŸ—³οΈ **Votes so far** β€” Solution 1: {s1} | Solution 2: {s2} | Total: {total}"

examples = [
    {"goal": "Escape from a burning house", "option1": "Run out through the front door", "option2": "Hide in the bathroom"},
    {"goal": "Improve sleep quality", "option1": "Use phone in bed", "option2": "Turn off screens 1 hour before bed"},
    {"goal": "Lose weight", "option1": "Skip meals", "option2": "Exercise and eat healthy"},
]

def load_random_example():
    example = random.choice(examples)
    return example["goal"], example["option1"], example["option2"]

def evo_chat(goal, sol1, sol2):
    result = generate_response(goal, sol1, sol2)
    return result["evo_suggestion"], result["gpt_suggestion"]

def handle_feedback(goal, sol1, sol2, winner):
    try:
        log_user_feedback(goal, sol1, sol2, winner)
        return "βœ… Feedback logged. Thank you!"
    except Exception as e:
        return f"❌ Failed to log: {e}"

with gr.Blocks(title="EvoTransformer v2.1") as demo:
    gr.Markdown("""
# EvoTransformer v2.1  
*Built Different. Learns Live.*

🧬 Learns from your input  
🧠 Compares reasoning like GPT-3.5  
πŸ“Š Tracks accuracy & evolves
""")

    with gr.Row():
        goal = gr.Textbox(label="Goal", placeholder="e.g. Escape from house on fire")
    with gr.Row():
        sol1 = gr.Textbox(label="Option 1", placeholder="e.g. Exit through door")
        sol2 = gr.Textbox(label="Option 2", placeholder="e.g. Hide under bed")

    with gr.Row():
        compare_btn = gr.Button("πŸ” Compare")
        random_btn = gr.Button("🎲 Load Random Example")

    evo_box = gr.Textbox(label="🧠 Evo Suggestion")
    gpt_box = gr.Textbox(label="πŸ’¬ GPT-3.5 Suggestion")
    feedback_msg = gr.Textbox(visible=False)

    with gr.Row():
        vote = gr.Radio(["Solution 1", "Solution 2"], label="Which was better?")
        log_btn = gr.Button("βœ… Log Feedback")

    compare_btn.click(fn=evo_chat, inputs=[goal, sol1, sol2], outputs=[evo_box, gpt_box])
    random_btn.click(fn=load_random_example, outputs=[goal, sol1, sol2])
    log_btn.click(fn=handle_feedback, inputs=[goal, sol1, sol2, vote], outputs=[feedback_msg])

    # === Visual Stats ===
    vote_counter = gr.Markdown(update_vote_counter())
    arch_summary = gr.Textbox(value=get_architecture_summary(model), label="πŸ“ Model Configuration", lines=4, interactive=False)
    evo_plot = gr.Image(value=evolution_accuracy_plot(), label="πŸ“ˆ Accuracy Over Time")
    dash_img = gr.Image(value=update_dashboard_plot(), label="πŸ“Š User Feedback Distribution")
    leaderboard = gr.Image(value=leaderboard_plot(), label="πŸ† Hall of Fame")

    retrain_note = gr.Markdown("Click to retrain Evo using your latest feedback.")
    retrain_status = gr.Textbox(label="Retrain Status", value="No retrain triggered yet.")
    retrain_btn = gr.Button("♻️ Retrain Evo", variant="primary")
    retrain_btn.click(fn=retrain_model, outputs=[arch_summary, evo_plot, retrain_status])

    with gr.Accordion("🧬 EvoTransformer Architecture", open=False): arch_summary
    with gr.Accordion("πŸ“ˆ Accuracy Plot", open=False): evo_plot
    with gr.Accordion("πŸ“Š Vote Summary", open=False): dash_img
    with gr.Accordion("πŸ… Top Goals", open=False): leaderboard
    with gr.Accordion("πŸ” Retrain Log", open=False): retrain_status

    gr.Markdown("Built with ❀️ using Gradio · Contact us for API access")

if __name__ == "__main__":
    demo.launch(share=True)