Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,240 +1,63 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
from inference import get_evo_response, get_gpt_response
|
| 4 |
-
from logger import log_feedback
|
| 5 |
-
import csv
|
| 6 |
import subprocess
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
entries = []
|
| 11 |
-
if os.path.exists("feedback_log.csv"):
|
| 12 |
-
with open("feedback_log.csv", newline='', encoding='utf-8') as f:
|
| 13 |
-
reader = csv.DictReader(f)
|
| 14 |
-
for row in reader:
|
| 15 |
-
try:
|
| 16 |
-
score = float(row.get("evo_was_correct", "0") == "yes")
|
| 17 |
-
if "π" in row.get("feedback", "") or score > 0.85:
|
| 18 |
-
entries.append(row)
|
| 19 |
-
except:
|
| 20 |
-
continue
|
| 21 |
-
return entries[-10:][::-1] # last 10, reverse order
|
| 22 |
-
|
| 23 |
-
def handle_query(question, option1, option2, context):
|
| 24 |
-
options = [option1, option2]
|
| 25 |
-
evo_answer, evo_reasoning, evo_score, evo_context = get_evo_response(question, options, context)
|
| 26 |
-
gpt_answer = get_gpt_response(question, context)
|
| 27 |
-
return (
|
| 28 |
-
f"Answer: {evo_answer} (Confidence: {evo_score:.2f})\n\nReasoning: {evo_reasoning}\n\nContext used: {evo_context[:400]}...",
|
| 29 |
-
gpt_answer,
|
| 30 |
-
f"{question} | {context} | {evo_answer}"
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
def handle_feedback(feedback_text, question, option1, option2, context, evo_output):
|
| 34 |
-
evo_was_correct = "π" in feedback_text
|
| 35 |
-
log_feedback(question, option1, option2, context, evo_output, evo_was_correct)
|
| 36 |
-
return "β
Feedback logged and Evo will improve."
|
| 37 |
-
|
| 38 |
-
def trigger_retrain():
|
| 39 |
-
try:
|
| 40 |
-
subprocess.run(["python", "retrain_from_feedback.py"], check=True)
|
| 41 |
-
return "π Evo retraining completed."
|
| 42 |
-
except subprocess.CalledProcessError:
|
| 43 |
-
return "β Retraining failed. Check logs."
|
| 44 |
-
|
| 45 |
-
def render_hof():
|
| 46 |
-
entries = load_hall_of_fame()
|
| 47 |
-
if not entries:
|
| 48 |
-
return "No Hall of Fame entries yet. Submit feedback!"
|
| 49 |
-
result = "\n\n".join(
|
| 50 |
-
[
|
| 51 |
-
f"π **Q:** {e['question']}\n**A:** {e['evo_output']}\n**Feedback:** {e.get('feedback', 'N/A')}\n**Context:** {e['context'][:200]}..."
|
| 52 |
-
for e in entries
|
| 53 |
-
]
|
| 54 |
-
)
|
| 55 |
-
return result
|
| 56 |
-
|
| 57 |
-
description = """
|
| 58 |
-
# π§ EvoRAG β Adaptive Reasoning AI
|
| 59 |
-
|
| 60 |
-
**What is Evo?**
|
| 61 |
-
EvoTransformer is a lightweight, evolving neural network with ~28M parameters.
|
| 62 |
-
It learns from feedback, adapts over time, and reasons using both web and context data.
|
| 63 |
-
|
| 64 |
-
**Why Evo?**
|
| 65 |
-
β
Evolves from human input
|
| 66 |
-
β
Architecturally updatable
|
| 67 |
-
β
Transparent and fine-tunable
|
| 68 |
-
β
Efficient on modest hardware
|
| 69 |
-
|
| 70 |
-
**Hardware**: Trained on Google Colab CPU/GPU
|
| 71 |
-
**Token limit**: 128
|
| 72 |
-
**Benchmark**: PIQA, HellaSwag, ARC
|
| 73 |
-
**Version**: Evo v2.2 (Memory + Web Retrieval + Feedback Learning)
|
| 74 |
-
"""
|
| 75 |
-
|
| 76 |
-
with gr.Blocks(title="EvoRAG") as demo:
|
| 77 |
-
gr.Markdown(description)
|
| 78 |
-
with gr.Row():
|
| 79 |
-
question = gr.Textbox(label="π Ask anything", placeholder="e.g., Whatβs the best way to escape a house fire?")
|
| 80 |
-
with gr.Row():
|
| 81 |
-
option1 = gr.Textbox(label="Option A", placeholder="e.g., Run outside")
|
| 82 |
-
option2 = gr.Textbox(label="Option B", placeholder="e.g., Hide under bed")
|
| 83 |
-
context = gr.Textbox(label="π Optional Context", placeholder="Paste any extra background info here", lines=3)
|
| 84 |
-
|
| 85 |
-
submit_btn = gr.Button("π Run Comparison")
|
| 86 |
-
with gr.Row():
|
| 87 |
-
evo_output = gr.Textbox(label="π§ EvoRAG's Reasoned Answer", lines=6)
|
| 88 |
-
gpt_output = gr.Textbox(label="π€ GPT-3.5's Suggestion", lines=6)
|
| 89 |
-
|
| 90 |
-
feedback = gr.Radio(["π Evo was correct. Retrain from this.", "π Evo was wrong. Don't retrain."], label="Was Evoβs answer useful?", value=None)
|
| 91 |
-
submit_feedback = gr.Button("π¬ Submit Feedback")
|
| 92 |
-
feedback_status = gr.Textbox(label="Feedback Status", interactive=False)
|
| 93 |
-
|
| 94 |
-
retrain_button = gr.Button("π Retrain Evo Now")
|
| 95 |
-
retrain_status = gr.Textbox(label="Retraining Status", interactive=False)
|
| 96 |
-
|
| 97 |
-
with gr.Accordion("π Evo Hall of Fame (Top Reasoning Entries)", open=False):
|
| 98 |
-
hof_display = gr.Markdown(render_hof())
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
inputs=[feedback, question, option1, option2, context, feedback_status],
|
| 104 |
-
outputs=[feedback_status]
|
| 105 |
-
)
|
| 106 |
-
retrain_button.click(fn=trigger_retrain, inputs=[], outputs=[retrain_status])
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
import time
|
| 113 |
-
import os
|
| 114 |
-
from inference import load_model_and_tokenizer, infer
|
| 115 |
-
from logger import log_feedback
|
| 116 |
-
from retrain_from_feedback import train_evo
|
| 117 |
-
from datetime import datetime
|
| 118 |
-
from inference import get_gpt_response
|
| 119 |
-
|
| 120 |
-
# Globals
|
| 121 |
-
model, tokenizer = load_model_and_tokenizer()
|
| 122 |
-
|
| 123 |
-
# Helper to reload model
|
| 124 |
-
def reload_model():
|
| 125 |
-
global model, tokenizer
|
| 126 |
-
model, tokenizer = load_model_and_tokenizer()
|
| 127 |
|
| 128 |
-
#
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
return f.read().strip()
|
| 133 |
-
return "Never"
|
| 134 |
|
| 135 |
-
|
| 136 |
-
def get_model_summary():
|
| 137 |
-
num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 138 |
-
summary = f"""
|
| 139 |
-
β’ π’ Parameters: {num_params:,}
|
| 140 |
-
β’ π§± Layers: 6 TransformerEncoder
|
| 141 |
-
β’ π― Attention Heads: 8
|
| 142 |
-
β’ π§ FFN Dim: 1024
|
| 143 |
-
⒠𧬠Memory Module: Enabled
|
| 144 |
-
β’ βοΈ Pooling: AdaptiveAvgPool1d
|
| 145 |
-
β’ π§Ύ Classifier: Linear(512 β 1)
|
| 146 |
-
"""
|
| 147 |
-
return summary.strip()
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
def
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
return
|
| 154 |
-
f"Answer: {evo_ans} (Confidence: {evo_score:.2f})\n\nReasoning: {evo_reason}\n\nContext used: {evo_ctx}",
|
| 155 |
-
gpt_ans
|
| 156 |
-
)
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
def
|
| 160 |
-
evo_was_correct = feedback_text.strip().lower() == "π evo was correct. retrain from this."
|
| 161 |
-
log_feedback(question, option1, option2, context, evo_output, evo_was_correct)
|
| 162 |
-
return "β
Feedback logged and Evo will improve."
|
| 163 |
-
|
| 164 |
-
# Manual retrain
|
| 165 |
-
def manual_retrain():
|
| 166 |
try:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
ts = datetime.utcnow().strftime("%Y-%m-%d %H:%M GMT")
|
| 170 |
-
with open("last_updated.txt", "w") as f:
|
| 171 |
-
f.write(ts)
|
| 172 |
-
return f"β
Evo successfully evolved! Reloaded at {ts}"
|
| 173 |
except Exception as e:
|
| 174 |
return f"β Retraining failed: {str(e)}"
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
EvoTransformer is a lightweight, evolving neural network with ~28M parameters.
|
| 181 |
-
It learns from feedback, adapts over time, and reasons using both web and context data.
|
| 182 |
-
|
| 183 |
-
**Why Evo?**
|
| 184 |
-
β
Evolves from human input
|
| 185 |
-
β
Architecturally updatable
|
| 186 |
-
β
Transparent and fine-tunable
|
| 187 |
-
β
Efficient on modest hardware
|
| 188 |
-
|
| 189 |
-
**Hardware:** Trained on Google Colab CPU/GPU
|
| 190 |
-
**Token limit:** 128
|
| 191 |
-
**Benchmark:** PIQA, HellaSwag, ARC
|
| 192 |
-
**Version:** Evo v2.2 (Memory + Web Retrieval + Feedback Learning)
|
| 193 |
-
**π Last Evolution:** {get_last_update()}
|
| 194 |
-
""")
|
| 195 |
-
|
| 196 |
-
gr.Markdown(f"""
|
| 197 |
-
## π§ EvoTransformer Architecture Summary
|
| 198 |
-
{get_model_summary()}
|
| 199 |
-
""")
|
| 200 |
-
|
| 201 |
-
with gr.Row():
|
| 202 |
-
question = gr.Textbox(label="Ask anything", placeholder="e.g. Whatβs the best way to boil water?")
|
| 203 |
-
|
| 204 |
-
with gr.Row():
|
| 205 |
-
option1 = gr.Textbox(label="Option A")
|
| 206 |
-
option2 = gr.Textbox(label="Option B")
|
| 207 |
-
|
| 208 |
-
context = gr.Textbox(label="π Optional Context", lines=2, placeholder="Paste any extra background info here")
|
| 209 |
-
|
| 210 |
-
run_btn = gr.Button("π Run Comparison")
|
| 211 |
-
|
| 212 |
-
with gr.Row():
|
| 213 |
-
evo_out = gr.Textbox(label="π§ EvoRAG's Reasoned Answer")
|
| 214 |
-
gpt_out = gr.Textbox(label="π€ GPT-3.5's Suggestion")
|
| 215 |
-
|
| 216 |
-
with gr.Row():
|
| 217 |
-
feedback_dropdown = gr.Dropdown([
|
| 218 |
-
"π Evo was correct. Retrain from this.",
|
| 219 |
-
"π Evo was wrong. Don't retrain."
|
| 220 |
-
], label="Was Evoβs answer useful?")
|
| 221 |
-
submit_btn = gr.Button("π¬ Submit Feedback")
|
| 222 |
-
|
| 223 |
-
feedback_status = gr.Textbox(label="Feedback Status")
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
""")
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
retrain_btn.click(fn=manual_retrain, outputs=retrain_status)
|
| 239 |
|
| 240 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from inference import evo_chat_predict
|
|
|
|
|
|
|
|
|
|
| 3 |
import subprocess
|
| 4 |
|
| 5 |
+
# Global chat history buffer
|
| 6 |
+
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# π§ Main chat handler
|
| 9 |
+
def chat_fn(user_input, option1, option2):
|
| 10 |
+
global chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Validate input
|
| 13 |
+
if not user_input or not option1 or not option2:
|
| 14 |
+
return "Please enter a message and both options.", chat_history
|
| 15 |
|
| 16 |
+
options = [option1.strip(), option2.strip()]
|
| 17 |
+
result = evo_chat_predict(chat_history, user_input, options)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Format Evo reply
|
| 20 |
+
evo_response = f"**Answer:** {result['answer']} \n**Reasoning:** {result['reasoning']}"
|
| 21 |
+
chat_history.append(f"User: {user_input}")
|
| 22 |
+
chat_history.append(f"Evo: {evo_response}")
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
return evo_response, chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# π Reset chat history
|
| 27 |
+
def clear_fn():
|
| 28 |
+
global chat_history
|
| 29 |
+
chat_history = []
|
| 30 |
+
return "", "", "", []
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# π Trigger Evo retraining
|
| 33 |
+
def retrain_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
subprocess.run(["python", "retrain_from_feedback.py"], check=True)
|
| 36 |
+
return "β
Evo retrained successfully."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return f"β Retraining failed: {str(e)}"
|
| 39 |
|
| 40 |
+
# π§ Gradio UI layout
|
| 41 |
+
with gr.Blocks(title="EvoRAG β Real-Time Adaptive Reasoning AI") as demo:
|
| 42 |
+
gr.Markdown("## 𧬠EvoRAG β The Evolving Reasoning AI")
|
| 43 |
+
gr.Markdown("Ask a question, give two options, and Evo will decide with confidence. Then, retrain it live.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
+
with gr.Column(scale=4):
|
| 47 |
+
user_input = gr.Textbox(label="Your Question", lines=2)
|
| 48 |
+
option1 = gr.Textbox(label="Option 1")
|
| 49 |
+
option2 = gr.Textbox(label="Option 2")
|
| 50 |
+
submit = gr.Button("π§ Ask Evo")
|
| 51 |
+
clear = gr.Button("π Clear")
|
| 52 |
+
retrain = gr.Button("π Retrain Evo from Feedback")
|
| 53 |
|
| 54 |
+
with gr.Column(scale=6):
|
| 55 |
+
evo_reply = gr.Markdown()
|
| 56 |
+
chat_display = gr.HighlightedText(label="Conversation History")
|
|
|
|
| 57 |
|
| 58 |
+
submit.click(fn=chat_fn, inputs=[user_input, option1, option2],
|
| 59 |
+
outputs=[evo_reply, chat_display])
|
| 60 |
+
clear.click(fn=clear_fn, inputs=[], outputs=[user_input, option1, option2, chat_display])
|
| 61 |
+
retrain.click(fn=retrain_model, inputs=[], outputs=evo_reply)
|
|
|
|
| 62 |
|
| 63 |
demo.launch()
|