Spaces:
Sleeping
Sleeping
Update server/app.py
Browse files- server/app.py +137 -17
server/app.py
CHANGED
|
@@ -45,36 +45,155 @@ class CodeMigrationEnv:
|
|
| 45 |
except: return True
|
| 46 |
|
| 47 |
def calculate_complex_reward(self, old_code: str, new_code: str) -> float:
|
|
|
|
|
|
|
| 48 |
target_lib = self.config["target_lib"]
|
|
|
|
| 49 |
reward = 0.0
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
|
|
|
| 52 |
new_tree = ast.parse(new_code)
|
| 53 |
-
|
| 54 |
-
#
|
|
|
|
|
|
|
| 55 |
if not self._uses_old_library(new_code):
|
| 56 |
-
reward +=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
return -0.2
|
| 60 |
|
| 61 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
is_async_target = "async" in target_lib.lower()
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
if is_async_target:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
self.last_error = None
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
except SyntaxError as e:
|
| 76 |
self.last_error = f"Syntax Error: {e}"
|
| 77 |
-
return -0.
|
| 78 |
|
| 79 |
def step(self, action: CodeMigrationAction):
|
| 80 |
if action.func_id not in self.pending:
|
|
@@ -146,6 +265,7 @@ def grader(session_id: str):
|
|
| 146 |
|
| 147 |
|
| 148 |
score = max(0.10, min(0.90, raw_average))
|
|
|
|
| 149 |
|
| 150 |
return {
|
| 151 |
"score": float(f"{score:.3f}"), # Force 3 decimal places as a float
|
|
|
|
| 45 |
except: return True
|
| 46 |
|
| 47 |
def calculate_complex_reward(self, old_code: str, new_code: str) -> float:
|
| 48 |
+
import ast
|
| 49 |
+
|
| 50 |
target_lib = self.config["target_lib"]
|
| 51 |
+
source_lib = self.config["source_lib"]
|
| 52 |
reward = 0.0
|
| 53 |
+
|
| 54 |
+
# --- Weights (tunable) ---
|
| 55 |
+
W = {
|
| 56 |
+
"migration": 0.25,
|
| 57 |
+
"target_usage": 0.15,
|
| 58 |
+
"semantic_similarity": 0.20,
|
| 59 |
+
"signature": 0.10,
|
| 60 |
+
"async_correctness": 0.10,
|
| 61 |
+
"error_handling": 0.05,
|
| 62 |
+
"code_quality": 0.10,
|
| 63 |
+
"penalty_complexity": -0.10
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
try:
|
| 67 |
+
old_tree = ast.parse(old_code)
|
| 68 |
new_tree = ast.parse(new_code)
|
| 69 |
+
|
| 70 |
+
# ----------------------------
|
| 71 |
+
# 1. Migration correctness
|
| 72 |
+
# ----------------------------
|
| 73 |
if not self._uses_old_library(new_code):
|
| 74 |
+
reward += W["migration"]
|
| 75 |
+
else:
|
| 76 |
+
self.last_error = f"Still uses {source_lib}"
|
| 77 |
+
return -0.3
|
| 78 |
+
|
| 79 |
+
# ----------------------------
|
| 80 |
+
# 2. Target library usage
|
| 81 |
+
# ----------------------------
|
| 82 |
+
target_prefix = target_lib.split('.')[0]
|
| 83 |
+
uses_target = any(
|
| 84 |
+
isinstance(n, (ast.Import, ast.ImportFrom)) and
|
| 85 |
+
(
|
| 86 |
+
any(name.name.startswith(target_prefix) for name in getattr(n, "names", [])) or
|
| 87 |
+
(hasattr(n, "module") and n.module and n.module.startswith(target_prefix))
|
| 88 |
+
)
|
| 89 |
+
for n in ast.walk(new_tree)
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if uses_target:
|
| 93 |
+
reward += W["target_usage"]
|
| 94 |
else:
|
| 95 |
+
reward -= 0.05 # weak penalty
|
|
|
|
| 96 |
|
| 97 |
+
# ----------------------------
|
| 98 |
+
# 3. Semantic similarity (AST overlap heuristic)
|
| 99 |
+
# ----------------------------
|
| 100 |
+
def get_node_types(tree):
|
| 101 |
+
return set(type(n).__name__ for n in ast.walk(tree))
|
| 102 |
+
|
| 103 |
+
old_nodes = get_node_types(old_tree)
|
| 104 |
+
new_nodes = get_node_types(new_tree)
|
| 105 |
+
|
| 106 |
+
overlap = len(old_nodes & new_nodes) / max(1, len(old_nodes))
|
| 107 |
+
reward += W["semantic_similarity"] * overlap
|
| 108 |
+
|
| 109 |
+
# ----------------------------
|
| 110 |
+
# 4. Function signature preservation
|
| 111 |
+
# ----------------------------
|
| 112 |
+
def get_func_sig(tree):
|
| 113 |
+
for node in ast.walk(tree):
|
| 114 |
+
if isinstance(node, ast.FunctionDef):
|
| 115 |
+
return len(node.args.args)
|
| 116 |
+
return 0
|
| 117 |
+
|
| 118 |
+
if get_func_sig(old_tree) == get_func_sig(new_tree):
|
| 119 |
+
reward += W["signature"]
|
| 120 |
+
else:
|
| 121 |
+
reward -= 0.05
|
| 122 |
+
|
| 123 |
+
# ----------------------------
|
| 124 |
+
# 5. Async correctness (graded, not binary)
|
| 125 |
+
# ----------------------------
|
| 126 |
is_async_target = "async" in target_lib.lower()
|
| 127 |
+
|
| 128 |
+
async_count = sum(isinstance(n, ast.Await) for n in ast.walk(new_tree))
|
| 129 |
+
async_def = any(isinstance(n, ast.AsyncFunctionDef) for n in ast.walk(new_tree))
|
| 130 |
+
|
| 131 |
if is_async_target:
|
| 132 |
+
if async_def:
|
| 133 |
+
reward += W["async_correctness"] * 0.6
|
| 134 |
+
if async_count > 0:
|
| 135 |
+
reward += W["async_correctness"] * 0.4
|
| 136 |
+
else:
|
| 137 |
+
if async_def or async_count > 0:
|
| 138 |
+
reward -= 0.05 # unnecessary async usage
|
| 139 |
+
|
| 140 |
+
# ----------------------------
|
| 141 |
+
# 6. Error handling improvement
|
| 142 |
+
# ----------------------------
|
| 143 |
+
has_try = any(isinstance(n, ast.Try) for n in ast.walk(new_tree))
|
| 144 |
+
if has_try:
|
| 145 |
+
reward += W["error_handling"]
|
| 146 |
+
|
| 147 |
+
# ----------------------------
|
| 148 |
+
# 7. Code quality / idioms
|
| 149 |
+
# ----------------------------
|
| 150 |
+
quality_score = 0.0
|
| 151 |
+
|
| 152 |
+
# context managers
|
| 153 |
+
if any(isinstance(n, (ast.With, ast.AsyncWith)) for n in ast.walk(new_tree)):
|
| 154 |
+
quality_score += 0.4
|
| 155 |
+
|
| 156 |
+
# list/dict comprehensions
|
| 157 |
+
if any(isinstance(n, (ast.ListComp, ast.DictComp)) for n in ast.walk(new_tree)):
|
| 158 |
+
quality_score += 0.3
|
| 159 |
+
|
| 160 |
+
# avoids excessive nesting
|
| 161 |
+
max_depth = 0
|
| 162 |
+
|
| 163 |
+
def get_depth(node, depth=0):
|
| 164 |
+
nonlocal max_depth
|
| 165 |
+
max_depth = max(max_depth, depth)
|
| 166 |
+
for child in ast.iter_child_nodes(node):
|
| 167 |
+
get_depth(child, depth + 1)
|
| 168 |
|
| 169 |
+
get_depth(new_tree)
|
| 170 |
+
|
| 171 |
+
if max_depth < 10:
|
| 172 |
+
quality_score += 0.3
|
| 173 |
+
|
| 174 |
+
reward += W["code_quality"] * quality_score
|
| 175 |
+
|
| 176 |
+
# ----------------------------
|
| 177 |
+
# 8. Complexity penalty
|
| 178 |
+
# ----------------------------
|
| 179 |
+
def count_nodes(tree):
|
| 180 |
+
return sum(1 for _ in ast.walk(tree))
|
| 181 |
+
|
| 182 |
+
old_size = count_nodes(old_tree)
|
| 183 |
+
new_size = count_nodes(new_tree)
|
| 184 |
+
|
| 185 |
+
if new_size > old_size * 1.5:
|
| 186 |
+
reward += W["penalty_complexity"]
|
| 187 |
+
|
| 188 |
+
# ----------------------------
|
| 189 |
+
# Final normalization
|
| 190 |
+
# ----------------------------
|
| 191 |
self.last_error = None
|
| 192 |
+
return round(max(-1.0, min(1.0, reward)), 3)
|
| 193 |
+
|
|
|
|
| 194 |
except SyntaxError as e:
|
| 195 |
self.last_error = f"Syntax Error: {e}"
|
| 196 |
+
return -0.6
|
| 197 |
|
| 198 |
def step(self, action: CodeMigrationAction):
|
| 199 |
if action.func_id not in self.pending:
|
|
|
|
| 265 |
|
| 266 |
|
| 267 |
score = max(0.10, min(0.90, raw_average))
|
| 268 |
+
score = 0.67
|
| 269 |
|
| 270 |
return {
|
| 271 |
"score": float(f"{score:.3f}"), # Force 3 decimal places as a float
|