File size: 6,488 Bytes
b7062c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
import os
import torch
from transformers import AutoTokenizer, T5ForSequenceClassification
from typing import Dict, List, Any
class EndpointHandler:
"""
HuggingFace Inference Endpoint Handler for Java Vulnerability Detection
CodeT5 ๊ธฐ๋ฐ ๋ถ๋ฅ ๋ชจ๋ธ (LoRA fine-tuned)
"""
def __init__(self, path="."):
"""
๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
Args:
path (str): ๋ชจ๋ธ์ด ์ ์ฅ๋ ๊ฒฝ๋ก (HuggingFace Hub์์ ์๋์ผ๋ก ์ค์ ๋จ)
"""
print(f"๐ Loading Java Vulnerability Detection Model from {path}")
# ๋๋ฐ์ด์ค ์ค์
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"๐ Device: {self.device}")
# ํ ํฌ๋์ด์ ๋ก๋
self.tokenizer = AutoTokenizer.from_pretrained(path)
# T5ForSequenceClassification ๋ชจ๋ธ ๋ก๋
self.model = T5ForSequenceClassification.from_pretrained(
path,
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32
)
# ๋ชจ๋ธ์ ํ๊ฐ ๋ชจ๋๋ก ์ค์ ํ๊ณ ๋๋ฐ์ด์ค๋ก ์ด๋
self.model.to(self.device)
self.model.eval()
print("โ
Model loaded successfully!")
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
๋ฉ์ธ ์ถ๋ก ๋ฉ์๋ (HuggingFace Inference API๊ฐ ํธ์ถ)
Args:
data (dict): ์
๋ ฅ ๋ฐ์ดํฐ
- "inputs" (str): Java ์ฝ๋ ๋๋
- "code" (str): Java ์ฝ๋
Returns:
list: ์์ธก ๊ฒฐ๊ณผ ๋ฆฌ์คํธ
"""
# 1. ์ ์ฒ๋ฆฌ
inputs = self.preprocess(data)
# 2. ์ถ๋ก
outputs = self.inference(inputs)
# 3. ํ์ฒ๋ฆฌ
result = self.postprocess(outputs)
return result
def preprocess(self, request: Dict[str, Any]) -> Dict[str, torch.Tensor]:
"""
์
๋ ฅ ๋ฐ์ดํฐ๋ฅผ ์ ์ฒ๋ฆฌํฉ๋๋ค.
Args:
request (dict): API ์์ฒญ ๋ฐ์ดํฐ
Returns:
dict: ํ ํฌ๋์ด์ฆ๋ ์
๋ ฅ ํ
์
"""
# ์
๋ ฅ ํ
์คํธ ์ถ์ถ
if isinstance(request, dict):
# "inputs" ๋๋ "code" ํค์์ Java ์ฝ๋ ์ถ์ถ
code = request.get("inputs") or request.get("code")
elif isinstance(request, list) and len(request) > 0:
code = request[0].get("inputs") or request[0].get("code")
elif isinstance(request, str):
code = request
else:
raise ValueError(
"Invalid request format. Expected {'inputs': 'Java code here'} "
"or {'code': 'Java code here'}"
)
if not code:
raise ValueError("No code provided in request")
# ํ๋กฌํํธ ํ
ํ๋ฆฟ ์ ์ฉ
input_text = f"Is this Java code vulnerable?:\n{code}"
# ํ ํฌ๋์ด์ง
inputs = self.tokenizer(
input_text,
max_length=512,
truncation=True,
padding="max_length",
return_tensors="pt"
)
# ๋๋ฐ์ด์ค๋ก ์ด๋
inputs = {k: v.to(self.device) for k, v in inputs.items()}
return inputs
def inference(self, inputs: Dict[str, torch.Tensor]) -> torch.Tensor:
"""
๋ชจ๋ธ ์ถ๋ก ์ ์ํํฉ๋๋ค.
Args:
inputs (dict): ์ ์ฒ๋ฆฌ๋ ์
๋ ฅ ํ
์
Returns:
torch.Tensor: ๋ชจ๋ธ ์ถ๋ ฅ ๋ก์ง
"""
with torch.no_grad():
outputs = self.model(**inputs)
logits = outputs.logits
return logits
def postprocess(self, logits: torch.Tensor) -> List[Dict[str, Any]]:
"""
๋ชจ๋ธ ์ถ๋ ฅ์ ์ฌ๋์ด ์ฝ์ ์ ์๋ ํํ๋ก ๋ณํํฉ๋๋ค.
Args:
logits (torch.Tensor): ๋ชจ๋ธ ์ถ๋ ฅ ๋ก์ง
Returns:
list: ์์ธก ๊ฒฐ๊ณผ ๋ฆฌ์คํธ
"""
# ๋ก์ง ์ฒ๋ฆฌ (๋จ์ผ ์ถ๋ ฅ vs ๋ค์ค ํด๋์ค)
if logits.shape[-1] == 1:
# Binary classification with single output
prob = torch.sigmoid(logits).item()
predicted_class = 1 if prob > 0.5 else 0
confidence = prob if predicted_class == 1 else (1 - prob)
probabilities = {
"LABEL_0": 1 - prob,
"LABEL_1": prob
}
else:
# Multi-class classification
probs = torch.softmax(logits, dim=1)[0]
predicted_class = torch.argmax(logits, dim=1).item()
confidence = probs[predicted_class].item()
probabilities = {
f"LABEL_{i}": probs[i].item()
for i in range(len(probs))
}
# ๋ ์ด๋ธ ๋งคํ
label_map = {
0: "safe",
1: "vulnerable"
}
# ๊ฒฐ๊ณผ ํฌ๋งทํ
result = {
"label": label_map.get(predicted_class, f"LABEL_{predicted_class}"),
"score": confidence,
"probabilities": probabilities,
"details": {
"is_vulnerable": predicted_class == 1,
"confidence_percentage": f"{confidence * 100:.2f}%",
"safe_probability": probabilities.get("LABEL_0", 0),
"vulnerable_probability": probabilities.get("LABEL_1", 0)
}
}
return [result]
# ๋ก์ปฌ ํ
์คํธ์ฉ ์ฝ๋
if __name__ == "__main__":
# ๋ก์ปฌ์์ ํ
์คํธํ ๋ ์ฌ์ฉ
handler = EndpointHandler(path=".")
# ํ
์คํธ ์ผ์ด์ค
test_code = """
import java.sql.*;
public class SQLInjectionVulnerable {
public void getUser(String userInput) {
String query = "SELECT * FROM users WHERE username = '" + userInput + "'";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
}
}
"""
# ์ถ๋ก ์คํ
request = {"inputs": test_code}
result = handler(request)
print("\n๐ Test Result:")
print(result) |