Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -65,99 +65,119 @@ from logger import log_feedback
|
|
65 |
import csv
|
66 |
import subprocess
|
67 |
|
68 |
-
#
|
69 |
def load_hall_of_fame():
|
70 |
entries = []
|
71 |
if os.path.exists("feedback_log.csv"):
|
72 |
with open("feedback_log.csv", newline='', encoding='utf-8') as f:
|
73 |
reader = csv.DictReader(f)
|
74 |
for row in reader:
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
entries.append(row)
|
79 |
-
except:
|
80 |
-
continue
|
81 |
-
return entries[-10:][::-1] # last 10, reverse order
|
82 |
|
|
|
83 |
def handle_query(question, option1, option2, context):
|
84 |
options = [option1, option2]
|
85 |
evo_answer, evo_reasoning, evo_score, evo_context = get_evo_response(question, options, context)
|
86 |
gpt_answer = get_gpt_response(question, context)
|
87 |
-
|
88 |
-
f"
|
89 |
-
|
90 |
-
f"
|
91 |
)
|
|
|
|
|
|
|
92 |
|
|
|
93 |
def handle_feedback(feedback_text, question, option1, option2, context, evo_output):
|
94 |
is_helpful = "π" in feedback_text
|
95 |
-
log_feedback(question, context, evo_output,
|
96 |
-
return "β
Feedback logged
|
97 |
|
98 |
-
|
|
|
99 |
try:
|
100 |
-
subprocess.run(["python3", "
|
101 |
-
return "
|
102 |
except Exception as e:
|
103 |
-
return f"
|
104 |
|
|
|
105 |
def render_hof():
|
106 |
entries = load_hall_of_fame()
|
107 |
if not entries:
|
108 |
return "No Hall of Fame entries yet. Submit feedback!"
|
109 |
-
|
110 |
-
[
|
111 |
-
|
112 |
-
|
113 |
-
]
|
114 |
-
)
|
115 |
-
return result
|
116 |
|
|
|
117 |
description = """
|
118 |
-
# π§ EvoRAG β Adaptive Reasoning AI
|
119 |
|
120 |
**What is Evo?**
|
121 |
-
EvoTransformer is a lightweight, evolving
|
122 |
-
It
|
123 |
-
|
124 |
-
**Why Evo?**
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
**
|
|
|
|
|
|
|
131 |
"""
|
132 |
|
133 |
-
|
|
|
134 |
gr.Markdown(description)
|
|
|
135 |
with gr.Row():
|
136 |
-
question = gr.Textbox(label="π Ask
|
|
|
137 |
with gr.Row():
|
138 |
option1 = gr.Textbox(label="Option A", placeholder="e.g., Run outside")
|
139 |
option2 = gr.Textbox(label="Option B", placeholder="e.g., Hide under bed")
|
140 |
-
|
|
|
141 |
|
142 |
submit_btn = gr.Button("π Run Comparison")
|
|
|
143 |
with gr.Row():
|
144 |
-
evo_output = gr.
|
145 |
-
gpt_output = gr.
|
146 |
|
147 |
-
feedback = gr.Radio(
|
|
|
|
|
|
|
|
|
148 |
submit_feedback = gr.Button("π¬ Submit Feedback")
|
149 |
-
retrain_button = gr.Button("π Retrain Evo Now")
|
150 |
feedback_status = gr.Textbox(label="Feedback Status", interactive=False)
|
151 |
|
152 |
with gr.Accordion("π Evo Hall of Fame (Top Reasoning Entries)", open=False):
|
153 |
hof_display = gr.Markdown(render_hof())
|
154 |
|
|
|
|
|
|
|
|
|
|
|
155 |
submit_btn.click(fn=handle_query, inputs=[question, option1, option2, context], outputs=[evo_output, gpt_output, feedback_status])
|
|
|
156 |
submit_feedback.click(
|
157 |
fn=lambda fb, q, o1, o2, ctx, eo: handle_feedback(fb, q, o1, o2, ctx, eo),
|
158 |
inputs=[feedback, question, option1, option2, context, feedback_status],
|
159 |
outputs=[feedback_status]
|
160 |
)
|
161 |
-
|
|
|
162 |
|
163 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
65 |
import csv
|
66 |
import subprocess
|
67 |
|
68 |
+
# Load Hall of Fame entries
|
69 |
def load_hall_of_fame():
|
70 |
entries = []
|
71 |
if os.path.exists("feedback_log.csv"):
|
72 |
with open("feedback_log.csv", newline='', encoding='utf-8') as f:
|
73 |
reader = csv.DictReader(f)
|
74 |
for row in reader:
|
75 |
+
if row.get("evo_was_correct", "").lower() == "yes":
|
76 |
+
entries.append(row)
|
77 |
+
return entries[-10:][::-1] # last 10, reversed
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
# Process question & get answers
|
80 |
def handle_query(question, option1, option2, context):
|
81 |
options = [option1, option2]
|
82 |
evo_answer, evo_reasoning, evo_score, evo_context = get_evo_response(question, options, context)
|
83 |
gpt_answer = get_gpt_response(question, context)
|
84 |
+
evo_display = (
|
85 |
+
f"β
Evo's Suggestion: **{evo_answer}**\n\n"
|
86 |
+
f"Why? {evo_reasoning}\n\n"
|
87 |
+
f"Context Used (truncated): {evo_context[:400]}..."
|
88 |
)
|
89 |
+
gpt_display = f"{gpt_answer}"
|
90 |
+
evo_output_summary = f"{question} | {context} | {evo_answer}"
|
91 |
+
return evo_display, gpt_display, evo_output_summary
|
92 |
|
93 |
+
# Feedback handler
|
94 |
def handle_feedback(feedback_text, question, option1, option2, context, evo_output):
|
95 |
is_helpful = "π" in feedback_text
|
96 |
+
log_feedback(question, option1, option2, context, evo_output, is_helpful)
|
97 |
+
return "β
Feedback logged. Evo will improve."
|
98 |
|
99 |
+
# Trigger retrain (placeholder command)
|
100 |
+
def retrain_evo():
|
101 |
try:
|
102 |
+
result = subprocess.run(["python3", "watchdog.py"], capture_output=True, text=True, timeout=60)
|
103 |
+
return f"π Retraining started:\n{result.stdout[:300]}"
|
104 |
except Exception as e:
|
105 |
+
return f"β οΈ Error starting retraining: {str(e)}"
|
106 |
|
107 |
+
# Render Hall of Fame
|
108 |
def render_hof():
|
109 |
entries = load_hall_of_fame()
|
110 |
if not entries:
|
111 |
return "No Hall of Fame entries yet. Submit feedback!"
|
112 |
+
return "\n\n".join([
|
113 |
+
f"π **Q:** {e['question']}\n**Evo A:** {e['evo_output']}\n**Feedback:** β
\n**Context:** {e['context'][:200]}..."
|
114 |
+
for e in entries
|
115 |
+
])
|
|
|
|
|
|
|
116 |
|
117 |
+
# Header / Description
|
118 |
description = """
|
119 |
+
# π§ EvoRAG β Real-Time Adaptive Reasoning AI
|
120 |
|
121 |
**What is Evo?**
|
122 |
+
EvoTransformer is a lightweight, evolving transformer (~28M params).
|
123 |
+
It adapts on the fly, learns from feedback, uses live web + user context to reason.
|
124 |
+
|
125 |
+
**Why Evo over GPT?**
|
126 |
+
β
Evolves from human input
|
127 |
+
β
Architecturally updatable
|
128 |
+
β
Transparent and fine-tunable
|
129 |
+
β
Efficient on modest hardware
|
130 |
+
|
131 |
+
**Hardware:** Google Colab CPU/GPU
|
132 |
+
**Max Tokens per input:** 128
|
133 |
+
**Benchmarks:** PIQA, HellaSwag, ARC
|
134 |
+
**Version:** Evo v2.2 β Memory + Retrieval + Feedback Learning
|
135 |
"""
|
136 |
|
137 |
+
# Build Interface
|
138 |
+
with gr.Blocks(title="EvoRAG β Evo vs GPT Reasoning") as demo:
|
139 |
gr.Markdown(description)
|
140 |
+
|
141 |
with gr.Row():
|
142 |
+
question = gr.Textbox(label="π Ask any question", placeholder="e.g., Whatβs the best way to escape a house fire?")
|
143 |
+
|
144 |
with gr.Row():
|
145 |
option1 = gr.Textbox(label="Option A", placeholder="e.g., Run outside")
|
146 |
option2 = gr.Textbox(label="Option B", placeholder="e.g., Hide under bed")
|
147 |
+
|
148 |
+
context = gr.Textbox(label="π Optional Context", placeholder="Paste relevant info, article, user context", lines=3)
|
149 |
|
150 |
submit_btn = gr.Button("π Run Comparison")
|
151 |
+
|
152 |
with gr.Row():
|
153 |
+
evo_output = gr.Markdown(label="π§ EvoRAG's Reasoned Answer")
|
154 |
+
gpt_output = gr.Markdown(label="π€ GPT-3.5's Suggestion")
|
155 |
|
156 |
+
feedback = gr.Radio(
|
157 |
+
["π Evo was correct. Retrain from this.", "π Evo was wrong. Don't retrain."],
|
158 |
+
label="Was Evoβs answer better?",
|
159 |
+
value=None
|
160 |
+
)
|
161 |
submit_feedback = gr.Button("π¬ Submit Feedback")
|
|
|
162 |
feedback_status = gr.Textbox(label="Feedback Status", interactive=False)
|
163 |
|
164 |
with gr.Accordion("π Evo Hall of Fame (Top Reasoning Entries)", open=False):
|
165 |
hof_display = gr.Markdown(render_hof())
|
166 |
|
167 |
+
with gr.Accordion("π Live Evo Retraining (Manual Trigger)", open=False):
|
168 |
+
retrain_btn = gr.Button("Retrain Evo from Feedback Now")
|
169 |
+
retrain_status = gr.Textbox(label="Retrain Status", interactive=False)
|
170 |
+
|
171 |
+
# Events
|
172 |
submit_btn.click(fn=handle_query, inputs=[question, option1, option2, context], outputs=[evo_output, gpt_output, feedback_status])
|
173 |
+
|
174 |
submit_feedback.click(
|
175 |
fn=lambda fb, q, o1, o2, ctx, eo: handle_feedback(fb, q, o1, o2, ctx, eo),
|
176 |
inputs=[feedback, question, option1, option2, context, feedback_status],
|
177 |
outputs=[feedback_status]
|
178 |
)
|
179 |
+
|
180 |
+
retrain_btn.click(fn=retrain_evo, inputs=[], outputs=[retrain_status])
|
181 |
|
182 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
183 |
+
|