Spaces:
Running
Running
import gradio as gr | |
from inference import predict | |
from logger import log_feedback_to_firebase | |
import openai | |
import os | |
from watchdog import manual_retrain | |
from dashboard import render_dashboard | |
openai.api_key = os.environ["OPENAI_API_KEY"] # Add this secret in your Hugging Face Space | |
def gpt_predict(goal, sol1, sol2): | |
prompt = f"You're solving a commonsense reasoning problem. Choose the better solution to achieve the goal.\n\nGoal: {goal}\n\nOption 1: {sol1}\nOption 2: {sol2}\n\nWhich option is better? Reply with only 'Solution 1' or 'Solution 2'." | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0 | |
) | |
reply = response.choices[0].message["content"] | |
return "Solution 1" if "1" in reply else "Solution 2" | |
except Exception as e: | |
return f"GPT Error: {str(e)}" | |
def compare(goal, sol1, sol2, correct): | |
evo = predict(goal, sol1, sol2) | |
gpt = gpt_predict(goal, sol1, sol2) | |
if evo == gpt == correct: | |
verdict = "β Both Evo and GPT-3.5 are correct!" | |
elif evo == correct and gpt != correct: | |
verdict = "π§ Evo got it right. GPT-3.5 missed it." | |
elif gpt == correct and evo != correct: | |
verdict = "π€ GPT-3.5 got it right. Evo missed it." | |
else: | |
verdict = "β Both models got it wrong." | |
log_feedback_to_firebase(goal, sol1, sol2, evo, gpt, correct, "from app.py") | |
return evo, gpt, verdict | |
def trigger_retrain(): | |
success = manual_retrain() | |
return "β Evo retrained successfully!" if success else "β οΈ Retraining failed." | |
with gr.Blocks() as demo: | |
with gr.Tab("βοΈ Evo vs GPT-3.5 Showdown"): | |
gr.Markdown("## βοΈ Evo vs GPT-3.5 β Real-Time Commonsense Showdown") | |
gr.Markdown( | |
"> π§ EvoTransformer v2.1 β PIQA Accuracy: 69.7% (vs GPT-3.5 β 81%)\n" | |
"> 13M Parameters β’ Fully Scratch-Trained β’ Leans Smart\n" | |
"> π§ͺ *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.*" | |
) | |
gr.Markdown( | |
"> π *EvoTransformer is not static. Every time you provide feedback, Evo learns and evolves. Welcome to real-time neural evolution.*" | |
) | |
with gr.Row(): | |
goal = gr.Text(label="Goal") | |
with gr.Row(): | |
sol1 = gr.Text(label="Solution 1") | |
sol2 = gr.Text(label="Solution 2") | |
correct = gr.Radio(choices=["Solution 1", "Solution 2"], label="β Which is actually correct?", value="Solution 1") | |
btn = gr.Button("Submit") | |
evo_out = gr.Text(label="π§ EvoTransformer Response") | |
gpt_out = gr.Text(label="π€ GPT-3.5 Response") | |
verdict_out = gr.Text(label="βοΈ Verdict") | |
btn.click(fn=compare, inputs=[goal, sol1, sol2, correct], outputs=[evo_out, gpt_out, verdict_out]) | |
gr.Markdown("#### π Try These Examples:") | |
examples = [ | |
["Start a fire", "Use a match", "Pour water", "Solution 1"], | |
["Warm up food", "Use microwave", "Put it in fridge", "Solution 1"], | |
["Charge a phone", "Plug it in", "Put it on grass", "Solution 1"], | |
["Get rid of bad smell", "Open window", "Close door", "Solution 1"], | |
] | |
gr.Examples(examples=examples, inputs=[goal, sol1, sol2, correct]) | |
retrain_btn = gr.Button("π Retrain EvoTransformer") | |
retrain_status = gr.Text(label="π’ Retrain Status") | |
retrain_btn.click(fn=trigger_retrain, outputs=[retrain_status]) | |
gr.Markdown("Made with β€οΈ by Dr. Heman Mohabeer β EvoTransformer is not just code. It's evolution.") | |
with gr.Tab("π Evo Dashboard"): | |
render_dashboard() | |
demo.launch() |