TanujInsane commited on
Commit Β·
bd7ba9a
1
Parent(s): d183b62
professional README - badges, architecture, API docs, scoring table
Browse files
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
title: Document Classification
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
|
@@ -7,124 +7,160 @@ sdk: docker
|
|
| 7 |
pinned: false
|
| 8 |
tags:
|
| 9 |
- openenv
|
| 10 |
-
- reinforcement-learning
|
| 11 |
- document-classification
|
|
|
|
| 12 |
- gymnasium
|
| 13 |
-
- pytorch
|
| 14 |
---
|
| 15 |
|
| 16 |
-
# Document Classification Environment
|
| 17 |
|
| 18 |
**OpenEnv submission for Meta x PyTorch Hackathon**
|
| 19 |
|
| 20 |
-
|
| 21 |
-
[](https://python.org)
|
| 22 |
-
[](https://gymnasium.farama.org)
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
```python
|
| 32 |
|
| 33 |
-
$readme = @'
|
| 34 |
-
---
|
| 35 |
-
title: Document Classification Environment
|
| 36 |
-
emoji: π
|
| 37 |
-
colorFrom: blue
|
| 38 |
-
colorTo: green
|
| 39 |
-
sdk: docker
|
| 40 |
-
pinned: false
|
| 41 |
-
tags:
|
| 42 |
-
- openenv
|
| 43 |
-
- reinforcement-learning
|
| 44 |
-
- document-classification
|
| 45 |
-
- gymnasium
|
| 46 |
-
- pytorch
|
| 47 |
---
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
**OpenEnv submission for Meta x PyTorch Hackathon**
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
## Quick Start
|
| 64 |
```python
|
| 65 |
from environment import DocumentClassificationEnv
|
| 66 |
|
| 67 |
-
env = DocumentClassificationEnv("hard")
|
| 68 |
obs, info = env.reset()
|
| 69 |
|
| 70 |
while True:
|
| 71 |
-
action = your_agent(obs)
|
| 72 |
-
obs, reward, done,
|
|
|
|
| 73 |
if done:
|
| 74 |
print(info["episode_summary"])
|
| 75 |
break
|
| 76 |
```
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
##
|
| 81 |
|
| 82 |
-
|
|
| 83 |
-
|-------|-----------|-----------|-----------|
|
| 84 |
-
| Easy | 5 |
|
| 85 |
-
| Medium | 10 |
|
| 86 |
-
| Hard | 22 | 1000 |
|
| 87 |
|
| 88 |
-
###
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|-------|------|-------------|
|
| 92 |
-
| content | str | Document text (with noise) |
|
| 93 |
-
| features | array[100] | Normalized TF-IDF features |
|
| 94 |
-
| word_count | int | Document length |
|
| 95 |
-
| has_urgency_markers | bool | Urgency signal |
|
| 96 |
-
| true_category | str | Structural category signal |
|
| 97 |
|
| 98 |
-
##
|
| 99 |
-
Discrete(N) where N = number of categories for difficulty level
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|---------|--------|
|
| 104 |
-
| Exact match | 1.00 |
|
| 105 |
-
| Parent category match | 0.50 |
|
| 106 |
-
| Wrong category | 0.00 |
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|------|-------------|
|
| 116 |
-
| environment.py | Core Gymnasium environment |
|
| 117 |
-
| tasks.py | Document generation with adversarial noise |
|
| 118 |
-
| grading.py | Hierarchical partial credit scoring |
|
| 119 |
-
| baseline_inference.py | Structural feature agent (1.0 accuracy) |
|
| 120 |
-
| app.py | Interactive Gradio demo |
|
| 121 |
-
| openenv.yaml | Environment specification |
|
| 122 |
|
| 123 |
-
##
|
| 124 |
|
| 125 |
-
|
| 126 |
-
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Document Classification Env
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
|
|
|
| 7 |
pinned: false
|
| 8 |
tags:
|
| 9 |
- openenv
|
|
|
|
| 10 |
- document-classification
|
| 11 |
+
- reinforcement-learning
|
| 12 |
- gymnasium
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# π Document Classification Environment
|
| 16 |
|
| 17 |
**OpenEnv submission for Meta x PyTorch Hackathon**
|
| 18 |
|
| 19 |
+
An interactive Gymnasium-compatible environment where AI agents learn to classify and route customer support tickets to the correct department.
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
---
|
| 22 |
|
| 23 |
+
## π― Real-World Task
|
| 24 |
|
| 25 |
+
Customer support teams receive hundreds of tickets daily β billing issues, technical bugs, HR complaints, legal queries. This environment simulates that routing challenge, training agents to read a document and instantly decide which department should handle it.
|
| 26 |
|
| 27 |
+
**Why this matters:** Misrouted tickets waste time and frustrate customers. A well-trained agent can reduce misrouting by 80%+.
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
---
|
| 30 |
|
| 31 |
+
## ποΈ Environment Design
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
```
|
| 34 |
+
DocumentClassificationEnv(task_difficulty="hard", seed=42)
|
| 35 |
+
βββ observation_space: Dict
|
| 36 |
+
β βββ content: Text (the document)
|
| 37 |
+
β βββ document_id: Text
|
| 38 |
+
β βββ word_count: Box(1,)
|
| 39 |
+
β βββ has_urgency_markers: MultiBinary(1)
|
| 40 |
+
β βββ features: Box(100,) β TF-IDF features
|
| 41 |
+
β βββ document_index: Box(1,)
|
| 42 |
+
β βββ total_documents: Box(1,)
|
| 43 |
+
βββ action_space: Discrete(N) β N = num categories
|
| 44 |
+
```
|
| 45 |
|
| 46 |
+
### API
|
| 47 |
|
|
|
|
| 48 |
```python
|
| 49 |
from environment import DocumentClassificationEnv
|
| 50 |
|
| 51 |
+
env = DocumentClassificationEnv(task_difficulty="hard", seed=42)
|
| 52 |
obs, info = env.reset()
|
| 53 |
|
| 54 |
while True:
|
| 55 |
+
action = your_agent(obs) # int: category index
|
| 56 |
+
obs, reward, done, _, info = env.step(action)
|
| 57 |
+
print(f"Reward: {reward:.3f}, Correct: {info['is_correct']}")
|
| 58 |
if done:
|
| 59 |
print(info["episode_summary"])
|
| 60 |
break
|
| 61 |
```
|
| 62 |
|
| 63 |
+
---
|
| 64 |
|
| 65 |
+
## π Three Progressive Tasks
|
| 66 |
|
| 67 |
+
| Task | Categories | Documents | Time Limit | Target Score |
|
| 68 |
+
|------|-----------|-----------|------------|--------------|
|
| 69 |
+
| **Easy** | 5 | 100 | None | 0.85 |
|
| 70 |
+
| **Medium** | 10 | 500 | 2 sec | 0.75 |
|
| 71 |
+
| **Hard** | 22 | 1000 | 1 sec | 0.75 |
|
| 72 |
|
| 73 |
+
### Categories (Hard Mode β 22 total)
|
| 74 |
+
`General` `Billing` `Billing-Dispute` `Billing-Refund` `Support` `Support-Urgent` `Support-Normal` `Technical` `Technical-Bug` `Technical-Feature` `HR` `HR-Payroll` `HR-Benefits` `HR-Complaint` `Legal` `Legal-Contract` `Legal-Compliance` `Executive` `Executive-Strategic` `Finance` `Marketing` `Operations`
|
| 75 |
|
| 76 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
## π Reward Function
|
|
|
|
| 79 |
|
| 80 |
+
```python
|
| 81 |
+
reward = accuracy_reward + speed_bonus
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# accuracy_reward:
|
| 84 |
+
# +1.0 for correct classification
|
| 85 |
+
# -0.5 for wrong classification
|
| 86 |
|
| 87 |
+
# speed_bonus (difficulty-dependent):
|
| 88 |
+
# Easy: +0.10 if < 100ms
|
| 89 |
+
# Medium: +0.15 if < 200ms, +0.10 if < 500ms
|
| 90 |
+
# Hard: +0.20 if < 100ms, +0.10 if < 300ms
|
| 91 |
+
```
|
| 92 |
|
| 93 |
+
Partial credit via speed bonus encourages efficient inference, not just accuracy.
|
| 94 |
|
| 95 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
## π Baseline Results (Keyword Agent)
|
| 98 |
|
| 99 |
+
| Task | Score | Notes |
|
| 100 |
+
|------|-------|-------|
|
| 101 |
+
| EASY | **0.77** | Simple keyword matching |
|
| 102 |
+
| MEDIUM | **0.89** | Priority-ordered keywords |
|
| 103 |
+
| HARD | **0.165** | 22 categories, harder to distinguish |
|
| 104 |
+
|
| 105 |
+
Better agents (TF-IDF similarity, fine-tuned LLM) can significantly beat baseline.
|
| 106 |
+
|
| 107 |
+
---
|
| 108 |
+
|
| 109 |
+
## π Quick Start
|
| 110 |
+
|
| 111 |
+
```bash
|
| 112 |
+
git clone https://huggingface.co/spaces/TanujInsane/document-classification-env
|
| 113 |
+
cd document-classification-env
|
| 114 |
+
pip install -r requirements.txt
|
| 115 |
+
|
| 116 |
+
# Run baseline
|
| 117 |
+
python baseline_inference.py --task all --output results.json
|
| 118 |
+
|
| 119 |
+
# Launch UI
|
| 120 |
+
python app.py
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## π File Structure
|
| 126 |
+
|
| 127 |
+
```
|
| 128 |
+
βββ environment.py # Main Gymnasium environment
|
| 129 |
+
βββ tasks.py # Document generation + TF-IDF features
|
| 130 |
+
βββ grading.py # Scoring logic (0.0 - 1.0)
|
| 131 |
+
βββ baseline_inference.py # Keyword-based baseline agent
|
| 132 |
+
βββ app.py # Gradio interactive demo
|
| 133 |
+
βββ test_environment.py # 5 unit tests (all passing β
)
|
| 134 |
+
βββ openenv.yaml # OpenEnv specification
|
| 135 |
+
βββ Dockerfile # Container deployment
|
| 136 |
+
βββ requirements.txt # Dependencies
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
## π¬ Reproducibility
|
| 142 |
+
|
| 143 |
+
- Seed-controlled document generation
|
| 144 |
+
- Fixed test sets for fair grading
|
| 145 |
+
- Deterministic reward calculation
|
| 146 |
+
- Docker containerization for consistent deployment
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
|
| 150 |
+
## π‘ Improving Beyond Baseline
|
| 151 |
+
|
| 152 |
+
```python
|
| 153 |
+
# Example: TF-IDF similarity agent (beats keyword matching)
|
| 154 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 155 |
+
|
| 156 |
+
class TFIDFAgent:
|
| 157 |
+
def __init__(self, difficulty):
|
| 158 |
+
self.env = DocumentClassificationEnv(difficulty)
|
| 159 |
+
# Pre-compute category centroid vectors
|
| 160 |
+
# Use cosine similarity at inference time
|
| 161 |
+
...
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
---
|
| 165 |
|
| 166 |
+
*Built for Meta x PyTorch OpenEnv Hackathon | Gymnasium-compatible | Docker deployed*
|