ayushraj2349-2 commited on
Commit
6bb18d3
Β·
verified Β·
1 Parent(s): 93c3822

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -8,18 +8,20 @@ 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
- # βœ… Function to review Python code with debug logs
12
  def review_code(code_snippet):
13
  print("βœ… Received Code:", code_snippet) # Debugging log
14
 
 
 
 
15
  # Process input
16
- inputs = tokenizer(code_snippet, return_tensors="pt").to("cpu") # Move to CPU
17
  outputs = model.generate(
18
  **inputs,
19
- max_length=50, # βœ… Reduced token generation to prevent hallucinations
20
  do_sample=False,
21
- num_beams=3,
22
- repetition_penalty=2.0 # βœ… Penalizes repetitive text generation
23
  )
24
 
25
  # Check if the model generated output
@@ -37,6 +39,7 @@ def review_code(code_snippet):
37
 
38
  return reviewed_code, temp_file_path # βœ… Return reviewed code & file path
39
 
 
40
  # βœ… Handle user input and return reviewed code
41
  def check_code(input_code):
42
  reviewed_code, file_path = review_code(input_code)
 
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
 
39
 
40
  return reviewed_code, temp_file_path # βœ… Return reviewed code & file path
41
 
42
+
43
  # βœ… Handle user input and return reviewed code
44
  def check_code(input_code):
45
  reviewed_code, file_path = review_code(input_code)