import gradio as gr import os from inference import get_evo_response, get_gpt_response from logger import log_feedback import csv import subprocess # Helper to load Hall of Fame def load_hall_of_fame(): entries = [] if os.path.exists("feedback_log.csv"): with open("feedback_log.csv", newline='', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: try: score = float(row.get("evo_was_correct", "0") == "yes") if "πŸ‘" in row.get("feedback", "") or score > 0.85: entries.append(row) except: continue return entries[-10:][::-1] # last 10, reverse order def handle_query(question, option1, option2, context): options = [option1, option2] evo_answer, evo_reasoning, evo_score, evo_context = get_evo_response(question, options, context) gpt_answer = get_gpt_response(question, context) return ( f"Answer: {evo_answer} (Confidence: {evo_score:.2f})\n\nReasoning: {evo_reasoning}\n\nContext used: {evo_context[:400]}...", gpt_answer, f"{question} | {context} | {evo_answer}" ) def handle_feedback(feedback_text, question, option1, option2, context, evo_output): evo_was_correct = "πŸ‘" in feedback_text log_feedback(question, option1, option2, context, evo_output, evo_was_correct) return "βœ… Feedback logged and Evo will improve." def trigger_retrain(): try: subprocess.run(["python", "retrain_from_feedback.py"], check=True) return "πŸ” Evo retraining completed." except subprocess.CalledProcessError: return "❌ Retraining failed. Check logs." def render_hof(): entries = load_hall_of_fame() if not entries: return "No Hall of Fame entries yet. Submit feedback!" result = "\n\n".join( [ f"πŸ† **Q:** {e['question']}\n**A:** {e['evo_output']}\n**Feedback:** {e.get('feedback', 'N/A')}\n**Context:** {e['context'][:200]}..." for e in entries ] ) return result description = """ # 🧠 EvoRAG – Adaptive Reasoning AI **What is Evo?** EvoTransformer is a lightweight, evolving neural network with ~28M parameters. It learns from feedback, adapts over time, and reasons using both web and context data. **Why Evo?** βœ… Evolves from human input βœ… Architecturally updatable βœ… Transparent and fine-tunable βœ… Efficient on modest hardware **Hardware**: Trained on Google Colab CPU/GPU **Token limit**: 128 **Benchmark**: PIQA, HellaSwag, ARC **Version**: Evo v2.2 (Memory + Web Retrieval + Feedback Learning) """ with gr.Blocks(title="EvoRAG") as demo: gr.Markdown(description) with gr.Row(): question = gr.Textbox(label="πŸ“ Ask anything", placeholder="e.g., What’s the best way to escape a house fire?") with gr.Row(): option1 = gr.Textbox(label="Option A", placeholder="e.g., Run outside") option2 = gr.Textbox(label="Option B", placeholder="e.g., Hide under bed") context = gr.Textbox(label="πŸ“‚ Optional Context", placeholder="Paste any extra background info here", lines=3) submit_btn = gr.Button("πŸ” Run Comparison") with gr.Row(): evo_output = gr.Textbox(label="🧠 EvoRAG's Reasoned Answer", lines=6) gpt_output = gr.Textbox(label="πŸ€– GPT-3.5's Suggestion", lines=6) feedback = gr.Radio(["πŸ‘ Evo was correct. Retrain from this.", "πŸ‘Ž Evo was wrong. Don't retrain."], label="Was Evo’s answer useful?", value=None) submit_feedback = gr.Button("πŸ“¬ Submit Feedback") feedback_status = gr.Textbox(label="Feedback Status", interactive=False) retrain_button = gr.Button("πŸ”„ Retrain Evo Now") retrain_status = gr.Textbox(label="Retraining Status", interactive=False) with gr.Accordion("πŸ† Evo Hall of Fame (Top Reasoning Entries)", open=False): hof_display = gr.Markdown(render_hof()) submit_btn.click(fn=handle_query, inputs=[question, option1, option2, context], outputs=[evo_output, gpt_output, feedback_status]) submit_feedback.click( fn=lambda fb, q, o1, o2, ctx, eo: handle_feedback(fb, q, o1, o2, ctx, eo), inputs=[feedback, question, option1, option2, context, feedback_status], outputs=[feedback_status] ) retrain_button.click(fn=trigger_retrain, inputs=[], outputs=[retrain_status]) demo.launch(server_name="0.0.0.0", server_port=7860, share=True)