Download main.py from usman-ai-dev/ai-code-maintainability-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/spaces/usman-ai-dev/ai-code-maintainability-engine/resolve/main/main.py
- Command line
-
hf download hf://spaces/usman-ai-dev/ai-code-maintainability-engine/main.py
-
curl -L -o main.py https://huggingface.co/spaces/usman-ai-dev/ai-code-maintainability-engine/resolve/main/main.py
3.07 kB
| import argparse | |
| import sys | |
| import os | |
| try: | |
| sys.stdout.reconfigure(encoding='utf-8') | |
| sys.stderr.reconfigure(encoding='utf-8') | |
| except Exception: | |
| pass | |
| from scoring_api import evaluate | |
| from phase2.optimization import IterativeOptimizer | |
| def display_results(initial_code, result): | |
| print("\n" + "="*60) | |
| print(" π AI CODE MAINTAINABILITY ENGINE (Unified Pipeline) π") | |
| print("="*60) | |
| print("\nπ [ORIGINAL CODE]\n") | |
| print(initial_code.strip()) | |
| # Phase 1: Evaluate initial code directly | |
| print("\nπ [PHASE 1: INITIAL SCORING]") | |
| initial_score_result = evaluate(initial_code) | |
| print(f"Risk Score: {initial_score_result['risk_score']}") | |
| print(f"Risk Level: {initial_score_result['risk_level']}") | |
| if initial_score_result.get('top_risk_factors'): | |
| print("Details:") | |
| for factor in initial_score_result['top_risk_factors']: | |
| print(f" - {factor}") | |
| # Phase 2: Show Optimization Results | |
| print("\nπ οΈ [PHASE 2: AUTONOMOUS REFACTORING]") | |
| if result['iterations_run'] == 0: | |
| print("No optimization was needed (code already meets target maintainability score).") | |
| else: | |
| print(f"Completed {result['iterations_run']} refinement iteration(s).") | |
| print("\n⨠[REFACTORED CODE]\n") | |
| print(result['final_code'].strip()) | |
| print("\nπ [FINAL METRICS]") | |
| print(f"Original Score: {initial_score_result['risk_score']}") | |
| print(f"Final Score: {result['final_score']}") | |
| if result['final_score'] < initial_score_result['risk_score']: | |
| print(f"π Success! Maintainability improved by {initial_score_result['risk_score'] - result['final_score']} points.") | |
| else: | |
| print("β οΈ Code was refactored, maintainability score remained optimal.") | |
| print("\n" + "="*60 + "\n") | |
| def main(): | |
| parser = argparse.ArgumentParser(description="AI Code Maintainability Scoring & Refactoring Engine") | |
| parser.add_argument("file", nargs="?", help="Path to the Python file to analyze and refactor.") | |
| args = parser.parse_args() | |
| if args.file: | |
| if not os.path.exists(args.file): | |
| print(f"Error: File '{args.file}' not found.") | |
| sys.exit(1) | |
| with open(args.file, "r", encoding="utf-8") as f: | |
| code = f.read() | |
| else: | |
| # Default fallback example | |
| code = ''' | |
| def process_data(data_list): | |
| results = [] | |
| for i in range(len(data_list)): | |
| if data_list[i] > 0: | |
| if data_list[i] % 2 == 0: | |
| results.append(data_list[i]) | |
| return results | |
| ''' | |
| print("Initializing Unified Maintainability Engine...") | |
| try: | |
| optimizer = IterativeOptimizer(target_score=10.0, max_iterations=2) | |
| print("Analyzing and Refactoring code...") | |
| result = optimizer.optimize(code) | |
| display_results(code, result) | |
| except Exception as e: | |
| print(f"\nError during engine execution: {e}") | |
| if __name__ == "__main__": | |
| main() | |