HemanM commited on
Commit
1db4c59
Β·
verified Β·
1 Parent(s): 5340d71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -44
app.py CHANGED
@@ -65,99 +65,119 @@ from logger import log_feedback
65
  import csv
66
  import subprocess
67
 
68
- # Helper to load Hall of Fame
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
- try:
76
- score = float(row.get("feedback", "0"))
77
- if "πŸ‘" in row["feedback"] or score > 0.85:
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
- return (
88
- f"Answer: {evo_answer} (Confidence: {evo_score:.2f})\n\nReasoning: {evo_reasoning}\n\nContext used: {evo_context[:400]}...",
89
- gpt_answer,
90
- f"{question} | {context} | {evo_answer}"
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, feedback_text)
96
- return "βœ… Feedback logged and Evo will improve."
97
 
98
- def trigger_retraining():
 
99
  try:
100
- subprocess.run(["python3", "retrain.py"], check=True)
101
- return "βœ… Evo retrained from recent feedback!"
102
  except Exception as e:
103
- return f"❌ Retrain failed: {str(e)}"
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
- result = "\n\n".join(
110
- [
111
- f"πŸ† **Q:** {e['question']}\n**A:** {e['evo_answer']}\n**Feedback:** {e['feedback']}\n**Context:** {e['context'][:200]}..."
112
- for e in entries
113
- ]
114
- )
115
- return result
116
 
 
117
  description = """
118
- # 🧠 EvoRAG – Adaptive Reasoning AI
119
 
120
  **What is Evo?**
121
- EvoTransformer is a lightweight, evolving neural network with ~28M parameters.
122
- It learns from feedback, adapts over time, and reasons using both web and context data.
123
-
124
- **Why Evo?**
125
- Unlike GPT, Evo evolves from your interaction β€” architecture, weights, and logic. It's efficient, smart, and improvable on the fly.
126
-
127
- **Hardware**: Trained on Google Colab CPU/GPU.
128
- **Token limit**: 128.
129
- **Benchmark**: PIQA, HellaSwag, ARC
130
- **Version**: Evo v2.2 (Memory + Web Retrieval + Feedback Learning)
 
 
 
131
  """
132
 
133
- with gr.Blocks(title="EvoRAG") as demo:
 
134
  gr.Markdown(description)
 
135
  with gr.Row():
136
- question = gr.Textbox(label="πŸ“ Ask anything", placeholder="e.g., What’s the best way to escape a house fire?")
 
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
- context = gr.Textbox(label="πŸ“‚ Optional Context", placeholder="Paste any extra background info here", lines=3)
 
141
 
142
  submit_btn = gr.Button("πŸ” Run Comparison")
 
143
  with gr.Row():
144
- evo_output = gr.Textbox(label="🧠 EvoRAG's Reasoned Answer", lines=6)
145
- gpt_output = gr.Textbox(label="πŸ€– GPT-3.5's Suggestion", lines=6)
146
 
147
- feedback = gr.Radio(["πŸ‘ Evo was correct. Retrain from this.", "πŸ‘Ž Evo was wrong. Don't retrain."], label="Was Evo’s answer useful?", value=None)
 
 
 
 
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
- retrain_button.click(fn=trigger_retraining, inputs=[], outputs=[feedback_status])
 
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
+