Download phase2/selector.py from usman-ai-dev/ai-code-maintainability-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/spaces/usman-ai-dev/ai-code-maintainability-engine/resolve/main/phase2/selector.py
- Command line
-
hf download hf://spaces/usman-ai-dev/ai-code-maintainability-engine/phase2/selector.py
-
curl -L -o selector.py https://huggingface.co/spaces/usman-ai-dev/ai-code-maintainability-engine/resolve/main/phase2/selector.py
1.35 kB
| import os | |
| import sys | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| parent_dir = os.path.dirname(current_dir) | |
| if parent_dir not in sys.path: | |
| sys.path.append(parent_dir) | |
| try: | |
| from scoring_api import evaluate | |
| except ImportError as e: | |
| print(f"Error: Unable to import Phase 1 'scoring_api'. Ensure it exists in the parent directory.") | |
| print(e) | |
| def evaluate(code): | |
| return {'error': True, 'message': 'Phase 1 missing.'} | |
| from typing import List, Dict, Any | |
| class CandidateSelector: | |
| def select_best(self, candidates: List[Dict[str, Any]]) -> Dict[str, Any]: | |
| if not candidates: | |
| return None | |
| evaluated_candidates = [] | |
| for cand in candidates: | |
| code = cand['code'] | |
| result = evaluate(code) | |
| if not result.get('error'): | |
| evaluated_candidates.append({'candidate_data': cand, 'score_result': result, 'risk_score': result.get('risk_score', 100)}) | |
| else: | |
| print(f"Warning: Scoring failed for a candidate: {result.get('message')}") | |
| if not evaluated_candidates: | |
| print('Warning: All candidates failed during Phase 1 evaluation.') | |
| return None | |
| best_candidate = min(evaluated_candidates, key=lambda c: c['risk_score']) | |
| return best_candidate |