EvoPlatformV3 / app.py
HemanM's picture
Update app.py
4f2bf95 verified
raw
history blame
5.06 kB
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()