Tarun commited on
Commit ·
fc33c83
1
Parent(s): a23653a
changes 2
Browse files- pyproject.toml +1 -1
- server/app.py +93 -2
pyproject.toml
CHANGED
|
@@ -28,4 +28,4 @@ dependencies = [
|
|
| 28 |
include = ["openenv*"]
|
| 29 |
|
| 30 |
[project.scripts]
|
| 31 |
-
server = "server.app:
|
|
|
|
| 28 |
include = ["openenv*"]
|
| 29 |
|
| 30 |
[project.scripts]
|
| 31 |
+
server = "server.app:main"
|
server/app.py
CHANGED
|
@@ -44,6 +44,97 @@ def step_env(action_req: ActionRequest):
|
|
| 44 |
|
| 45 |
# --- Gradio UI ---
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def ui_reset(task_choice):
|
| 48 |
mapping = {"Easy (Classification)": "easy", "Medium (Assignment)": "medium", "Hard (Resolution)": "hard"}
|
| 49 |
task_name = mapping.get(task_choice, "easy")
|
|
@@ -95,9 +186,9 @@ with gr.Blocks(title="OpenEnv Support Agent") as demo:
|
|
| 95 |
demo.queue()
|
| 96 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 97 |
|
| 98 |
-
def
|
| 99 |
port = int(os.environ.get("PORT", 7860))
|
| 100 |
uvicorn.run("server.app:app", host="0.0.0.0", port=port, reload=True)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
|
|
|
|
| 44 |
|
| 45 |
# --- Gradio UI ---
|
| 46 |
|
| 47 |
+
def ui_reset(task_choice):
|
| 48 |
+
mapping = {"Easy (Classification)": "easy", "Medium (Assignment)": "medium", "Hard (Resolution)": "hard"}
|
| 49 |
+
task_name = mapping.get(task_choice, "easy")
|
| 50 |
+
res = env.reset(task_name)
|
| 51 |
+
return f"Environment reset to {task_name}.", res.observation.model_dump_json(indent=2)
|
| 52 |
+
|
| 53 |
+
def ui_step(a_type, t_name, resp_text):
|
| 54 |
+
if env.done:
|
| 55 |
+
return "Episode is already done! Please reset.", env.state().model_dump_json(indent=2), ""
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
act = Action(action_type=a_type, team=t_name if t_name else None, response=resp_text if resp_text else None)
|
| 59 |
+
res = env.step(act)
|
| 60 |
+
feedback = f"Reward: {res.reward}\nDone: {res.done}\nInfo: {res.info}"
|
| 61 |
+
return feedback, res.observation.model_dump_json(indent=2), "-> " + "\n-> ".join(env.history)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
obs_json = env.get_current_observation().model_dump_json(indent=2) if env.current_scenario else "{}"
|
| 64 |
+
return f"Error: {e}", obs_json, ""
|
| 65 |
+
|
| 66 |
+
with gr.Blocks(title="OpenEnv Support Agent") as demo:
|
| 67 |
+
gr.Markdown("# 🎧 OpenEnv-Based Autonomous Customer Support Agent")
|
| 68 |
+
gr.Markdown("This interface lets you manually step the environment or view the state. For agent evaluation, use `inference.py`.")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
task_dropdown = gr.Dropdown(
|
| 72 |
+
choices=["Easy (Classification)", "Medium (Assignment)", "Hard (Resolution)"],
|
| 73 |
+
value="Easy (Classification)", label="Select Task Level"
|
| 74 |
+
)
|
| 75 |
+
reset_btn = gr.Button("Reset Environment")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
gr.Markdown("### Observation (Current State)")
|
| 80 |
+
obs_box = gr.Code(language="json", label="Observation JSON")
|
| 81 |
+
history_box = gr.Textbox(label="Action History", lines=5, interactive=False)
|
| 82 |
+
|
| 83 |
+
with gr.Column():
|
| 84 |
+
gr.Markdown("### Take Action")
|
| 85 |
+
act_type = gr.Dropdown(choices=["classify", "assign", "respond", "refund", "escalate"], value="classify", label="Action Type")
|
| 86 |
+
act_team = gr.Textbox(label="Team (optional)")
|
| 87 |
+
act_resp = gr.Textbox(label="Response text (optional)")
|
| 88 |
+
step_btn = gr.Button("Step Environment", variant="primary")
|
| 89 |
+
|
| 90 |
+
feedback_box = gr.Textbox(label="Reward / Step Info", interactive=False)
|
| 91 |
+
|
| 92 |
+
import os
|
| 93 |
+
import uvicorn
|
| 94 |
+
import gradio as gr
|
| 95 |
+
from fastapi import FastAPI, BackgroundTasks
|
| 96 |
+
from pydantic import BaseModel
|
| 97 |
+
from dotenv import load_dotenv
|
| 98 |
+
|
| 99 |
+
load_dotenv()
|
| 100 |
+
|
| 101 |
+
from openenv.env import SupportEnv
|
| 102 |
+
from openenv.models import Action
|
| 103 |
+
|
| 104 |
+
app = FastAPI(title="OpenEnv Support Agent API")
|
| 105 |
+
|
| 106 |
+
# Global environment instance for the UI/API
|
| 107 |
+
# In a real deployed multiuser scenario, this would be session-based.
|
| 108 |
+
env = SupportEnv(task_name="easy")
|
| 109 |
+
env.reset()
|
| 110 |
+
|
| 111 |
+
# --- FastAPI Routes ---
|
| 112 |
+
|
| 113 |
+
class ActionRequest(BaseModel):
|
| 114 |
+
action_type: str
|
| 115 |
+
team: str = None
|
| 116 |
+
response: str = None
|
| 117 |
+
|
| 118 |
+
@app.get("/api/state")
|
| 119 |
+
@app.get("/state")
|
| 120 |
+
def get_state():
|
| 121 |
+
return env.state()
|
| 122 |
+
|
| 123 |
+
@app.post("/api/reset")
|
| 124 |
+
@app.post("/reset")
|
| 125 |
+
def reset_env(task_name: str = "easy"):
|
| 126 |
+
res = env.reset(task_name=task_name)
|
| 127 |
+
return res
|
| 128 |
+
|
| 129 |
+
@app.post("/api/step")
|
| 130 |
+
@app.post("/step")
|
| 131 |
+
def step_env(action_req: ActionRequest):
|
| 132 |
+
act = Action(**action_req.model_dump())
|
| 133 |
+
res = env.step(act)
|
| 134 |
+
return res
|
| 135 |
+
|
| 136 |
+
# --- Gradio UI ---
|
| 137 |
+
|
| 138 |
def ui_reset(task_choice):
|
| 139 |
mapping = {"Easy (Classification)": "easy", "Medium (Assignment)": "medium", "Hard (Resolution)": "hard"}
|
| 140 |
task_name = mapping.get(task_choice, "easy")
|
|
|
|
| 186 |
demo.queue()
|
| 187 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 188 |
|
| 189 |
+
def main():
|
| 190 |
port = int(os.environ.get("PORT", 7860))
|
| 191 |
uvicorn.run("server.app:app", host="0.0.0.0", port=port, reload=True)
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|
| 194 |
+
main()
|