ayushraj2349-2 commited on
Commit
850a3e0
Β·
verified Β·
1 Parent(s): 09197ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
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
- # βœ… Better instruction prompt
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
- Do NOT generate new functions or extra textβ€”only return the fixed version of the provided code.
24
 
25
  ### Input Code:
26
  {code_snippet}
27
 
28
- ### Reviewed Code:
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, # βœ… Generate only 50 new tokens (fixes warning)
36
  do_sample=False,
37
- num_beams=4, # βœ… Ensures better correction quality
38
- repetition_penalty=2.5 # βœ… Prevents repeated/unnecessary output
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: