Update app.py
Browse files
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 |
-
# ✅
|
15 |
-
prompt = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
22 |
do_sample=False,
|
23 |
-
num_beams=
|
24 |
-
repetition_penalty=
|
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
|