Shivaaaahdjdnd/code_analysis
0
1"""2🔧 Configuration file for Qwen2.5-1.5B Coding Analysis System3"""4 5import os6from dataclasses import dataclass, field7from typing import Dict, List8 9 10@dataclass11class ModelConfig:12 """AI Model Configuration"""13 model_name: str = "Qwen/Qwen2.5-1.5B-Instruct"14 max_new_tokens: int = 60015 temperature: float = 0.116 top_p: float = 0.917 repetition_penalty: float = 1.118 do_sample: bool = True19 torch_dtype: str = "float32"20 device_map: str = "auto"21 trust_remote_code: bool = True22 23 24@dataclass25class ScoringConfig:26 """Scoring System Configuration"""27 max_correctness: int = 4028 max_code_quality: int = 2529 max_efficiency: int = 2030 max_ai_penalty: int = 1031 max_similarity_penalty: int = 532 33 # Quality sub-scores34 readability_max: int = 535 structure_max: int = 536 naming_max: int = 537 comments_max: int = 538 best_practices_max: int = 539 40 41@dataclass42class ServerConfig:43 """Server Configuration"""44 host: str = "0.0.0.0"45 port: int = 786046 debug: bool = False47 cors_enabled: bool = True48 49 50@dataclass51class AnalysisConfig:52 """Analysis Configuration"""53 supported_languages: List[str] = None54 test_timeout: int = 555 max_code_length: int = 1000056 enable_ai_detection: bool = True57 enable_similarity_check: bool = True58 59 def __post_init__(self):60 if self.supported_languages is None:61 self.supported_languages = ["python", "java", "cpp", "javascript"]62 63 64class Config:65 """Main Configuration Class"""66 67 def __init__(self):68 self.model = ModelConfig()69 self.scoring = ScoringConfig()70 self.server = ServerConfig()71 self.analysis = AnalysisConfig()72 self._load_from_env()73 74 def _load_from_env(self):75 """Load configuration from environment variables"""76 self.server.host = os.getenv("HOST", self.server.host)77 self.server.port = int(os.getenv("PORT", self.server.port))78 self.server.debug = os.getenv("DEBUG", "false").lower() == "true"79 80 # Always default to Qwen2.5-1.5B-Instruct81 self.model.model_name = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-1.5B-Instruct")82 self.model.max_new_tokens = int(os.getenv("MAX_NEW_TOKENS", self.model.max_new_tokens))83 self.model.temperature = float(os.getenv("TEMPERATURE", self.model.temperature))84 85 self.analysis.test_timeout = int(os.getenv("TEST_TIMEOUT", self.analysis.test_timeout))86 self.analysis.enable_ai_detection = os.getenv("ENABLE_AI_DETECTION", "true").lower() == "true"87 self.analysis.enable_similarity_check = os.getenv("ENABLE_SIMILARITY_CHECK", "true").lower() == "true"88 89 90# Global configuration instance91config = Config()92 93# Complexity scoring mapping94COMPLEXITY_SCORES = {95 "O(1)": 20,96 "O(log n)": 18,97 "O(n)": 15,98 "O(n log n)": 12,99 "O(n²)": 8,100 "O(n³)": 4,101 "O(2^n)": 2,102 "O(n!)": 1103}104 105# AI detection patterns106AI_PATTERNS = [107 r"# This function",108 r"# Initialize",109 r"# Check if",110 r"# Return the result",111 r"# Edge case",112 r"# Base case"113]114 115# Language-specific configurations116LANGUAGE_CONFIG = {117 "python": {118 "file_extension": ".py",119 "comment_prefix": "#",120 "function_pattern": r"def\s+(\w+)",121 "class_pattern": r"class\s+(\w+)"122 },123 "java": {124 "file_extension": ".java",125 "comment_prefix": "//",126 "function_pattern": r"public\s+\w+\s+(\w+)\s*\(",127 "class_pattern": r"class\s+(\w+)"128 },129 "cpp": {130 "file_extension": ".cpp",131 "comment_prefix": "//",132 "function_pattern": r"\w+\s+(\w+)\s*\(",133 "class_pattern": r"class\s+(\w+)"134 },135 "javascript": {136 "file_extension": ".js",137 "comment_prefix": "//",138 "function_pattern": r"function\s+(\w+)",139 "class_pattern": r"class\s+(\w+)"140 }141}142 143# Difficulty multipliers144DIFFICULTY_MULTIPLIERS = {145 "easy": 1.0,146 "medium": 1.1,147 "hard": 1.2148}149 150 151def get_language_config(language: str) -> Dict:152 """Get configuration for specific programming language"""153 return LANGUAGE_CONFIG.get(language.lower(), LANGUAGE_CONFIG["python"])154 155 156def get_complexity_score(complexity: str) -> int:157 """Get score for time complexity"""158 return COMPLEXITY_SCORES.get(complexity, 5)159 160 161def get_difficulty_multiplier(difficulty: str) -> float:162 """Get multiplier for difficulty level"""163 return DIFFICULTY_MULTIPLIERS.get(difficulty.lower(), 1.0)