HemanM commited on
Commit
c7678f3
Β·
verified Β·
1 Parent(s): ea1b287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -89
app.py CHANGED
@@ -1,101 +1,109 @@
1
  import gradio as gr
2
- from inference import evo_chat_predict, get_model_config, get_gpt_response, load_model
3
  import pandas as pd
4
- import csv
 
 
 
 
 
 
 
5
  import os
6
- from datetime import datetime
7
- from inference import retrain_from_feedback_csv as train_evo
8
-
9
 
10
- feedback_log = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- with gr.Blocks(theme=gr.themes.Base(), css="body { background-color: #0f0f0f; color: #f5f5f5; }") as demo:
13
- with gr.Column():
14
- gr.HTML("""
15
- <div style="padding: 10px; border-radius: 12px; background: #1f1f2e; color: #fff; font-size: 16px; margin-bottom: 12px;">
16
- <b>Why Evo?</b> πŸš€ Evo is not just another AI. It evolves. It learns from you. It adapts its architecture live based on feedback. No retraining labs, no frozen weights. This is <u>live reasoning meets evolution</u>. <span style="color:#88ffcc">Built to outperform, built to survive.</span>
17
- </div>
18
- """)
19
 
20
  with gr.Row():
21
  with gr.Column():
22
- query = gr.Textbox(label="🧠 Your Question", placeholder="e.g. What should you do if there’s a fire?")
23
- option1 = gr.Textbox(label="❌ Option 1", placeholder="Enter the first option")
24
- option2 = gr.Textbox(label="❌ Option 2", placeholder="Enter the second option")
25
- feedback = gr.Radio(["Evo", "GPT"], label="🧠 Who was better?", info="Optional – fuels evolution", interactive=True)
26
- evo_btn = gr.Button("⚑ Ask Evo", elem_id="evo-btn")
27
- retrain_btn = gr.Button("πŸ” Retrain Evo", elem_id="retrain-btn")
28
- clear_btn = gr.Button("🧹 Clear")
29
- export_btn = gr.Button("πŸ“€ Export Feedback CSV")
30
-
31
  with gr.Column():
32
- evo_stats = gr.Textbox(label="πŸ“Š Evo Stats", interactive=False)
33
- evo_box = gr.Textbox(label="🧠 Evo", interactive=False)
34
- gpt_box = gr.Textbox(label="πŸ€– GPT-3.5", interactive=False)
35
- status_box = gr.Textbox(label="πŸ”΅ Status", interactive=False)
36
-
37
- convo = gr.Dataframe(
38
- headers=["Question", "Option 1", "Option 2", "Answer", "Confidence", "Reasoning", "Context"],
39
- interactive=False, wrap=True, label="πŸ“œ Conversation History"
40
- )
41
-
42
- # πŸ” Ask Evo
43
- def ask_evo(q, opt1, opt2, hist, selected):
44
- result = evo_chat_predict(hist, q, [opt1, opt2])
45
- evo_text = f"Answer: {result['answer']} (Confidence: {result['confidence']})\n\nReasoning: {result['reasoning']}"
46
- gpt_text = get_gpt_response(q)
47
- stats = get_model_config()
48
- stats_text = f"Layers: {stats['num_layers']} | Heads: {stats['num_heads']} | FFN: {stats['ffn_dim']} | Memory: {stats['memory_enabled']} | Phase: {stats['phase']} | Accuracy: {stats['accuracy']}"
49
-
50
- # Update history
51
- new_row = [q, opt1, opt2, result["answer"], result["confidence"], result["reasoning"], result["context_used"]]
52
- new_row_df = pd.DataFrame([new_row], columns=hist.columns)
53
- updated_df = new_row_df if hist.empty else pd.concat([hist, new_row_df], ignore_index=True)
54
-
55
- # Log feedback
56
- if selected in ["Evo", "GPT"]:
57
- feedback_log.append(new_row + [selected, "yes" if selected == "Evo" else "no"])
58
-
59
- return evo_text, gpt_text, stats_text, updated_df
60
-
61
- # πŸ” Retrain Evo
62
- def retrain_evo():
63
- if not feedback_log:
64
- return "⚠️ No feedback data to retrain from."
65
-
66
- with open("feedback_log.csv", "w", newline="") as f:
67
- writer = csv.writer(f)
68
- writer.writerow(["question", "option1", "option2", "answer", "confidence", "reasoning", "context", "user_preference", "evo_was_correct"])
69
- for row in feedback_log:
70
- writer.writerow(row)
71
-
72
- train_evo()
73
- load_model()
74
- return f"βœ… Evo retrained on {len(feedback_log)} entries and reloaded."
75
-
76
- # 🧹 Clear UI
77
- def clear_fields():
78
- feedback_log.clear()
79
- return "", "", "", "", "", pd.DataFrame(columns=["Question", "Option 1", "Option 2", "Answer", "Confidence", "Reasoning", "Context"])
80
-
81
- # πŸ“€ Export feedback
82
- def log_feedback_to_csv():
83
- if feedback_log:
84
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
85
- filepath = f"feedback_{timestamp}.csv"
86
- with open(filepath, "w", newline="") as f:
87
- writer = csv.writer(f)
88
- writer.writerow(["Question", "Option 1", "Option 2", "Answer", "Confidence", "Reasoning", "Context", "User Pref", "Evo Correct"])
89
- writer.writerows(feedback_log)
90
- return f"βœ… Feedback exported to {filepath}"
91
- else:
92
- return "⚠️ No feedback to export."
93
-
94
- # πŸ”˜ Event bindings
95
- evo_btn.click(fn=ask_evo, inputs=[query, option1, option2, convo, feedback], outputs=[evo_box, gpt_box, evo_stats, convo])
96
- retrain_btn.click(fn=retrain_evo, inputs=[], outputs=[status_box])
97
- clear_btn.click(fn=clear_fields, inputs=[], outputs=[query, option1, option2, evo_box, gpt_box, convo])
98
- export_btn.click(fn=log_feedback_to_csv, inputs=[], outputs=[status_box])
99
 
100
  if __name__ == "__main__":
101
  demo.launch()
 
1
  import gradio as gr
 
2
  import pandas as pd
3
+ from inference import (
4
+ evo_chat_predict,
5
+ get_gpt_response,
6
+ get_model_config,
7
+ get_system_stats,
8
+ retrain_from_feedback_csv,
9
+ load_model,
10
+ )
11
  import os
12
+ import csv
 
 
13
 
14
+ FEEDBACK_LOG = "feedback_log.csv"
15
+
16
+ # 🧠 Ask Evo
17
+ def ask_evo(question, option1, option2, history):
18
+ options = [option1.strip(), option2.strip()]
19
+ result = evo_chat_predict(history, question.strip(), options)
20
+
21
+ row = {
22
+ "question": question.strip(),
23
+ "option1": option1.strip(),
24
+ "option2": option2.strip(),
25
+ "evo_answer": result["answer"],
26
+ "confidence": result["confidence"],
27
+ "reasoning": result["reasoning"],
28
+ "context": result["context_used"]
29
+ }
30
+
31
+ # Log feedback
32
+ file_exists = os.path.exists(FEEDBACK_LOG)
33
+ with open(FEEDBACK_LOG, "a", newline='', encoding="utf-8") as f:
34
+ writer = csv.DictWriter(f, fieldnames=row.keys())
35
+ if not file_exists:
36
+ writer.writeheader()
37
+ writer.writerow(row)
38
+
39
+ # Prepare outputs
40
+ evo_output = f"Answer: {row['evo_answer']} (Confidence: {row['confidence']})\n\nReasoning: {row['reasoning']}\n\nContext used: {row['context']}"
41
+ gpt_output = get_gpt_response(question)
42
+ history.append(row)
43
+
44
+ stats = get_model_config()
45
+ sys_stats = get_system_stats()
46
+
47
+ # βœ… FIXED PHASE ERROR HERE
48
+ stats_text = f"Layers: {stats.get('num_layers', '?')} | Heads: {stats.get('num_heads', '?')} | FFN: {stats.get('ffn_dim', '?')} | Memory: {stats.get('memory_enabled', '?')} | Accuracy: {stats.get('accuracy', '?')}"
49
+
50
+ sys_text = f"Device: {sys_stats['device']} | CPU: {sys_stats['cpu_usage_percent']}% | RAM: {sys_stats['memory_used_gb']}GB / {sys_stats['memory_total_gb']}GB | GPU: {sys_stats['gpu_name']} ({sys_stats['gpu_memory_used_gb']}GB / {sys_stats['gpu_memory_total_gb']}GB)"
51
+
52
+ return evo_output, gpt_output, stats_text, sys_text, history
53
+
54
+ # πŸ” Manual retrain button
55
+ def retrain_evo():
56
+ msg = retrain_from_feedback_csv()
57
+ load_model(force_reload=True)
58
+ return msg
59
+
60
+ # πŸ“€ Export feedback
61
+ def export_feedback():
62
+ if not os.path.exists(FEEDBACK_LOG):
63
+ return pd.DataFrame()
64
+ return pd.read_csv(FEEDBACK_LOG)
65
+
66
+ # 🧹 Clear
67
+ def clear_all():
68
+ return "", "", "", "", []
69
+
70
+ # πŸ–ΌοΈ UI
71
+ with gr.Blocks(title="🧠 Evo – Reasoning AI") as demo:
72
+ gr.Markdown("## Why Evo? πŸš€ Evo is not just another AI. It evolves. It learns from you. It adapts its architecture live based on feedback.\n\nNo retraining labs, no frozen weights. This is live reasoning meets evolution. Built to outperform, built to survive.")
73
 
74
+ with gr.Row():
75
+ question = gr.Textbox(label="🧠 Your Question", placeholder="e.g. Why is the sky blue?")
76
+ with gr.Row():
77
+ option1 = gr.Textbox(label="❌ Option 1")
78
+ option2 = gr.Textbox(label="❌ Option 2")
 
 
79
 
80
  with gr.Row():
81
  with gr.Column():
82
+ evo_ans = gr.Textbox(label="🧠 Evo", lines=6)
 
 
 
 
 
 
 
 
83
  with gr.Column():
84
+ gpt_ans = gr.Textbox(label="πŸ€– GPT-3.5", lines=6)
85
+
86
+ with gr.Row():
87
+ stats = gr.Textbox(label="πŸ“Š Evo Stats")
88
+ system = gr.Textbox(label="πŸ”΅ Status")
89
+
90
+ with gr.Row():
91
+ evo_radio = gr.Radio(["Evo", "GPT"], label="🧠 Who was better?", info="Optional – fuels evolution")
92
+
93
+ history = gr.State([])
94
+
95
+ with gr.Row():
96
+ ask_btn = gr.Button("⚑ Ask Evo")
97
+ retrain_btn = gr.Button("πŸ” Retrain Evo")
98
+ clear_btn = gr.Button("🧹 Clear")
99
+ export_btn = gr.Button("πŸ“€ Export Feedback CSV")
100
+
101
+ export_table = gr.Dataframe(label="πŸ“œ Conversation History")
102
+
103
+ ask_btn.click(fn=ask_evo, inputs=[question, option1, option2, history], outputs=[evo_ans, gpt_ans, stats, system, history])
104
+ retrain_btn.click(fn=retrain_evo, inputs=[], outputs=[stats])
105
+ clear_btn.click(fn=clear_all, inputs=[], outputs=[question, option1, option2, evo_ans, gpt_ans, stats, system, history])
106
+ export_btn.click(fn=export_feedback, inputs=[], outputs=[export_table])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  if __name__ == "__main__":
109
  demo.launch()