Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -27,7 +27,6 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
27 |
load_in_4bit=True,
|
28 |
device_map='auto'
|
29 |
)
|
30 |
-
|
31 |
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
32 |
|
33 |
# Load the Lora model
|
@@ -38,16 +37,12 @@ def get_completion(query, model, tokenizer):
|
|
38 |
try:
|
39 |
# Move model to CUDA
|
40 |
model = model.cuda()
|
41 |
-
|
42 |
# Ensure input is on CUDA
|
43 |
inputs = tokenizer(query, return_tensors="pt").to('cuda')
|
44 |
-
|
45 |
with torch.no_grad():
|
46 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
47 |
-
|
48 |
# Move outputs to CPU before decoding
|
49 |
outputs = to_cpu(outputs)
|
50 |
-
|
51 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
except Exception as e:
|
53 |
return f"An error occurred: {str(e)}"
|
@@ -58,8 +53,86 @@ def get_completion(query, model, tokenizer):
|
|
58 |
|
59 |
@spaces.GPU()
|
60 |
def code_review(code_to_analyze):
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
return result
|
64 |
|
65 |
# Create Gradio interface
|
@@ -68,7 +141,7 @@ iface = gr.Interface(
|
|
68 |
inputs=gr.Textbox(lines=10, label="Enter code to analyze"),
|
69 |
outputs=gr.Textbox(label="Code Review Result"),
|
70 |
title="Code Review Expert",
|
71 |
-
description="This tool analyzes code for potential security flaws and provides guidance on secure coding practices."
|
72 |
)
|
73 |
|
74 |
# Launch the Gradio app
|
|
|
27 |
load_in_4bit=True,
|
28 |
device_map='auto'
|
29 |
)
|
|
|
30 |
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
31 |
|
32 |
# Load the Lora model
|
|
|
37 |
try:
|
38 |
# Move model to CUDA
|
39 |
model = model.cuda()
|
|
|
40 |
# Ensure input is on CUDA
|
41 |
inputs = tokenizer(query, return_tensors="pt").to('cuda')
|
|
|
42 |
with torch.no_grad():
|
43 |
+
outputs = model.generate(**inputs, max_new_tokens=1024, do_sample=True, temperature=0.7)
|
|
|
44 |
# Move outputs to CPU before decoding
|
45 |
outputs = to_cpu(outputs)
|
|
|
46 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
47 |
except Exception as e:
|
48 |
return f"An error occurred: {str(e)}"
|
|
|
53 |
|
54 |
@spaces.GPU()
|
55 |
def code_review(code_to_analyze):
|
56 |
+
few_shot_prompt = """
|
57 |
+
You are an expert in secure coding practices and software logic analysis, tasked with reviewing source code for potential security vulnerabilities and logic flaws. Your goal is to understand the code, identify security issues that could be exploited, and uncover any logic vulnerabilities that could lead to unintended behavior. Follow these steps for each code review:
|
58 |
+
|
59 |
+
1. Understand the code
|
60 |
+
- Analyze the purpose and functionality of the code
|
61 |
+
- Identify input sources and output destinations
|
62 |
+
- Note any security-sensitive operations (e.g., authentication, data storage, network communication)
|
63 |
+
- Understand the logical flow and decision points in the code
|
64 |
+
|
65 |
+
2. Identify potential security issues
|
66 |
+
- Look for common vulnerabilities (e.g., injection flaws, broken authentication, sensitive data exposure)
|
67 |
+
- Consider how the code might be misused or exploited
|
68 |
+
- Evaluate the handling of user input and data validation
|
69 |
+
- Check for proper use of security functions and libraries
|
70 |
+
|
71 |
+
3. Identify potential logic vulnerabilities
|
72 |
+
- Look for incorrect boolean logic in conditional statements
|
73 |
+
- Check for off-by-one errors in loops and array operations
|
74 |
+
- Identify potential race conditions in multi-threaded or asynchronous code
|
75 |
+
- Evaluate edge cases and boundary conditions
|
76 |
+
- Check for proper error handling and exception management
|
77 |
+
|
78 |
+
Here's an example of how to review code:
|
79 |
+
|
80 |
+
Code to review:
|
81 |
+
```php
|
82 |
+
function authenticateUser($username, $password) {
|
83 |
+
$conn = new mysqli("localhost", "user", "password", "database");
|
84 |
+
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
|
85 |
+
$result = $conn->query($query);
|
86 |
+
if ($result->num_rows > 0) {
|
87 |
+
return true;
|
88 |
+
}
|
89 |
+
return false;
|
90 |
+
}
|
91 |
+
```
|
92 |
+
|
93 |
+
Review:
|
94 |
+
1. Understanding the code:
|
95 |
+
- The function attempts to authenticate a user based on a username and password.
|
96 |
+
- It queries a database to check if the credentials exist.
|
97 |
+
- Returns true if a matching user is found, false otherwise.
|
98 |
+
|
99 |
+
2. Potential security issues:
|
100 |
+
- SQL Injection vulnerability: Username and password are directly inserted into the query.
|
101 |
+
- Passwords are stored and compared in plain text, which is a severe security risk.
|
102 |
+
- Hardcoded database credentials in the source code.
|
103 |
+
- Potential for timing attacks due to direct string comparison.
|
104 |
+
|
105 |
+
3. Potential logic vulnerabilities:
|
106 |
+
- The function returns true if more than one row is returned, which could lead to authentication bypass if multiple users have the same credentials.
|
107 |
+
- No input validation on username or password, potentially allowing empty strings or null values.
|
108 |
+
|
109 |
+
Suggestions:
|
110 |
+
- Use prepared statements to prevent SQL injection.
|
111 |
+
- Use password hashing instead of storing plain text passwords.
|
112 |
+
- Store database credentials securely outside the source code.
|
113 |
+
- Implement proper error handling and use constant-time comparison for passwords.
|
114 |
+
- Ensure only one user can be authenticated at a time.
|
115 |
+
- Add input validation for username and password.
|
116 |
+
|
117 |
+
Now, review the following code using this approach:
|
118 |
+
|
119 |
+
{code_to_analyze}
|
120 |
+
|
121 |
+
Provide a detailed review following the structure above, including understanding the code, identifying potential security issues, identifying potential logic vulnerabilities, and offering specific suggestions for improvement. Start your response with 'Code Review:' and end it with 'End of Review.'"
|
122 |
+
"""
|
123 |
+
|
124 |
+
query = few_shot_prompt.format(code_to_analyze=code_to_analyze)
|
125 |
+
full_result = get_completion(query, model, tokenizer)
|
126 |
+
|
127 |
+
# Extract only the AI's answer
|
128 |
+
start_index = full_result.find("Code Review:")
|
129 |
+
end_index = full_result.find("End of Review.")
|
130 |
+
|
131 |
+
if start_index != -1 and end_index != -1:
|
132 |
+
result = full_result[start_index:end_index].strip()
|
133 |
+
else:
|
134 |
+
result = "Unable to generate a proper code review. Please try again."
|
135 |
+
|
136 |
return result
|
137 |
|
138 |
# Create Gradio interface
|
|
|
141 |
inputs=gr.Textbox(lines=10, label="Enter code to analyze"),
|
142 |
outputs=gr.Textbox(label="Code Review Result"),
|
143 |
title="Code Review Expert",
|
144 |
+
description="This tool analyzes code for potential security flaws, logic vulnerabilities, and provides guidance on secure coding practices."
|
145 |
)
|
146 |
|
147 |
# Launch the Gradio app
|