Download test_environment.py from TanujInsane/document-classification-env: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/spaces/TanujInsane/document-classification-env/resolve/main/test_environment.py
- Command line
-
hf download hf://spaces/TanujInsane/document-classification-env/test_environment.py
-
curl -L -o test_environment.py https://huggingface.co/spaces/TanujInsane/document-classification-env/resolve/main/test_environment.py
3.45 kB
| import sys | |
| import numpy as np | |
| from environment import DocumentClassificationEnv | |
| from grading import AgentGrader | |
| from models import Observation, Action, Reward, EnvironmentState | |
| from agent import TicketAgent | |
| def test_environment_creation(): | |
| print("Testing environment creation...") | |
| for difficulty in ["easy", "medium", "hard"]: | |
| try: | |
| env = DocumentClassificationEnv(task_difficulty=difficulty) | |
| obs, info = env.reset() | |
| print(f" β {difficulty} environment created successfully") | |
| assert "features" in obs, f"Missing features in {difficulty} observation" | |
| assert "content" in obs, f"Missing content in {difficulty} observation" | |
| except Exception as e: | |
| print(f" β Failed to create {difficulty} environment: {e}") | |
| return False | |
| return True | |
| def test_step_function(): | |
| print("\nTesting step function...") | |
| env = DocumentClassificationEnv(task_difficulty="easy") | |
| obs, _ = env.reset() | |
| for i in range(5): | |
| action = i % env.num_categories | |
| obs, reward, done, truncated, info = env.step(action) | |
| assert isinstance(reward, float), f"Reward should be float, got {type(reward)}" | |
| assert "is_correct" in info, "Missing is_correct in info" | |
| print(f" β Step {i+1}: reward={reward:.3f}, accuracy={info.get('episode_accuracy', 0):.3f}") | |
| if done: break | |
| return True | |
| def test_ticket_agent_loop(): | |
| print("\nTesting TicketAgent interaction loop...") | |
| agent = TicketAgent(model_dir=".") | |
| env = DocumentClassificationEnv(task_difficulty="easy") | |
| state = agent.run_episode(env) | |
| print(f" β Agent finished episode. Accuracy: {state['accuracy']:.2%}, Total Reward: {state['total_reward']:.2f}") | |
| assert state['total_documents'] > 0 | |
| assert 'accuracy' in state | |
| return True | |
| def test_pydantic_models(): | |
| print("\nTesting Pydantic typed models...") | |
| env = DocumentClassificationEnv(task_difficulty="easy") | |
| obs_raw, _ = env.reset() | |
| obs_typed = Observation.from_gym_obs(obs_raw) | |
| assert obs_typed.document_id != "" | |
| print(f" β Observation model: doc_id={obs_typed.document_id}") | |
| state_raw = env.state() | |
| state_typed = EnvironmentState.from_state_dict(state_raw) | |
| assert state_typed.difficulty == "easy" | |
| print(f" β EnvironmentState model: difficulty={state_typed.difficulty}, accuracy={state_typed.accuracy:.3f}") | |
| return True | |
| def run_all_tests(): | |
| print("=" * 60) | |
| print("Document Classification Environment - Fixed Test Suite") | |
| print("=" * 60) | |
| tests = [ | |
| ("Environment Creation", test_environment_creation), | |
| ("Step Function", test_step_function), | |
| ("TicketAgent Loop", test_ticket_agent_loop), | |
| ("Pydantic Models", test_pydantic_models), | |
| ] | |
| results = [] | |
| for name, func in tests: | |
| try: | |
| res = func() | |
| results.append((name, res)) | |
| except Exception as e: | |
| print(f" β {name} failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| results.append((name, False)) | |
| print("\nSUMMARY") | |
| passed = sum(1 for _, r in results if r) | |
| for name, r in results: | |
| print(f" [{'PASS' if r else 'FAIL'}] {name}") | |
| print(f"\nTotal: {passed}/{len(tests)} passed") | |
| return passed == len(tests) | |
| if __name__ == "__main__": | |
| success = run_all_tests() | |
| sys.exit(0 if success else 1) | |