MartyNattakit commited on
Commit
2a8fcc0
·
1 Parent(s): fbfb0eb

Update app.py to load model from Hugging Face Model Hub

Browse files
Files changed (1) hide show
  1. app.py +4 -9
app.py CHANGED
@@ -10,24 +10,19 @@ class CodeClassifier(torch.nn.Module):
10
  def __init__(self, base_model, num_labels=6):
11
  super(CodeClassifier, self).__init__()
12
  self.base = base_model
13
- self.reduction = torch.nn.Linear(768, 512) # Randomly initialized
14
- self.classifier = torch.nn.Linear(512, num_labels) # Match checkpoint
15
 
16
  def forward(self, input_ids, attention_mask):
17
  outputs = self.base(input_ids=input_ids, attention_mask=attention_mask)
18
  reduced = self.reduction(outputs.pooler_output)
19
  return self.classifier(reduced)
20
 
21
- # Load model and tokenizer
22
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
  tokenizer = RobertaTokenizer.from_pretrained('microsoft/codebert-base')
24
- base_model = RobertaModel.from_pretrained('microsoft/codebert-base')
25
  model = CodeClassifier(base_model)
26
- checkpoint = torch.load("C:\\Users\\MartyNattakit\\Downloads\\best_model.pt", map_location=device)
27
-
28
- # Load the state dict, focusing on classifier weights
29
- model_state = checkpoint.get('model_state_dict', checkpoint)
30
- model.load_state_dict(model_state, strict=False)
31
  print("Loaded state dict keys:", model.state_dict().keys())
32
  print("Classifier weight shape:", model.classifier.weight.shape)
33
  model.eval()
 
10
  def __init__(self, base_model, num_labels=6):
11
  super(CodeClassifier, self).__init__()
12
  self.base = base_model
13
+ self.reduction = torch.nn.Linear(768, 512)
14
+ self.classifier = torch.nn.Linear(512, num_labels)
15
 
16
  def forward(self, input_ids, attention_mask):
17
  outputs = self.base(input_ids=input_ids, attention_mask=attention_mask)
18
  reduced = self.reduction(outputs.pooler_output)
19
  return self.classifier(reduced)
20
 
21
+ # Load model and tokenizer from Hugging Face Model Hub
22
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
  tokenizer = RobertaTokenizer.from_pretrained('microsoft/codebert-base')
24
+ base_model = RobertaModel.from_pretrained('martynattakit/CodeSentinel-Model') # Match your Model repo
25
  model = CodeClassifier(base_model)
 
 
 
 
 
26
  print("Loaded state dict keys:", model.state_dict().keys())
27
  print("Classifier weight shape:", model.classifier.weight.shape)
28
  model.eval()