TanujInsane commited on
Commit ยท
28bf6cd
1
Parent(s): a739312
fix app imports and syntax
Browse files
app.py
CHANGED
|
@@ -1,267 +1,232 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Gradio UI for Document Classification Environment
|
| 3 |
-
Interactive demo for judges + baseline evaluation
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
-
import
|
| 8 |
-
import
|
|
|
|
|
|
|
| 9 |
from environment import DocumentClassificationEnv
|
| 10 |
-
from baseline_inference import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
SAMPLE_TICKETS = {
|
| 28 |
-
"Billing
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"Refund Request": "I would like to request a refund for my recent purchase. The product did not meet the specifications described and I need to return it and get refunded.",
|
| 34 |
}
|
| 35 |
|
| 36 |
-
|
| 37 |
-
def start_episode(difficulty):
|
| 38 |
-
global current_env, current_obs, current_difficulty
|
| 39 |
-
current_difficulty = difficulty
|
| 40 |
-
current_env = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
|
| 41 |
-
current_obs, _ = current_env.reset()
|
| 42 |
-
|
| 43 |
-
categories = list(current_env.CATEGORY_MAPS[difficulty].values())
|
| 44 |
-
doc_content = current_obs["content"]
|
| 45 |
-
word_count = int(current_obs["word_count"][0])
|
| 46 |
-
doc_id = current_obs["document_id"]
|
| 47 |
-
total = int(current_obs["total_documents"][0])
|
| 48 |
-
|
| 49 |
-
info_text = f"**Document ID:** {doc_id} | **Words:** {word_count} | **Total Docs:** {total}"
|
| 50 |
-
return doc_content, info_text, gr.update(choices=categories, value=None), "Episode started! Classify the document above.", ""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def classify_document(selected_category):
|
| 54 |
-
global current_env, current_obs, current_difficulty
|
| 55 |
-
|
| 56 |
-
if current_env is None:
|
| 57 |
-
return "Please start an episode first!", "", "", gr.update(choices=[])
|
| 58 |
-
if selected_category is None:
|
| 59 |
-
return current_obs["content"], "", "Please select a category!", ""
|
| 60 |
-
|
| 61 |
-
categories = list(current_env.CATEGORY_MAPS[current_difficulty].values())
|
| 62 |
-
action = categories.index(selected_category)
|
| 63 |
-
|
| 64 |
-
obs, reward, terminated, _, info = current_env.step(action)
|
| 65 |
-
current_obs = obs
|
| 66 |
-
|
| 67 |
-
is_correct = info.get("is_correct", False)
|
| 68 |
-
true_cat = info.get("true_category", "?")
|
| 69 |
-
pred_cat = info.get("predicted_category", "?")
|
| 70 |
-
accuracy = info.get("episode_accuracy", 0)
|
| 71 |
-
|
| 72 |
-
true_icon = CATEGORY_COLORS.get(true_cat, "๐")
|
| 73 |
-
pred_icon = CATEGORY_COLORS.get(pred_cat, "๐")
|
| 74 |
-
|
| 75 |
-
result = f"{'โ
CORRECT' if is_correct else 'โ INCORRECT'}\n"
|
| 76 |
-
result += f"Your pick: {pred_icon} {pred_cat}\n"
|
| 77 |
-
result += f"True category: {true_icon} {true_cat}\n"
|
| 78 |
-
result += f"Reward: {reward:.3f} | Accuracy so far: {accuracy:.1%}"
|
| 79 |
-
|
| 80 |
-
if terminated:
|
| 81 |
-
summary = info.get("episode_summary", {})
|
| 82 |
-
final_acc = summary.get("accuracy", 0)
|
| 83 |
-
total_reward = summary.get("total_reward", 0)
|
| 84 |
-
next_doc = "๐ Episode Complete!"
|
| 85 |
-
result += f"\n\n**FINAL: Accuracy={final_acc:.1%} | Total Reward={total_reward:.2f}**"
|
| 86 |
-
return "Episode complete! Click 'Start Episode' to play again.", "", result, gr.update(choices=categories)
|
| 87 |
-
|
| 88 |
-
next_doc = obs["content"]
|
| 89 |
-
next_id = obs["document_id"]
|
| 90 |
-
next_words = int(obs["word_count"][0])
|
| 91 |
-
next_idx = int(obs["document_index"][0])
|
| 92 |
-
total = int(obs["total_documents"][0])
|
| 93 |
-
info_text = f"**Document ID:** {next_id} | **Words:** {next_words} | **Progress:** {next_idx}/{total}"
|
| 94 |
-
|
| 95 |
-
return next_doc, info_text, result, gr.update(choices=categories, value=None)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def auto_classify():
|
| 99 |
-
global current_env, current_obs, current_difficulty
|
| 100 |
-
if current_env is None or current_obs is None:
|
| 101 |
-
return "Please start an episode first!", "", "No episode running.", ""
|
| 102 |
-
|
| 103 |
-
action = keyword_agent(current_obs, current_difficulty)
|
| 104 |
-
categories = list(current_env.CATEGORY_MAPS[current_difficulty].values())
|
| 105 |
-
selected = categories[action]
|
| 106 |
-
return classify_document(selected)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
def load_sample(sample_name):
|
| 110 |
return SAMPLE_TICKETS.get(sample_name, "")
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
correct += int(info.get("is_correct", False))
|
| 125 |
-
total += 1
|
| 126 |
-
if total <= 10:
|
| 127 |
-
true_cat = info.get("true_category", "?")
|
| 128 |
-
pred_cat = info.get("predicted_category", "?")
|
| 129 |
-
icon = "โ
" if info.get("is_correct") else "โ"
|
| 130 |
-
results.append(f"{icon} Predicted: {pred_cat} | True: {true_cat}")
|
| 131 |
-
|
| 132 |
-
accuracy = correct / total if total > 0 else 0
|
| 133 |
-
output = f"## Baseline Results โ {difficulty.upper()}\n\n"
|
| 134 |
-
output += f"**Accuracy: {accuracy:.1%}** ({correct}/{total} correct)\n\n"
|
| 135 |
-
output += "### Sample Predictions (first 10):\n"
|
| 136 |
-
output += "\n".join(results)
|
| 137 |
-
return output
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
def create_interface():
|
| 141 |
-
with gr.Blocks(title="Document Classification OpenEnv", theme=gr.themes.Soft()) as demo:
|
| 142 |
-
gr.Markdown("""
|
| 143 |
-
# ๐ Document Classification Environment
|
| 144 |
-
### OpenEnv โ Meta x PyTorch Hackathon Submission
|
| 145 |
-
An interactive environment for training AI agents to classify and route support tickets.
|
| 146 |
-
""")
|
| 147 |
-
|
| 148 |
-
with gr.Tabs():
|
| 149 |
-
# Tab 1: Interactive Demo
|
| 150 |
-
with gr.TabItem("๐ฎ Interactive Demo"):
|
| 151 |
-
gr.Markdown("### Try classifying documents yourself!")
|
| 152 |
-
with gr.Row():
|
| 153 |
-
difficulty_select = gr.Radio(
|
| 154 |
-
choices=["easy", "medium", "hard"],
|
| 155 |
-
value="easy",
|
| 156 |
-
label="Difficulty Level",
|
| 157 |
-
info="Easy=5 categories, Medium=10, Hard=22"
|
| 158 |
-
)
|
| 159 |
-
start_btn = gr.Button("โถ Start Episode", variant="primary", scale=1)
|
| 160 |
-
|
| 161 |
-
with gr.Row():
|
| 162 |
-
with gr.Column(scale=2):
|
| 163 |
-
doc_display = gr.Textbox(
|
| 164 |
-
label="๐จ Document to Classify",
|
| 165 |
-
lines=6,
|
| 166 |
-
placeholder="Click 'Start Episode' to begin..."
|
| 167 |
-
)
|
| 168 |
-
doc_info = gr.Markdown("")
|
| 169 |
-
|
| 170 |
-
with gr.Column(scale=1):
|
| 171 |
-
category_radio = gr.Radio(choices=[], label="๐ท๏ธ Select Category")
|
| 172 |
-
with gr.Row():
|
| 173 |
-
classify_btn = gr.Button("โ
Classify", variant="primary")
|
| 174 |
-
auto_btn = gr.Button("๐ค Auto (AI)", variant="secondary")
|
| 175 |
-
|
| 176 |
-
result_display = gr.Textbox(label="๐ Result", lines=5)
|
| 177 |
-
|
| 178 |
-
# Sample tickets
|
| 179 |
-
gr.Markdown("### ๐ Try Sample Tickets")
|
| 180 |
-
with gr.Row():
|
| 181 |
-
for name in SAMPLE_TICKETS:
|
| 182 |
-
gr.Button(name).click(
|
| 183 |
-
fn=lambda n=name: load_sample(n),
|
| 184 |
-
outputs=doc_display
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
start_btn.click(
|
| 188 |
-
fn=start_episode,
|
| 189 |
-
inputs=difficulty_select,
|
| 190 |
-
outputs=[doc_display, doc_info, category_radio, result_display, gr.Textbox(visible=False)]
|
| 191 |
)
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
gr.
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
##
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
```
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
-
|
| 253 |
-
|
| 254 |
-
- **HARD:** 0.165
|
| 255 |
-
|
| 256 |
-
### Categories (Hard Mode โ 22 total)
|
| 257 |
-
General, Billing, Billing-Dispute, Billing-Refund, Support, Support-Urgent, Support-Normal,
|
| 258 |
-
Technical, Technical-Bug, Technical-Feature, HR, HR-Payroll, HR-Benefits, HR-Complaint,
|
| 259 |
-
Legal, Legal-Contract, Legal-Compliance, Executive, Executive-Strategic, Finance, Marketing, Operations
|
| 260 |
-
""")
|
| 261 |
-
|
| 262 |
-
return demo
|
| 263 |
-
|
| 264 |
|
| 265 |
if __name__ == "__main__":
|
| 266 |
-
|
|
|
|
| 267 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import threading
|
| 3 |
+
import os
|
| 4 |
+
import pickle
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
from environment import DocumentClassificationEnv
|
| 7 |
+
from baseline_inference import run_task, load_or_train
|
| 8 |
+
from agent import TicketAgent
|
| 9 |
+
|
| 10 |
+
print("Pre-training models...")
|
| 11 |
+
models = {}
|
| 12 |
+
for d in ["easy", "medium", "hard"]:
|
| 13 |
+
models[d] = load_or_train(d)
|
| 14 |
+
print(f" {d} model ready!")
|
| 15 |
+
print("All models ready!")
|
| 16 |
+
|
| 17 |
+
agent = TicketAgent(model_dir=".")
|
| 18 |
+
print("Agent ready!")
|
| 19 |
+
|
| 20 |
+
CATEGORIES = {
|
| 21 |
+
"easy": ["General","Billing","Support","Technical","HR"],
|
| 22 |
+
"medium": ["General","Billing","Support","Technical","HR","Legal","Sales","Marketing","Operations","Complaints"],
|
| 23 |
+
"hard": [f"Cat_{i}" for i in range(22)]
|
| 24 |
}
|
| 25 |
|
| 26 |
+
api = Flask(__name__)
|
| 27 |
+
api_envs = {}
|
| 28 |
+
|
| 29 |
+
@api.route("/api/reset", methods=["POST"])
|
| 30 |
+
def api_reset():
|
| 31 |
+
data = request.json or {}
|
| 32 |
+
difficulty = data.get("difficulty", "easy")
|
| 33 |
+
seed = data.get("seed", 42)
|
| 34 |
+
env = DocumentClassificationEnv(task_difficulty=difficulty, seed=seed)
|
| 35 |
+
obs, _ = env.reset(seed=seed)
|
| 36 |
+
api_envs["current"] = env
|
| 37 |
+
return jsonify({"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()}})
|
| 38 |
+
|
| 39 |
+
@api.route("/api/step", methods=["POST"])
|
| 40 |
+
def api_step():
|
| 41 |
+
data = request.json or {}
|
| 42 |
+
action = data.get("action", 0)
|
| 43 |
+
env = api_envs.get("current")
|
| 44 |
+
if env is None:
|
| 45 |
+
return jsonify({"error": "Call /api/reset first"}), 400
|
| 46 |
+
obs, reward, done, _, info = env.step(int(action))
|
| 47 |
+
return jsonify({
|
| 48 |
+
"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()},
|
| 49 |
+
"reward": reward, "done": done, "info": info
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
@api.route("/api/run", methods=["POST"])
|
| 53 |
+
def api_run():
|
| 54 |
+
data = request.json or {}
|
| 55 |
+
difficulty = data.get("difficulty", "easy")
|
| 56 |
+
score, t = run_task(difficulty)
|
| 57 |
+
return jsonify({"difficulty": difficulty, "score": score, "time": t})
|
| 58 |
+
|
| 59 |
+
@api.route("/api/agent/process", methods=["POST"])
|
| 60 |
+
def api_agent_process():
|
| 61 |
+
data = request.json or {}
|
| 62 |
+
text = data.get("text", "")
|
| 63 |
+
difficulty = data.get("difficulty", "easy")
|
| 64 |
+
ticket_id = data.get("ticket_id", None)
|
| 65 |
+
if not text:
|
| 66 |
+
return jsonify({"error": "text field required"}), 400
|
| 67 |
+
result = agent.process_ticket(text, difficulty=difficulty, ticket_id=ticket_id)
|
| 68 |
+
return jsonify(result)
|
| 69 |
+
|
| 70 |
+
@api.route("/api/agent/classify", methods=["POST"])
|
| 71 |
+
def api_agent_classify():
|
| 72 |
+
data = request.json or {}
|
| 73 |
+
text = data.get("text", "")
|
| 74 |
+
difficulty = data.get("difficulty", "easy")
|
| 75 |
+
category, scores = agent.classify(text, difficulty)
|
| 76 |
+
priority = agent.get_priority(text)
|
| 77 |
+
return jsonify({"category": category, "priority": priority, "scores": scores})
|
| 78 |
+
|
| 79 |
+
def run_flask():
|
| 80 |
+
api.run(host="0.0.0.0", port=7861, debug=False)
|
| 81 |
+
|
| 82 |
+
env_state = {"env": None, "difficulty": "easy"}
|
| 83 |
+
|
| 84 |
+
def create_env(difficulty):
|
| 85 |
+
e = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
|
| 86 |
+
obs, _ = e.reset()
|
| 87 |
+
env_state["env"] = e
|
| 88 |
+
env_state["difficulty"] = difficulty
|
| 89 |
+
model = models[difficulty]
|
| 90 |
+
cats = CATEGORIES[difficulty]
|
| 91 |
+
proba = model.predict_proba([obs["content"]])[0]
|
| 92 |
+
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
|
| 93 |
+
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
|
| 94 |
+
pred_idx = int(model.predict([obs["content"]])[0])
|
| 95 |
+
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
|
| 96 |
+
return (f"โ
Environment created! Difficulty: {difficulty}",
|
| 97 |
+
obs["content"],
|
| 98 |
+
f"๐ค Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
|
| 99 |
+
"")
|
| 100 |
+
|
| 101 |
+
def classify_doc(category_idx):
|
| 102 |
+
e = env_state["env"]
|
| 103 |
+
if e is None:
|
| 104 |
+
return "Create environment first!", "", "", ""
|
| 105 |
+
obs, reward, done, _, info = e.step(int(category_idx))
|
| 106 |
+
result = f"Reward: {reward:.2f} | Correct: {info.get('is_correct', False)}"
|
| 107 |
+
if done:
|
| 108 |
+
acc = info.get('episode_accuracy', 0)
|
| 109 |
+
result += f"\n๐ Episode done! Final Accuracy: {acc:.2%}"
|
| 110 |
+
return result, "Episode complete โ create new environment", "", str(info)
|
| 111 |
+
difficulty = env_state["difficulty"]
|
| 112 |
+
model = models[difficulty]
|
| 113 |
+
cats = CATEGORIES[difficulty]
|
| 114 |
+
proba = model.predict_proba([obs["content"]])[0]
|
| 115 |
+
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
|
| 116 |
+
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
|
| 117 |
+
pred_idx = int(model.predict([obs["content"]])[0])
|
| 118 |
+
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
|
| 119 |
+
return (result, obs["content"],
|
| 120 |
+
f"๐ค Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
|
| 121 |
+
str(info))
|
| 122 |
+
|
| 123 |
+
def run_baseline_all():
|
| 124 |
+
rows = []
|
| 125 |
+
for d in ["easy", "medium", "hard"]:
|
| 126 |
+
score, t = run_task(d)
|
| 127 |
+
rows.append([d.upper(), f"{score:.4f}", f"{score*100:.1f}%", f"{t:.1f}s"])
|
| 128 |
+
return rows
|
| 129 |
+
|
| 130 |
+
def process_ticket_ui(ticket_text, difficulty):
|
| 131 |
+
if not ticket_text.strip():
|
| 132 |
+
return "โ Please enter ticket text!", "", "", "", "", ""
|
| 133 |
+
result = agent.process_ticket(ticket_text, difficulty=difficulty)
|
| 134 |
+
category_out = f"๐ท๏ธ {result['category']} (confidence: {result['confidence']*100:.1f}%)"
|
| 135 |
+
priority_out = f"๐จ {result['priority'].upper()}"
|
| 136 |
+
dept_out = f"๐ข {result['department']}\n๐ง {result['email']}\nโฑ๏ธ SLA: {result['sla']}"
|
| 137 |
+
top3_out = "\n".join([f" {c}: {p*100:.1f}%" for c,p in result['top3']])
|
| 138 |
+
ref_out = f"๐ซ {result['ref_id']} | โฐ {result['timestamp'][:19]}"
|
| 139 |
+
reply_out = result['reply']
|
| 140 |
+
return category_out, priority_out, dept_out, top3_out, ref_out, reply_out
|
| 141 |
+
|
| 142 |
SAMPLE_TICKETS = {
|
| 143 |
+
"Billing complaint": "My invoice shows an incorrect amount. I was charged $150 but should have been charged $75. Please review and correct this immediately.",
|
| 144 |
+
"Bug report": "The application crashes whenever I try to upload a file larger than 10MB. This is a critical issue affecting my workflow.",
|
| 145 |
+
"HR query": "I have a question about the company's maternity leave policy. How many weeks of paid leave am I entitled to?",
|
| 146 |
+
"Chat message": "Hey, I can't log into my account. The password reset link isn't working either. Please help ASAP!",
|
| 147 |
+
"Email ticket": "Dear Support, I would like to inquire about upgrading my current subscription plan to include additional users.",
|
|
|
|
| 148 |
}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def load_sample(sample_name):
|
| 151 |
return SAMPLE_TICKETS.get(sample_name, "")
|
| 152 |
|
| 153 |
+
with gr.Blocks(title="Document Classification OpenEnv") as demo:
|
| 154 |
+
gr.Markdown("# ๐ Document Classification OpenEnv")
|
| 155 |
+
gr.Markdown("Real-world customer support ticket routing environment for RL agent training.")
|
| 156 |
+
|
| 157 |
+
with gr.Tab("๐ค Agent Demo"):
|
| 158 |
+
gr.Markdown("### Full Automation โ ML Classification + Rule-based Priority + Auto-Reply")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column(scale=2):
|
| 161 |
+
sample_dd = gr.Dropdown(
|
| 162 |
+
choices=list(SAMPLE_TICKETS.keys()),
|
| 163 |
+
label="๐ Load Sample Ticket",
|
| 164 |
+
value=None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
)
|
| 166 |
+
ticket_input = gr.Textbox(
|
| 167 |
+
label="โ๏ธ Ticket / Message Text",
|
| 168 |
+
lines=6,
|
| 169 |
+
placeholder="Paste email, chat message, bug report, HR query..."
|
| 170 |
)
|
| 171 |
+
diff_agent = gr.Radio(["easy","medium","hard"], value="medium", label="Model Difficulty")
|
| 172 |
+
btn_process = gr.Button("๐ Process Ticket", variant="primary")
|
| 173 |
+
with gr.Column(scale=2):
|
| 174 |
+
ref_out_box = gr.Textbox(label="๐ซ Reference ID & Timestamp")
|
| 175 |
+
cat_out_box = gr.Textbox(label="๐ท๏ธ Category & Confidence")
|
| 176 |
+
pri_out_box = gr.Textbox(label="๐จ Priority")
|
| 177 |
+
dept_out_box = gr.Textbox(label="๐ข Routed To", lines=3)
|
| 178 |
+
top3_out_box = gr.Textbox(label="๐ Top 3 Predictions", lines=3)
|
| 179 |
+
reply_out_box = gr.Textbox(label="๐ง Auto-Generated Reply", lines=12)
|
| 180 |
+
|
| 181 |
+
sample_dd.change(load_sample, inputs=sample_dd, outputs=ticket_input)
|
| 182 |
+
btn_process.click(
|
| 183 |
+
process_ticket_ui,
|
| 184 |
+
inputs=[ticket_input, diff_agent],
|
| 185 |
+
outputs=[cat_out_box, pri_out_box, dept_out_box, top3_out_box, ref_out_box, reply_out_box]
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
with gr.Tab("๐ฎ Interactive Demo"):
|
| 189 |
+
diff = gr.Radio(["easy","medium","hard"], value="easy", label="Difficulty")
|
| 190 |
+
btn_create = gr.Button("Create Environment", variant="primary")
|
| 191 |
+
status = gr.Textbox(label="Status")
|
| 192 |
+
with gr.Row():
|
| 193 |
+
doc_content = gr.Textbox(label="๐ Document Content", lines=6)
|
| 194 |
+
explain_box = gr.Textbox(label="๐ง Explainability โ Model Reasoning", lines=6)
|
| 195 |
+
category = gr.Number(label="Category Index (easy: 0-4, medium: 0-9, hard: 0-21)", value=0)
|
| 196 |
+
btn_classify = gr.Button("Classify Document")
|
| 197 |
+
result = gr.Textbox(label="Result")
|
| 198 |
+
info_box = gr.Textbox(label="Info")
|
| 199 |
+
btn_create.click(create_env, inputs=diff, outputs=[status, doc_content, explain_box, result])
|
| 200 |
+
btn_classify.click(classify_doc, inputs=category, outputs=[result, doc_content, explain_box, info_box])
|
| 201 |
+
|
| 202 |
+
with gr.Tab("๐ Baseline Evaluation"):
|
| 203 |
+
gr.Markdown("TF-IDF + Logistic Regression baseline โ models pre-trained at startup for instant results.")
|
| 204 |
+
btn_eval = gr.Button("Run All Tasks", variant="primary")
|
| 205 |
+
score_table = gr.Dataframe(headers=["Task","Score","Accuracy","Time"], label="Results")
|
| 206 |
+
btn_eval.click(run_baseline_all, outputs=score_table)
|
| 207 |
+
|
| 208 |
+
with gr.Tab("๐ Environment Info"):
|
| 209 |
+
gr.Markdown("""
|
| 210 |
+
## Environment Design
|
| 211 |
+
**Task**: Classify customer support documents into correct departments.
|
| 212 |
+
|
| 213 |
+
| Difficulty | Categories | Episodes | Reward |
|
| 214 |
+
|------------|-----------|----------|--------|
|
| 215 |
+
| Easy | 5 | 20 | +1.0 correct, -0.4 wrong |
|
| 216 |
+
| Medium | 10 | 30 | +1.0 correct, -0.4 wrong |
|
| 217 |
+
| Hard | 22 | 50 | +1.0 correct, -0.4 wrong |
|
| 218 |
+
|
| 219 |
+
## Agent Pipeline
|
| 220 |
+
`Ticket Input` โ `ML Classify` โ `Rule Priority` โ `Department Route` โ `Auto Reply`
|
| 221 |
+
|
| 222 |
+
## API Endpoints (port 7861)
|
| 223 |
+
- `POST /api/reset` โ `{"difficulty": "easy", "seed": 42}`
|
| 224 |
+
- `POST /api/step` โ `{"action": 0}`
|
| 225 |
+
- `POST /api/agent/process` โ `{"text": "my invoice is wrong", "difficulty": "medium"}`
|
| 226 |
+
- `POST /api/agent/classify` โ `{"text": "urgent bug", "difficulty": "easy"}`
|
| 227 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
| 230 |
+
t = threading.Thread(target=run_flask, daemon=True)
|
| 231 |
+
t.start()
|
| 232 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|