Download app.py from TanujInsane/document-classification-env: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/spaces/TanujInsane/document-classification-env/resolve/main/app.py
- Command line
-
hf download hf://spaces/TanujInsane/document-classification-env/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TanujInsane/document-classification-env/resolve/main/app.py
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 = {} | |
| 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()}}) | |
| 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 | |
| }) | |
| 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}) | |
| 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) | |
| 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) | |