Update app.py
Browse files
app.py
CHANGED
@@ -12,30 +12,32 @@ import tempfile
|
|
12 |
|
13 |
import tempfile
|
14 |
|
|
|
|
|
15 |
def review_code(code_snippet):
|
16 |
print("β
Received Code:", code_snippet) # Debugging log
|
17 |
|
18 |
-
# β
|
19 |
prompt = f"""
|
20 |
### Instruction:
|
21 |
-
You are a Python code reviewer. Your job is to analyze and fix errors in the provided Python code.
|
22 |
Make necessary corrections such as adding missing return statements, fixing syntax errors, and correcting logical mistakes.
|
23 |
-
|
24 |
|
25 |
### Input Code:
|
26 |
{code_snippet}
|
27 |
|
28 |
-
###
|
29 |
"""
|
30 |
|
31 |
# Process input
|
32 |
inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move to CPU
|
33 |
outputs = model.generate(
|
34 |
**inputs,
|
35 |
-
max_new_tokens=50, # β
|
36 |
do_sample=False,
|
37 |
-
num_beams=4, # β
|
38 |
-
repetition_penalty=
|
39 |
)
|
40 |
|
41 |
# Check if the model generated output
|
@@ -43,9 +45,13 @@ Do NOT generate new functions or extra textβonly return the fixed version of t
|
|
43 |
print("β Model did not generate output!") # Debugging log
|
44 |
return "Error: Model did not generate output."
|
45 |
|
46 |
-
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
47 |
print("β
Generated Code:", reviewed_code) # Debugging log
|
48 |
|
|
|
|
|
|
|
|
|
49 |
# β
Write reviewed code to a temporary file for download
|
50 |
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
51 |
with open(temp_file_path, "w") as temp_file:
|
|
|
12 |
|
13 |
import tempfile
|
14 |
|
15 |
+
import tempfile
|
16 |
+
|
17 |
def review_code(code_snippet):
|
18 |
print("β
Received Code:", code_snippet) # Debugging log
|
19 |
|
20 |
+
# β
Improved prompt with strict format rules
|
21 |
prompt = f"""
|
22 |
### Instruction:
|
23 |
+
You are a Python code reviewer. Your job is to analyze and fix errors in the provided Python code.
|
24 |
Make necessary corrections such as adding missing return statements, fixing syntax errors, and correcting logical mistakes.
|
25 |
+
ONLY return the corrected function definitionβDO NOT generate any new function calls or explanations.
|
26 |
|
27 |
### Input Code:
|
28 |
{code_snippet}
|
29 |
|
30 |
+
### Corrected Function:
|
31 |
"""
|
32 |
|
33 |
# Process input
|
34 |
inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move to CPU
|
35 |
outputs = model.generate(
|
36 |
**inputs,
|
37 |
+
max_new_tokens=50, # β
Limits AI output to prevent hallucinations
|
38 |
do_sample=False,
|
39 |
+
num_beams=4, # β
Higher beams for better correction quality
|
40 |
+
repetition_penalty=3.0 # β
Stronger penalty to prevent repeated tokens
|
41 |
)
|
42 |
|
43 |
# Check if the model generated output
|
|
|
45 |
print("β Model did not generate output!") # Debugging log
|
46 |
return "Error: Model did not generate output."
|
47 |
|
48 |
+
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
49 |
print("β
Generated Code:", reviewed_code) # Debugging log
|
50 |
|
51 |
+
# β
Ensure AI output starts with "def" to guarantee it returned a function
|
52 |
+
if not reviewed_code.startswith("def"):
|
53 |
+
return "Error: AI did not return a valid function."
|
54 |
+
|
55 |
# β
Write reviewed code to a temporary file for download
|
56 |
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
57 |
with open(temp_file_path, "w") as temp_file:
|