Spaces:
Sleeping
Sleeping
updated
Browse files
app.py
CHANGED
@@ -4,27 +4,27 @@ model_name = "Salesforce/codet5-base"
|
|
4 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
5 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
6 |
|
7 |
-
|
8 |
-
inputs = tokenizer(code_snippet, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
|
9 |
-
review_ids = model.generate(inputs.input_ids, max_new_tokens=100, num_beams=4, length_penalty=2.0, early_stopping=True)
|
10 |
-
review = tokenizer.decode(review_ids[0], skip_special_tokens=True)
|
11 |
-
return review
|
12 |
-
|
13 |
-
# Example C# code snippet
|
14 |
code_snippet = """
|
|
|
15 |
public class Calculator {
|
|
|
16 |
public int Add(int a, int b) {
|
17 |
return a + b;
|
18 |
}
|
19 |
|
|
|
20 |
public int Subtract(int a, int b) {
|
21 |
return a - b;
|
22 |
}
|
23 |
|
|
|
24 |
public int Multiply(int a, int b) {
|
25 |
return a * b;
|
26 |
}
|
27 |
|
|
|
|
|
28 |
public int Divide(int a, int b) {
|
29 |
if (b == 0) {
|
30 |
throw new DivideByZeroException("Division by zero is not allowed.");
|
@@ -33,5 +33,9 @@ public class Calculator {
|
|
33 |
}
|
34 |
}
|
35 |
"""
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
print("Code Review:", review)
|
|
|
4 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
5 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
6 |
|
7 |
+
# More complex C# code snippet
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
code_snippet = """
|
9 |
+
// This class demonstrates a simple calculator program
|
10 |
public class Calculator {
|
11 |
+
// Adds two integers
|
12 |
public int Add(int a, int b) {
|
13 |
return a + b;
|
14 |
}
|
15 |
|
16 |
+
// Subtracts second integer from first
|
17 |
public int Subtract(int a, int b) {
|
18 |
return a - b;
|
19 |
}
|
20 |
|
21 |
+
// Multiplies two integers
|
22 |
public int Multiply(int a, int b) {
|
23 |
return a * b;
|
24 |
}
|
25 |
|
26 |
+
// Divides first integer by second
|
27 |
+
// Throws DivideByZeroException if b is zero
|
28 |
public int Divide(int a, int b) {
|
29 |
if (b == 0) {
|
30 |
throw new DivideByZeroException("Division by zero is not allowed.");
|
|
|
33 |
}
|
34 |
}
|
35 |
"""
|
36 |
+
|
37 |
+
inputs = tokenizer(code_snippet, return_tensors="pt")
|
38 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
39 |
+
review = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
40 |
+
|
41 |
print("Code Review:", review)
|