Update app.py
Browse files
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(
|
17 |
outputs = model.generate(
|
18 |
**inputs,
|
19 |
-
max_length=
|
20 |
do_sample=False,
|
21 |
-
num_beams=
|
22 |
-
repetition_penalty=
|
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)
|