ayushraj2349-2 commited on
Commit
5590ae6
·
verified ·
1 Parent(s): 6bb18d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -8,20 +8,32 @@ model_name = "Salesforce/codegen-350M-mono" # Fastest model for code review
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(model_name).to("cpu") # Force CPU mode
10
 
 
 
11
  def review_code(code_snippet):
12
  print("✅ Received Code:", code_snippet) # Debugging log
13
 
14
- # ✅ Add a clear instruction to the model
15
- prompt = f"### Instruction: Review and fix the Python code below.\n### Input Code:\n{code_snippet}\n### Fixed Code:\n"
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Process input
18
  inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move to CPU
19
  outputs = model.generate(
20
  **inputs,
21
- max_length=80, # ✅ Increased length for full function review
22
  do_sample=False,
23
- num_beams=5, # ✅ Higher beams for better decision-making
24
- repetition_penalty=1.8 # ✅ Lower penalty to avoid weird token removal
25
  )
26
 
27
  # Check if the model generated output
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(model_name).to("cpu") # Force CPU mode
10
 
11
+ import tempfile
12
+
13
  def review_code(code_snippet):
14
  print("✅ Received Code:", code_snippet) # Debugging log
15
 
16
+ # ✅ Better instruction prompt
17
+ prompt = f"""
18
+ ### Instruction:
19
+ You are a Python code reviewer. Your job is to analyze and fix errors in the provided Python code.
20
+ Make necessary corrections such as adding missing return statements, fixing syntax errors, and correcting logical mistakes.
21
+ Do NOT generate new functions or extra text—only return the fixed version of the provided code.
22
+
23
+ ### Input Code:
24
+ {code_snippet}
25
+
26
+ ### Reviewed Code:
27
+ """
28
 
29
  # Process input
30
  inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move to CPU
31
  outputs = model.generate(
32
  **inputs,
33
+ max_length=60, # ✅ Keeps response concise & correct
34
  do_sample=False,
35
+ num_beams=4, # ✅ Ensures better correction quality
36
+ repetition_penalty=2.5 # ✅ Prevents repeated/unnecessary output
37
  )
38
 
39
  # Check if the model generated output