HemanM commited on
Commit
c615588
Β·
verified Β·
1 Parent(s): 75ff33c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -83
app.py CHANGED
@@ -1,99 +1,54 @@
1
-
2
-
3
-
4
  import gradio as gr
5
- from inference import predict
6
- from logger import log_feedback_to_firebase
7
- from watchdog import manual_retrain
8
- from dashboard import render_dashboard
9
- from openai import OpenAI
10
  from watchdog import manual_retrain
 
 
 
11
 
12
- import os
 
 
 
13
 
14
- # Initialize OpenAI client
15
- client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) # Add this secret in your HF Space
16
 
17
- def gpt_predict(goal, sol1, sol2):
18
- prompt = (
19
- f"You're solving a commonsense reasoning problem. Choose the better solution to achieve the goal.\n\n"
20
- f"Goal: {goal}\n\nOption 1: {sol1}\nOption 2: {sol2}\n\n"
21
- f"Which option is better? Reply with only 'Solution 1' or 'Solution 2'."
22
- )
23
- try:
24
- response = client.chat.completions.create(
25
- model="gpt-3.5-turbo",
26
- messages=[{"role": "user", "content": prompt}],
27
- temperature=0
28
- )
29
- reply = response.choices[0].message.content
30
- return "Solution 1" if "1" in reply else "Solution 2"
31
- except Exception as e:
32
- return f"GPT Error: {str(e)}"
33
 
34
- def compare(goal, sol1, sol2, correct):
35
- evo = predict(goal, sol1, sol2)
36
- gpt = gpt_predict(goal, sol1, sol2)
37
-
38
- if evo == gpt == correct:
39
- verdict = "βœ… Both Evo and GPT-3.5 are correct!"
40
- elif evo == correct and gpt != correct:
41
- verdict = "🧠 Evo got it right. GPT-3.5 missed it."
42
- elif gpt == correct and evo != correct:
43
- verdict = "πŸ€– GPT-3.5 got it right. Evo missed it."
44
- else:
45
- verdict = "❌ Both models got it wrong."
46
-
47
- log_feedback_to_firebase(goal, sol1, sol2, evo, gpt, correct, "from app.py")
48
-
49
- return evo, gpt, verdict
50
-
51
- def trigger_retrain():
52
  success = manual_retrain()
53
- return "βœ… Evo retrained successfully!" if success else "⚠️ Retraining failed."
54
-
55
- with gr.Blocks() as demo:
56
- with gr.Tab("βš”οΈ Evo vs GPT-3.5 Showdown"):
57
- gr.Markdown("## βš”οΈ Evo vs GPT-3.5 – Real-Time Commonsense Showdown")
58
- gr.Markdown(
59
- "> 🧠 EvoTransformer v2.1 – PIQA Accuracy: 69.7% (vs GPT-3.5 β‰ˆ 81%)\n"
60
- "> 13M Parameters β€’ Fully Scratch-Trained β€’ Leans Smart\n"
61
- "> πŸ§ͺ *Note: EvoTransformer is a scratch-built model trained on 1K PIQA examples. It may occasionally misinterpret context or idioms. That’s part of its evolution.*"
62
- )
63
- gr.Markdown(
64
- "> πŸ”„ *EvoTransformer is not static. Every time you provide feedback, Evo learns and evolves. Welcome to real-time neural evolution.*"
65
- )
66
 
67
- with gr.Row():
68
- goal = gr.Text(label="Goal")
69
- with gr.Row():
70
- sol1 = gr.Text(label="Solution 1")
71
- sol2 = gr.Text(label="Solution 2")
72
- correct = gr.Radio(choices=["Solution 1", "Solution 2"], label="βœ… Which is actually correct?", value="Solution 1")
73
 
74
- btn = gr.Button("Submit")
75
- evo_out = gr.Text(label="🧠 EvoTransformer Response")
76
- gpt_out = gr.Text(label="πŸ€– GPT-3.5 Response")
77
- verdict_out = gr.Text(label="βš–οΈ Verdict")
78
 
79
- btn.click(fn=compare, inputs=[goal, sol1, sol2, correct], outputs=[evo_out, gpt_out, verdict_out])
 
 
 
 
80
 
81
- gr.Markdown("#### πŸ” Try These Examples:")
82
- examples = [
83
- ["Start a fire", "Use a match", "Pour water", "Solution 1"],
84
- ["Warm up food", "Use microwave", "Put it in fridge", "Solution 1"],
85
- ["Charge a phone", "Plug it in", "Put it on grass", "Solution 1"],
86
- ["Get rid of bad smell", "Open window", "Close door", "Solution 1"],
87
- ]
88
- gr.Examples(examples=examples, inputs=[goal, sol1, sol2, correct])
89
 
90
- retrain_btn = gr.Button("πŸ” Retrain EvoTransformer")
91
- retrain_status = gr.Text(label="πŸ“’ Retrain Status")
92
- retrain_btn.click(fn=trigger_retrain, outputs=[retrain_status])
 
93
 
94
- gr.Markdown("Made with ❀️ by Dr. Heman Mohabeer β€” EvoTransformer is not just code. It's evolution.")
 
95
 
96
- with gr.Tab("πŸ“Š Evo Dashboard"):
97
- render_dashboard()
 
98
 
99
  demo.launch()
 
1
+ import os
 
 
2
  import gradio as gr
 
 
 
 
 
3
  from watchdog import manual_retrain
4
+ from inference import generate_response
5
+ from logger import log_feedback
6
+ from dashboard import display_dashboard
7
 
8
+ # βœ… Ensure model is initialized
9
+ if not os.path.exists("trained_model/config.json"):
10
+ print("πŸ“¦ No model found in 'trained_model/'. Initializing...")
11
+ exec(open("init_save.py").read())
12
 
13
+ print("===== Application Startup at 2025-07-14 12:XX:XX =====")
 
14
 
15
+ # βœ… Interface logic
16
+ def evo_chat(goal, sol1, sol2):
17
+ response = generate_response(goal, sol1, sol2)
18
+ return response
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ def retrain_model():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  success = manual_retrain()
22
+ return "βœ… Retrained successfully!" if success else "⚠️ Retrain failed."
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ def log_user_feedback(goal, sol1, sol2, correct):
25
+ log_feedback(goal, sol1, sol2, correct)
26
+ return "βœ… Feedback logged. Thank you!"
 
 
 
27
 
28
+ # βœ… UI
29
+ with gr.Blocks(title="EvoTransformer v2.1") as demo:
30
+ gr.Markdown("### 🧠 EvoTransformer v2.1 – Compare Options and Learn")
 
31
 
32
+ with gr.Row():
33
+ goal = gr.Textbox(label="Goal")
34
+ with gr.Row():
35
+ sol1 = gr.Textbox(label="Option 1")
36
+ sol2 = gr.Textbox(label="Option 2")
37
 
38
+ output = gr.Textbox(label="Model Suggestion")
39
+ run_btn = gr.Button("πŸ” Compare")
40
+ run_btn.click(fn=evo_chat, inputs=[goal, sol1, sol2], outputs=output)
 
 
 
 
 
41
 
42
+ with gr.Row():
43
+ correct = gr.Radio(["Solution 1", "Solution 2"], label="Which was better?")
44
+ log_btn = gr.Button("βœ… Log Feedback")
45
+ log_btn.click(fn=log_user_feedback, inputs=[goal, sol1, sol2, correct], outputs=None)
46
 
47
+ with gr.Accordion("πŸ“Š Dashboard", open=False):
48
+ display_dashboard()
49
 
50
+ retrain_btn = gr.Button("♻️ Retrain Evo")
51
+ retrain_status = gr.Textbox(label="Retrain Status")
52
+ retrain_btn.click(fn=retrain_model, outputs=retrain_status)
53
 
54
  demo.launch()