AdamK29/Meta-OpenENV-Hackathon
0
1import gradio as gr2import asyncio3import matplotlib.pyplot as plt4 5from client import EmailEnvClient6from core.models import EmailAction7 8import ssl9import certifi10 11ssl._create_default_https_context = ssl._create_unverified_context12 13def decide(email):14 email = email.lower()15 16 if "free" in email or "win" in email:17 return "spam"18 if "deadline" in email or "asap" in email:19 return "urgent"20 return "important"21 22 23async def run_episode(task):24 env = EmailEnvClient(base_url="https://adamk29-meta-openenv-hackathon.hf.space")25 await env.__aenter__()26 27 rewards = []28 logs = []29 30 try:31 result = await env.reset(task=task)32 33 step = 034 35 while True:36 step += 137 38 email = result.observation.email_text39 action = decide(email)40 41 result = await env.step(42 EmailAction(action_type="classify", content=action)43 )44 45 reward = result.reward or 0.046 done = result.done47 48 rewards.append(reward)49 50 logs.append(51 f"""52[STEP {step}]53State (Email): {email}54Action: {action}55Reward: {reward:.2f}56Cumulative Reward: {sum(rewards):.2f}57"""58 )59 60 if done:61 break62 63 # Plot reward curve64 fig = plt.figure()65 plt.plot(rewards)66 plt.title("Reward Over Episode")67 68 score = sum(rewards)69 70 return "\n".join(logs), score, fig71 72 finally:73 await env.__aexit__(None, None, None)74 75 76def run(task):77 return asyncio.run(run_episode(task))78 79 80with gr.Blocks() as demo:81 gr.Markdown("# 🤖 RL Email Triage Environment")82 gr.Markdown("Trajectory-based RL interaction")83 84 task = gr.Dropdown(["easy", "medium", "hard"], value="easy")85 86 run_btn = gr.Button("▶ Run Episode")87 88 logs = gr.Textbox(lines=25, label="Trajectory Logs")89 score = gr.Number(label="Total Reward")90 graph = gr.Plot(label="Reward Curve")91 92 run_btn.click(run, inputs=task, outputs=[logs, score, graph])93 94demo.launch()