TanujInsane
Revert "Fix missing dependencies and app.py consistency"
b8c1416
Raw History Blame Contribute Delete
10.5 kB
import gradio as gr
import threading
import os
import pickle
from flask import Flask, request, jsonify
from environment import DocumentClassificationEnv
from baseline_inference import run_task, load_or_train
from agent import TicketAgent
print("Pre-training models...")
models = {}
for d in ["easy", "medium", "hard"]:
models[d] = load_or_train(d)
print(f" {d} model ready!")
print("All models ready!")
agent = TicketAgent(model_dir=".")
print("Agent ready!")
CATEGORIES = {
"easy": ["General","Billing","Support","Technical","HR"],
"medium": ["General","Billing","Support","Technical","HR","Legal","Sales","Marketing","Operations","Complaints"],
"hard": [f"Cat_{i}" for i in range(22)]
}
api = Flask(__name__)
api_envs = {}
@api.route("/api/reset", methods=["POST"])
def api_reset():
data = request.json or {}
difficulty = data.get("difficulty", "easy")
seed = data.get("seed", 42)
env = DocumentClassificationEnv(task_difficulty=difficulty, seed=seed)
obs, _ = env.reset(seed=seed)
api_envs["current"] = env
return jsonify({"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()}})
@api.route("/api/step", methods=["POST"])
def api_step():
data = request.json or {}
action = data.get("action", 0)
env = api_envs.get("current")
if env is None:
return jsonify({"error": "Call /api/reset first"}), 400
obs, reward, done, _, info = env.step(int(action))
return jsonify({
"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()},
"reward": reward, "done": done, "info": info
})
@api.route("/api/run", methods=["POST"])
def api_run():
data = request.json or {}
difficulty = data.get("difficulty", "easy")
score, t = run_task(difficulty)
return jsonify({"difficulty": difficulty, "score": score, "time": t})
@api.route("/api/agent/process", methods=["POST"])
def api_agent_process():
data = request.json or {}
text = data.get("text", "")
difficulty = data.get("difficulty", "easy")
ticket_id = data.get("ticket_id", None)
if not text:
return jsonify({"error": "text field required"}), 400
result = agent.process_ticket(text, difficulty=difficulty, ticket_id=ticket_id)
return jsonify(result)
@api.route("/api/agent/classify", methods=["POST"])
def api_agent_classify():
data = request.json or {}
text = data.get("text", "")
difficulty = data.get("difficulty", "easy")
category, scores = agent.classify(text, difficulty)
priority = agent.get_priority(text)
return jsonify({"category": category, "priority": priority, "scores": scores})
def run_flask():
api.run(host="0.0.0.0", port=7861, debug=False)
env_state = {"env": None, "difficulty": "easy"}
def create_env(difficulty):
e = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
obs, _ = e.reset()
env_state["env"] = e
env_state["difficulty"] = difficulty
model = models[difficulty]
cats = CATEGORIES[difficulty]
proba = model.predict_proba([obs["content"]])[0]
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
pred_idx = int(model.predict([obs["content"]])[0])
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
return (f"โœ… Environment created! Difficulty: {difficulty}",
obs["content"],
f"๐Ÿค– Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
"")
def classify_doc(category_idx):
e = env_state["env"]
if e is None:
return "Create environment first!", "", "", ""
obs, reward, done, _, info = e.step(int(category_idx))
result = f"Reward: {reward:.2f} | Correct: {info.get('is_correct', False)}"
if done:
acc = info.get('episode_accuracy', 0)
result += f"\n๐Ÿ Episode done! Final Accuracy: {acc:.2%}"
return result, "Episode complete โ€” create new environment", "", str(info)
difficulty = env_state["difficulty"]
model = models[difficulty]
cats = CATEGORIES[difficulty]
proba = model.predict_proba([obs["content"]])[0]
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
pred_idx = int(model.predict([obs["content"]])[0])
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
return (result, obs["content"],
f"๐Ÿค– Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
str(info))
def run_baseline_all():
rows = []
for d in ["easy", "medium", "hard"]:
score, t = run_task(d)
rows.append([d.upper(), f"{score:.4f}", f"{score*100:.1f}%", f"{t:.1f}s"])
return rows
def process_ticket_ui(ticket_text, difficulty):
if not ticket_text.strip():
return "โŒ Please enter ticket text!", "", "", "", "", ""
result = agent.process_ticket(ticket_text, difficulty=difficulty)
category_out = f"๐Ÿท๏ธ {result['category']} (confidence: {result['confidence']*100:.1f}%)"
priority_out = f"๐Ÿšจ {result['priority'].upper()}"
dept_out = f"๐Ÿข {result['department']}\n๐Ÿ“ง {result['email']}\nโฑ๏ธ SLA: {result['sla']}"
top3_out = "\n".join([f" {c}: {p*100:.1f}%" for c,p in result['top3']])
ref_out = f"๐ŸŽซ {result['ref_id']} | โฐ {result['timestamp'][:19]}"
reply_out = result['reply']
return category_out, priority_out, dept_out, top3_out, ref_out, reply_out
SAMPLE_TICKETS = {
"Billing complaint": "My invoice shows an incorrect amount. I was charged $150 but should have been charged $75. Please review and correct this immediately.",
"Bug report": "The application crashes whenever I try to upload a file larger than 10MB. This is a critical issue affecting my workflow.",
"HR query": "I have a question about the company's maternity leave policy. How many weeks of paid leave am I entitled to?",
"Chat message": "Hey, I can't log into my account. The password reset link isn't working either. Please help ASAP!",
"Email ticket": "Dear Support, I would like to inquire about upgrading my current subscription plan to include additional users.",
}
def load_sample(sample_name):
return SAMPLE_TICKETS.get(sample_name, "")
with gr.Blocks(title="Document Classification OpenEnv") as demo:
gr.Markdown("# ๐Ÿ“„ Document Classification OpenEnv")
gr.Markdown("Real-world customer support ticket routing environment for RL agent training.")
with gr.Tab("๐Ÿค– Agent Demo"):
gr.Markdown("### Full Automation โ€” ML Classification + Rule-based Priority + Auto-Reply")
with gr.Row():
with gr.Column(scale=2):
sample_dd = gr.Dropdown(
choices=list(SAMPLE_TICKETS.keys()),
label="๐Ÿ“‹ Load Sample Ticket",
value=None
)
ticket_input = gr.Textbox(
label="โœ๏ธ Ticket / Message Text",
lines=6,
placeholder="Paste email, chat message, bug report, HR query..."
)
diff_agent = gr.Radio(["easy","medium","hard"], value="medium", label="Model Difficulty")
btn_process = gr.Button("๐Ÿš€ Process Ticket", variant="primary")
with gr.Column(scale=2):
ref_out_box = gr.Textbox(label="๐ŸŽซ Reference ID & Timestamp")
cat_out_box = gr.Textbox(label="๐Ÿท๏ธ Category & Confidence")
pri_out_box = gr.Textbox(label="๐Ÿšจ Priority")
dept_out_box = gr.Textbox(label="๐Ÿข Routed To", lines=3)
top3_out_box = gr.Textbox(label="๐Ÿ“Š Top 3 Predictions", lines=3)
reply_out_box = gr.Textbox(label="๐Ÿ“ง Auto-Generated Reply", lines=12)
sample_dd.change(load_sample, inputs=sample_dd, outputs=ticket_input)
btn_process.click(
process_ticket_ui,
inputs=[ticket_input, diff_agent],
outputs=[cat_out_box, pri_out_box, dept_out_box, top3_out_box, ref_out_box, reply_out_box]
)
with gr.Tab("๐ŸŽฎ Interactive Demo"):
diff = gr.Radio(["easy","medium","hard"], value="easy", label="Difficulty")
btn_create = gr.Button("Create Environment", variant="primary")
status = gr.Textbox(label="Status")
with gr.Row():
doc_content = gr.Textbox(label="๐Ÿ“ Document Content", lines=6)
explain_box = gr.Textbox(label="๐Ÿง  Explainability โ€” Model Reasoning", lines=6)
category = gr.Number(label="Category Index (easy: 0-4, medium: 0-9, hard: 0-21)", value=0)
btn_classify = gr.Button("Classify Document")
result = gr.Textbox(label="Result")
info_box = gr.Textbox(label="Info")
btn_create.click(create_env, inputs=diff, outputs=[status, doc_content, explain_box, result])
btn_classify.click(classify_doc, inputs=category, outputs=[result, doc_content, explain_box, info_box])
with gr.Tab("๐Ÿ“Š Baseline Evaluation"):
gr.Markdown("TF-IDF + Logistic Regression baseline โ€” models pre-trained at startup for instant results.")
btn_eval = gr.Button("Run All Tasks", variant="primary")
score_table = gr.Dataframe(headers=["Task","Score","Accuracy","Time"], label="Results")
btn_eval.click(run_baseline_all, outputs=score_table)
with gr.Tab("๐Ÿ“‹ Environment Info"):
gr.Markdown("""
## Environment Design
**Task**: Classify customer support documents into correct departments.
| Difficulty | Categories | Episodes | Reward |
|------------|-----------|----------|--------|
| Easy | 5 | 20 | +1.0 correct, -0.4 wrong |
| Medium | 10 | 30 | +1.0 correct, -0.4 wrong |
| Hard | 22 | 50 | +1.0 correct, -0.4 wrong |
## Agent Pipeline
`Ticket Input` โ†’ `ML Classify` โ†’ `Rule Priority` โ†’ `Department Route` โ†’ `Auto Reply`
## API Endpoints (port 7861)
- `POST /api/reset` โ€” `{"difficulty": "easy", "seed": 42}`
- `POST /api/step` โ€” `{"action": 0}`
- `POST /api/agent/process` โ€” `{"text": "my invoice is wrong", "difficulty": "medium"}`
- `POST /api/agent/classify` โ€” `{"text": "urgent bug", "difficulty": "easy"}`
""")
if __name__ == "__main__":
t = threading.Thread(target=run_flask, daemon=True)
t.start()
demo.launch(server_name="0.0.0.0", server_port=7860)