Spaces:
Running
Running
import os | |
# π‘οΈ Ensure model is saved before importing anything that loads it | |
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() | |
else: | |
print("β EvoTransformer already initialized.") | |
import gradio as gr | |
import random | |
from inference import generate_response | |
from logger import log_user_feedback | |
from dashboard import update_dashboard_plot | |
from watchdog import retrain_model | |
# π² Random examples | |
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": "Increase productivity at work", | |
"option1": "Multitask all day", | |
"option2": "Use Pomodoro technique" | |
}, | |
{ | |
"goal": "Lose weight safely", | |
"option1": "Skip meals", | |
"option2": "Exercise regularly and eat balanced meals" | |
}, | |
{ | |
"goal": "Ace an exam", | |
"option1": "Cram the night before", | |
"option2": "Study consistently for 2 weeks" | |
}, | |
] | |
def load_random_example(): | |
example = random.choice(examples) | |
return example["goal"], example["option1"], example["option2"] | |
# π§ Model suggestion UI | |
evo_output = gr.Textbox(label="π§ EvoTransformer Suggestion") | |
gpt_output = gr.Textbox(label="π¬ GPT-3.5 Suggestion") | |
feedback_output = gr.Textbox(visible=False) | |
def evo_chat(goal, sol1, sol2): | |
response = generate_response(goal, sol1, sol2) | |
evo = response.get("evo_suggestion", "Error") | |
gpt = response.get("gpt_suggestion", "Error") | |
return evo, gpt | |
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 β Compare Options and Learn") as demo: | |
gr.Markdown("## π§ EvoTransformer v2.1 β Compare Options and Learn") | |
with gr.Row(): | |
goal_input = gr.Textbox(label="Goal", placeholder="e.g. Escape from house on fire") | |
with gr.Row(): | |
option1_input = gr.Textbox(label="Option 1", placeholder="e.g. Exit house through main door") | |
option2_input = 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") | |
with gr.Row(): | |
evo_output.render() | |
gpt_output.render() | |
with gr.Row(): | |
winner_dropdown = gr.Radio(["Solution 1", "Solution 2"], label="Which was better?") | |
feedback_btn = gr.Button("β Log Feedback") | |
feedback_output.render() | |
compare_btn.click( | |
fn=evo_chat, | |
inputs=[goal_input, option1_input, option2_input], | |
outputs=[evo_output, gpt_output] | |
) | |
random_btn.click( | |
fn=load_random_example, | |
inputs=[], | |
outputs=[goal_input, option1_input, option2_input] | |
) | |
feedback_btn.click( | |
fn=handle_feedback, | |
inputs=[goal_input, option1_input, option2_input, winner_dropdown], | |
outputs=[feedback_output] | |
) | |
with gr.Row(): | |
gr.Markdown("### π Dashboard") | |
dashboard_plot = gr.Plot() | |
dashboard_plot = update_dashboard_plot() | |
with gr.Row(): | |
retrain_button = gr.Button("β»οΈ Retrain Evo") | |
retrain_status = gr.Textbox(label="Retrain Status") | |
retrain_button.click(fn=retrain_model, inputs=[], outputs=[retrain_status]) | |
if __name__ == "__main__": | |
demo.launch(share=True) | |