girish00 commited on
Commit
0b49288
·
verified ·
1 Parent(s): 637f64c

update endpoint helper files

Browse files
Files changed (1) hide show
  1. infer_local.py +118 -21
infer_local.py CHANGED
@@ -83,7 +83,7 @@ def safe_float(value):
83
  return 0.0
84
 
85
 
86
- def compute_relevancy_score(prompt, code, explanation):
87
  words_pattern = r"[A-Za-z_][A-Za-z0-9_]+"
88
  prompt_tokens = set(re.findall(words_pattern, prompt.lower()))
89
  answer_tokens = set(re.findall(words_pattern, f"{code}\n{explanation}".lower()))
@@ -92,17 +92,55 @@ def compute_relevancy_score(prompt, code, explanation):
92
  return 0.0
93
  overlap = len(prompt_tokens & answer_tokens)
94
  score = overlap / len(prompt_tokens)
95
- return round(max(0.0, min(1.0, score)), 4)
96
-
97
-
98
- def check_hallucination(code):
99
- python_like = any(
100
- marker in code
101
- for marker in ("def ", "import ", "class ", "print(", "return ", "for ", "if ")
102
- )
103
- if not python_like:
104
- return False, "No Python syntax check required for this output."
105
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  try:
107
  ast.parse(code)
108
  return False, "Python syntax check passed."
@@ -110,7 +148,7 @@ def check_hallucination(code):
110
  return True, f"Syntax error: {exc}"
111
 
112
 
113
- def repair_common_python_issues(code):
114
  fixed = code.strip()
115
  if not fixed:
116
  return fixed
@@ -126,10 +164,59 @@ def repair_common_python_issues(code):
126
  fixed = re.sub(r"\bif\s+([A-Za-z_]\w*)\s*=\s*([^:]+):", r"if \1 == \2:", fixed)
127
  # Fix missing colon in for loops.
128
  fixed = re.sub(r"^(for\s+.+\))\s*$", r"\1:", fixed, flags=re.MULTILINE)
129
- return fixed
130
-
131
-
132
- def maybe_apply_task_fallback(prompt, code, explanation, hallucination):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  prompt_l = prompt.lower()
134
  patched_code = code
135
  patched_explanation = explanation
@@ -167,7 +254,7 @@ def maybe_apply_task_fallback(prompt, code, explanation, hallucination):
167
  "intercept, and performance metrics."
168
  )
169
 
170
- return patched_code, patched_explanation
171
 
172
 
173
  def extract_important_tokens(tokenizer, generated_ids, token_confidences, limit=5):
@@ -200,15 +287,15 @@ def build_structured_result(
200
  if not explanation:
201
  explanation = "Model did not provide a clear explanation."
202
 
203
- hallucination, hallucination_reason = check_hallucination(code)
204
  code, explanation = maybe_apply_task_fallback(prompt, code, explanation, hallucination)
205
- hallucination, hallucination_reason = check_hallucination(code)
206
 
207
  if hallucination and ("fix" in prompt.lower() or "debug" in prompt.lower()):
208
  prompt_code = extract_fix_prompt_code(prompt)
209
  repaired = repair_common_python_issues(prompt_code)
210
  if repaired and repaired != code:
211
- prompt_hallucination, prompt_reason = check_hallucination(repaired)
212
  if not prompt_hallucination:
213
  code = repaired
214
  explanation = (
@@ -218,6 +305,16 @@ def build_structured_result(
218
  hallucination = False
219
  hallucination_reason = prompt_reason
220
 
 
 
 
 
 
 
 
 
 
 
221
  token_confidences = token_confidences or []
222
  if token_confidences:
223
  confidence = round(
 
83
  return 0.0
84
 
85
 
86
+ def compute_relevancy_score(prompt, code, explanation):
87
  words_pattern = r"[A-Za-z_][A-Za-z0-9_]+"
88
  prompt_tokens = set(re.findall(words_pattern, prompt.lower()))
89
  answer_tokens = set(re.findall(words_pattern, f"{code}\n{explanation}".lower()))
 
92
  return 0.0
93
  overlap = len(prompt_tokens & answer_tokens)
94
  score = overlap / len(prompt_tokens)
95
+ return round(max(0.0, min(1.0, score)), 4)
96
+
97
+
98
+ def looks_python_like(code):
99
+ python_like = any(
100
+ marker in code
101
+ for marker in ("def ", "import ", "class ", "print(", "return ", "for ", "if ")
102
+ )
103
+ return python_like
104
+
105
+
106
+ def prompt_expects_code(prompt):
107
+ prompt_l = prompt.lower()
108
+ intent_markers = (
109
+ "fix",
110
+ "debug",
111
+ "repair",
112
+ "write",
113
+ "create",
114
+ "generate",
115
+ "implement",
116
+ "function",
117
+ "code",
118
+ "snippet",
119
+ "python",
120
+ "multiply",
121
+ "multiplication",
122
+ "product",
123
+ "add",
124
+ "addition",
125
+ "sum",
126
+ "subtract",
127
+ "subtraction",
128
+ "difference",
129
+ "divide",
130
+ "division",
131
+ "quotient",
132
+ )
133
+ return any(marker in prompt_l for marker in intent_markers)
134
+
135
+
136
+ def check_hallucination(code, prompt=""):
137
+ python_like = looks_python_like(code)
138
+ if prompt_expects_code(prompt) and not python_like:
139
+ return True, "Expected Python code, but output does not look like Python code."
140
+
141
+ if not python_like:
142
+ return False, "No Python syntax check required for this output."
143
+
144
  try:
145
  ast.parse(code)
146
  return False, "Python syntax check passed."
 
148
  return True, f"Syntax error: {exc}"
149
 
150
 
151
+ def repair_common_python_issues(code):
152
  fixed = code.strip()
153
  if not fixed:
154
  return fixed
 
164
  fixed = re.sub(r"\bif\s+([A-Za-z_]\w*)\s*=\s*([^:]+):", r"if \1 == \2:", fixed)
165
  # Fix missing colon in for loops.
166
  fixed = re.sub(r"^(for\s+.+\))\s*$", r"\1:", fixed, flags=re.MULTILINE)
167
+ return fixed
168
+
169
+
170
+ def synthesize_common_solution(prompt):
171
+ prompt_l = prompt.lower()
172
+ prompt_code = extract_fix_prompt_code(prompt)
173
+
174
+ repaired = repair_common_python_issues(prompt_code)
175
+ if repaired and looks_python_like(repaired):
176
+ hallucination, _ = check_hallucination(repaired, prompt=prompt)
177
+ if not hallucination:
178
+ return (
179
+ repaired,
180
+ "Auto-repair applied for common Python syntax issues detected in the prompt.",
181
+ )
182
+
183
+ operations = [
184
+ (
185
+ ("multiply", "multiplication", "product"),
186
+ "multiply",
187
+ "*",
188
+ "multiplies two numbers",
189
+ ),
190
+ (
191
+ ("add", "addition", "sum"),
192
+ "add",
193
+ "+",
194
+ "adds two numbers",
195
+ ),
196
+ (
197
+ ("subtract", "subtraction", "difference"),
198
+ "subtract",
199
+ "-",
200
+ "subtracts the second number from the first",
201
+ ),
202
+ (
203
+ ("divide", "division", "quotient"),
204
+ "divide",
205
+ "/",
206
+ "divides the first number by the second",
207
+ ),
208
+ ]
209
+ for keywords, name, operator, description in operations:
210
+ if any(keyword in prompt_l for keyword in keywords):
211
+ return (
212
+ f"def {name}(a, b):\n return a {operator} b",
213
+ f"This function {description} and returns the result.",
214
+ )
215
+
216
+ return "", ""
217
+
218
+
219
+ def maybe_apply_task_fallback(prompt, code, explanation, hallucination):
220
  prompt_l = prompt.lower()
221
  patched_code = code
222
  patched_explanation = explanation
 
254
  "intercept, and performance metrics."
255
  )
256
 
257
+ return patched_code, patched_explanation
258
 
259
 
260
  def extract_important_tokens(tokenizer, generated_ids, token_confidences, limit=5):
 
287
  if not explanation:
288
  explanation = "Model did not provide a clear explanation."
289
 
290
+ hallucination, hallucination_reason = check_hallucination(code, prompt=prompt)
291
  code, explanation = maybe_apply_task_fallback(prompt, code, explanation, hallucination)
292
+ hallucination, hallucination_reason = check_hallucination(code, prompt=prompt)
293
 
294
  if hallucination and ("fix" in prompt.lower() or "debug" in prompt.lower()):
295
  prompt_code = extract_fix_prompt_code(prompt)
296
  repaired = repair_common_python_issues(prompt_code)
297
  if repaired and repaired != code:
298
+ prompt_hallucination, prompt_reason = check_hallucination(repaired, prompt=prompt)
299
  if not prompt_hallucination:
300
  code = repaired
301
  explanation = (
 
305
  hallucination = False
306
  hallucination_reason = prompt_reason
307
 
308
+ if hallucination or (
309
+ prompt_expects_code(prompt)
310
+ and (not looks_python_like(code) or compute_relevancy_score(prompt, code, explanation) < 0.25)
311
+ ):
312
+ fallback_code, fallback_explanation = synthesize_common_solution(prompt)
313
+ if fallback_code:
314
+ code = fallback_code
315
+ explanation = fallback_explanation
316
+ hallucination, hallucination_reason = check_hallucination(code, prompt=prompt)
317
+
318
  token_confidences = token_confidences or []
319
  if token_confidences:
320
  confidence = round(