Spaces:
Sleeping
Sleeping
import gradio as gr | |
from inference import evo_chat_predict, get_model_config, get_gpt_response, load_model | |
import pandas as pd | |
import csv | |
import os | |
from datetime import datetime | |
from retrain_from_feedback import train_evo | |
feedback_log = [] | |
with gr.Blocks(theme=gr.themes.Base(), css="body { background-color: #0f0f0f; color: #f5f5f5; }") as demo: | |
with gr.Column(): | |
gr.HTML(""" | |
<div style="padding: 10px; border-radius: 12px; background: #1f1f2e; color: #fff; font-size: 16px; margin-bottom: 12px;"> | |
<b>Why Evo?</b> π Evo is not just another AI. It evolves. It learns from you. It adapts its architecture live based on feedback. No retraining labs, no frozen weights. This is <u>live reasoning meets evolution</u>. <span style="color:#88ffcc">Built to outperform, built to survive.</span> | |
</div> | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
query = gr.Textbox(label="π§ Your Question", placeholder="e.g. What should you do if thereβs a fire?") | |
option1 = gr.Textbox(label="β Option 1", placeholder="Enter the first option") | |
option2 = gr.Textbox(label="β Option 2", placeholder="Enter the second option") | |
feedback = gr.Radio(["Evo", "GPT"], label="π§ Who was better?", info="Optional β fuels evolution", interactive=True) | |
evo_btn = gr.Button("β‘ Ask Evo", elem_id="evo-btn") | |
retrain_btn = gr.Button("π Retrain Evo", elem_id="retrain-btn") | |
clear_btn = gr.Button("π§Ή Clear") | |
export_btn = gr.Button("π€ Export Feedback CSV") | |
with gr.Column(): | |
evo_stats = gr.Textbox(label="π Evo Stats", interactive=False) | |
evo_box = gr.Textbox(label="π§ Evo", interactive=False) | |
gpt_box = gr.Textbox(label="π€ GPT-3.5", interactive=False) | |
status_box = gr.Textbox(label="π΅ Status", interactive=False) | |
convo = gr.Dataframe( | |
headers=["Question", "Option 1", "Option 2", "Answer", "Confidence", "Reasoning", "Context"], | |
interactive=False, wrap=True, label="π Conversation History" | |
) | |
# π Ask Evo | |
def ask_evo(q, opt1, opt2, hist, selected): | |
result = evo_chat_predict(hist, q, [opt1, opt2]) | |
evo_text = f"Answer: {result['answer']} (Confidence: {result['confidence']})\n\nReasoning: {result['reasoning']}" | |
gpt_text = get_gpt_response(q) | |
stats = get_model_config() | |
stats_text = f"Layers: {stats['num_layers']} | Heads: {stats['num_heads']} | FFN: {stats['ffn_dim']} | Memory: {stats['memory_enabled']} | Phase: {stats['phase']} | Accuracy: {stats['accuracy']}" | |
# Update history | |
new_row = [q, opt1, opt2, result["answer"], result["confidence"], result["reasoning"], result["context_used"]] | |
new_row_df = pd.DataFrame([new_row], columns=hist.columns) | |
updated_df = new_row_df if hist.empty else pd.concat([hist, new_row_df], ignore_index=True) | |
# Log feedback | |
if selected in ["Evo", "GPT"]: | |
feedback_log.append(new_row + [selected, "yes" if selected == "Evo" else "no"]) | |
return evo_text, gpt_text, stats_text, updated_df | |
# π Retrain Evo | |
def retrain_evo(): | |
if not feedback_log: | |
return "β οΈ No feedback data to retrain from." | |
with open("feedback_log.csv", "w", newline="") as f: | |
writer = csv.writer(f) | |
writer.writerow(["question", "option1", "option2", "answer", "confidence", "reasoning", "context", "user_preference", "evo_was_correct"]) | |
for row in feedback_log: | |
writer.writerow(row) | |
train_evo() | |
load_model() | |
return f"β Evo retrained on {len(feedback_log)} entries and reloaded." | |
# π§Ή Clear UI | |
def clear_fields(): | |
feedback_log.clear() | |
return "", "", "", "", "", pd.DataFrame(columns=["Question", "Option 1", "Option 2", "Answer", "Confidence", "Reasoning", "Context"]) | |
# π€ Export feedback | |
def log_feedback_to_csv(): | |
if feedback_log: | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
filepath = f"feedback_{timestamp}.csv" | |
with open(filepath, "w", newline="") as f: | |
writer = csv.writer(f) | |
writer.writerow(["Question", "Option 1", "Option 2", "Answer", "Confidence", "Reasoning", "Context", "User Pref", "Evo Correct"]) | |
writer.writerows(feedback_log) | |
return f"β Feedback exported to {filepath}" | |
else: | |
return "β οΈ No feedback to export." | |
# π Event bindings | |
evo_btn.click(fn=ask_evo, inputs=[query, option1, option2, convo, feedback], outputs=[evo_box, gpt_box, evo_stats, convo]) | |
retrain_btn.click(fn=retrain_evo, inputs=[], outputs=[status_box]) | |
clear_btn.click(fn=clear_fields, inputs=[], outputs=[query, option1, option2, evo_box, gpt_box, convo]) | |
export_btn.click(fn=log_feedback_to_csv, inputs=[], outputs=[status_box]) | |
if __name__ == "__main__": | |
demo.launch() | |