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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -62
app.py CHANGED
@@ -59,83 +59,105 @@ with gr.Blocks(theme=gr.themes.Base(), title="EvoRAG - Smarter Than GPT?") as de
59
  demo.launch()'''
60
 
61
  import gradio as gr
62
- import torch
63
  import os
64
  from inference import get_evo_response, get_gpt_response
65
  from logger import log_feedback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- # ⬇️ Evo Model Stats
68
- EVO_PARAMS = "~28M Parameters"
69
- EVO_HARDWARE = "Running on CPU (Colab/Space)"
70
- EVO_VERSION = "EvoRAG v2.2 – Adaptive Reasoning"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- # ⬇️ Feedback Logger Wrapper
73
- FEEDBACK_LOG_PATH = "feedback_log.csv"
74
- os.makedirs(os.path.dirname(FEEDBACK_LOG_PATH), exist_ok=True) if os.path.dirname(FEEDBACK_LOG_PATH) else None
75
 
76
- def handle_feedback(is_correct, question, option1, option2, context, evo_output):
77
- feedback = "πŸ‘" if is_correct else "πŸ‘Ž"
78
- log_feedback(question, context, evo_output, feedback)
79
- return "βœ… Feedback recorded. Evo will learn from this." if is_correct else "βœ… Feedback noted."
80
 
81
- def run_comparison(question, option1, option2, context):
82
- options = [option1.strip(), option2.strip()]
83
- evo_ans, evo_reason, evo_conf, evo_ctx = get_evo_response(question, options, context)
84
- gpt_ans = get_gpt_response(question, context)
85
-
86
- evo_output = f"Evo's Suggestion: βœ… {evo_ans}\n\nWhy? {evo_reason}\n\nContext Used: {evo_ctx[:400]}..."
87
- gpt_output = f"GPT-3.5's Suggestion: {gpt_ans}"
88
- return evo_output, gpt_output, evo_ans
89
-
90
- # ⬇️ Interface
91
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
92
- with gr.Column():
93
- gr.Markdown(f"""
94
- <div style='padding: 1em; border-radius: 12px; background: linear-gradient(90deg, #f0f4ff, #eef2fa); border: 1px solid #ccc;'>
95
- <h1 style='font-size: 2em; font-weight: 800;'>🧠 EvoRAG – General-Purpose Adaptive AI</h1>
96
- <p><b>{EVO_VERSION}</b></p>
97
- <p>Trained on reasoning tasks. Live learning from feedback. Combines architecture evolution and retrieval-augmented generation.</p>
98
- <ul>
99
- <li><b>Parameters:</b> {EVO_PARAMS}</li>
100
- <li><b>Hardware:</b> {EVO_HARDWARE}</li>
101
- <li><b>Live Feedback:</b> Logs every correction to evolve smarter.</li>
102
- <li><b>Compare:</b> Evo vs GPT-3.5 on the same question.</li>
103
- </ul>
104
- <p style='font-style: italic; font-size: 0.9em;'>Built for ethical, explainable, and adaptive intelligence.</p>
105
- </div>
106
- """)
107
 
108
- with gr.Row():
109
- question = gr.Textbox(label="πŸ“ Ask a Question", placeholder="e.g., What should you do in case of a fire?", lines=2)
110
- with gr.Row():
111
- option1 = gr.Textbox(label="Option A", placeholder="e.g., Hide inside")
112
- option2 = gr.Textbox(label="Option B", placeholder="e.g., Run for dear life")
113
- context = gr.Textbox(label="πŸ“‚ Optional Context or Notes", placeholder="Paste any extra info here", lines=2)
114
 
 
 
115
  with gr.Row():
116
- evo_out = gr.Textbox(label="🧬 EvoRAG's Reasoned Answer")
117
- gpt_out = gr.Textbox(label="πŸ€– GPT-3.5's Suggestion")
118
-
119
- evo_choice = gr.State()
120
-
121
  with gr.Row():
122
- run_btn = gr.Button("πŸ” Run Comparison")
 
 
123
 
 
124
  with gr.Row():
125
- feedback = gr.Radio(
126
- ["πŸ‘ Evo was correct. Retrain from this.", "πŸ‘Ž Evo was wrong. Improve it."],
127
- label="Was Evo’s answer useful?"
128
- )
129
- submit_feedback = gr.Button("πŸ“¬ Submit Feedback")
130
- feedback_output = gr.Textbox(label="Feedback Status")
 
131
 
132
- run_btn.click(fn=run_comparison, inputs=[question, option1, option2, context], outputs=[evo_out, gpt_out, evo_choice])
 
133
 
 
134
  submit_feedback.click(
135
- fn=lambda fb, q, o1, o2, ctx, eo: handle_feedback(fb == "πŸ‘ Evo was correct. Retrain from this.", q, o1, o2, ctx, eo),
136
- inputs=[feedback, question, option1, option2, context, evo_choice],
137
- outputs=[feedback_output]
138
  )
 
139
 
140
- demo.launch(ssr=True)
141
-
 
59
  demo.launch()'''
60
 
61
  import gradio as gr
 
62
  import os
63
  from inference import get_evo_response, get_gpt_response
64
  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)