Spaces:
Sleeping
Sleeping
Upload model_utils.py
Browse files- model/model_utils.py +9 -12
model/model_utils.py
CHANGED
@@ -1,22 +1,19 @@
|
|
1 |
-
from transformers import AutoTokenizer,
|
2 |
import torch
|
3 |
|
4 |
def load_model():
|
5 |
-
model_name = "
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
-
model =
|
8 |
model.eval()
|
9 |
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
model.to(device)
|
12 |
return tokenizer, model, device
|
13 |
|
14 |
-
def generate_explanation(
|
15 |
-
inputs = tokenizer(
|
16 |
-
|
17 |
-
**inputs
|
18 |
-
|
19 |
-
|
20 |
-
temperature=0.7
|
21 |
-
)
|
22 |
-
return tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
import torch
|
3 |
|
4 |
def load_model():
|
5 |
+
model_name = "mrm8488/codebert-base-finetuned-stackoverflow"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
model.eval()
|
9 |
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
model.to(device)
|
12 |
return tokenizer, model, device
|
13 |
|
14 |
+
def generate_explanation(code, tokenizer, model, device):
|
15 |
+
inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True).to(device)
|
16 |
+
with torch.no_grad():
|
17 |
+
logits = model(**inputs).logits
|
18 |
+
predicted_class_id = logits.argmax().item()
|
19 |
+
return f"This code is classified as category ID: {predicted_class_id} (label may vary based on fine-tuning objective)"
|
|
|
|
|
|