Spaces:
Running
Running
deploy code
Browse files- __pycache__/config.cpython-312.pyc +0 -0
- __pycache__/model.cpython-312.pyc +0 -0
- app.py +36 -0
- config.py +32 -0
- model.py +36 -0
- requirements.txt +2 -0
__pycache__/config.cpython-312.pyc
ADDED
Binary file (930 Bytes). View file
|
|
__pycache__/model.cpython-312.pyc
ADDED
Binary file (2.26 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
import torch
|
4 |
+
from model import EnergySmellsDetector
|
5 |
+
from config import SMELLS, BEST_THRESHOLD
|
6 |
+
|
7 |
+
TOKENIZER = "microsoft/graphcodebert-base"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER)
|
9 |
+
model = EnergySmellsDetector.load_model_from_hf()
|
10 |
+
|
11 |
+
|
12 |
+
def round_logit(logits, threshold):
|
13 |
+
logits = (logits > threshold).to(int)
|
14 |
+
return logits.cpu().numpy()
|
15 |
+
|
16 |
+
|
17 |
+
def greet(code_snippet):
|
18 |
+
inputs = tokenizer(code_snippet, return_tensors="pt", truncation=True)
|
19 |
+
with torch.no_grad():
|
20 |
+
logits = model(**inputs)[0]
|
21 |
+
rounded_logits = round_logit(logits, BEST_THRESHOLD)
|
22 |
+
return f"{dict(zip(SMELLS, map(int, rounded_logits)))}"
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
textbox = gr.Textbox(label="Enter your code snippet", placeholder="Here goes your code")
|
27 |
+
description = "An application to identify whether your code has energy smells or not. It predicts the presence of 9 different energy smells."
|
28 |
+
title = "Energy Smells Detector"
|
29 |
+
|
30 |
+
gr.Interface(
|
31 |
+
title=title,
|
32 |
+
description=description,
|
33 |
+
inputs=textbox,
|
34 |
+
fn=greet,
|
35 |
+
outputs="text"
|
36 |
+
).launch()
|
config.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import set_seed
|
3 |
+
from pathlib import Path
|
4 |
+
import os
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
SMELLS = [
|
10 |
+
"BuiltInBehavior",
|
11 |
+
"ExtraUsageOfArrays",
|
12 |
+
"ImproperDataStructure",
|
13 |
+
"LackingBreak",
|
14 |
+
"NotCaching",
|
15 |
+
"OveruseOfLoops",
|
16 |
+
"UnnecessaryConditionals",
|
17 |
+
"UnnecessaryRecursion",
|
18 |
+
"UnnecessaryTypeConversion",
|
19 |
+
]
|
20 |
+
|
21 |
+
|
22 |
+
# Configurations
|
23 |
+
DROPOUT_PROB = 0.1
|
24 |
+
HIDDEN_SIZE = 768
|
25 |
+
NUM_LABELS = 9
|
26 |
+
LAST_NUM_NEURON = 9
|
27 |
+
BEST_THRESHOLD = 0.3
|
28 |
+
PRETRAINED_MODEL = "microsoft/graphcodebert-base"
|
29 |
+
MODEL_SAVE_DIR = Path(__file__).parents[1] / "models_weights"
|
30 |
+
WEIGHTS_FILE_NAME = "weights.bin"
|
31 |
+
MODEL_SAVE_PATH = MODEL_SAVE_DIR / WEIGHTS_FILE_NAME
|
32 |
+
HF_REPO_NAME = f"mjavadmt/EnergySmell"
|
model.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from transformers import AutoModel
|
4 |
+
from config import (
|
5 |
+
HIDDEN_SIZE,
|
6 |
+
DROPOUT_PROB,
|
7 |
+
LAST_NUM_NEURON,
|
8 |
+
HF_REPO_NAME,
|
9 |
+
WEIGHTS_FILE_NAME,
|
10 |
+
PRETRAINED_MODEL,
|
11 |
+
)
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
|
15 |
+
class EnergySmellsDetector(nn.Module):
|
16 |
+
def __init__(self, model_name):
|
17 |
+
super(EnergySmellsDetector, self).__init__()
|
18 |
+
self.model = AutoModel.from_pretrained(model_name)
|
19 |
+
self.dropout = nn.Dropout(DROPOUT_PROB)
|
20 |
+
self.fc = nn.Linear(HIDDEN_SIZE, LAST_NUM_NEURON)
|
21 |
+
|
22 |
+
def forward(self, input_ids, attention_mask):
|
23 |
+
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask)
|
24 |
+
x = self.dropout(outputs.pooler_output)
|
25 |
+
logits = self.fc(x)
|
26 |
+
return torch.sigmoid(logits).to(float)
|
27 |
+
|
28 |
+
@staticmethod
|
29 |
+
def load_model_from_hf():
|
30 |
+
model_path = hf_hub_download(repo_id=HF_REPO_NAME, filename=WEIGHTS_FILE_NAME)
|
31 |
+
|
32 |
+
# Load model
|
33 |
+
model = EnergySmellsDetector(PRETRAINED_MODEL)
|
34 |
+
model.load_state_dict(torch.load(model_path))
|
35 |
+
|
36 |
+
return model
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|