LutherYTT commited on
Commit
44416eb
·
1 Parent(s): 596abb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
 
4
  from safetensors.torch import load_file
5
- from transformers import AutoTokenizer, AutoModel, AutoConfig
6
  import gc
7
 
8
  # Release memory
@@ -10,37 +10,34 @@ gc.collect()
10
  torch.cuda.empty_cache()
11
 
12
  model_name = "hfl/chinese-roberta-wwm-ext"
13
- config = AutoConfig.from_pretrained(model_name)
14
-
15
- class MultiTaskRoberta(torch.nn.Module):
16
- def __init__(self, config):
17
- super().__init__()
18
- self.bert = torch.nn.Linear(768, 768)
19
- self.classifier = nn.Linear(config.hidden_size, 3) # 3 classes for sentiment
20
- self.regressor = nn.Linear(config.hidden_size, 5) # 5 regression outputs
21
 
22
- def forward(self, input_ids, attention_mask=None, **kwargs):
23
- outputs = self.roberta(input_ids=input_ids, attention_mask=attention_mask)
24
- pooled = outputs.last_hidden_state[:, 0]
25
- logits = self.classifier(pooled)
26
- regs = self.regressor(pooled)
27
- return {"logits": logits, "regression_outputs": regs}
 
 
 
 
 
 
 
 
28
 
29
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
  print(f"Device: {device}")
31
 
 
 
32
  # Load tokenizer
33
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
34
 
35
- # Load base model
36
- base_model = AutoModel.from_pretrained(model_name)
37
- model = MultiTaskRoberta(config)
38
-
39
- # Load safetensors
40
- model_path = "model1.safetensors"
41
- state_dict = torch.load(model_path)
42
  model.load_state_dict(state_dict)
43
- # model.to(device)
44
  model.eval()
45
 
46
  # Use half precision to reduce memory usage
 
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
4
+ from transformers import AutoTokenizer, AutoConfig, BertModel, BertPreTrainedModel
5
  from safetensors.torch import load_file
 
6
  import gc
7
 
8
  # Release memory
 
10
  torch.cuda.empty_cache()
11
 
12
  model_name = "hfl/chinese-roberta-wwm-ext"
 
 
 
 
 
 
 
 
13
 
14
+ class MultiTasRokBert(BertPreTrainedModel):
15
+ def __init__(self, config, model_name_or_path):
16
+ super().__init__(config)
17
+ # Load backbone with pretrained weights if desired
18
+ self.bert = BertModel.from_pretrained(model_name_or_path, config=config)
19
+ self.classifier = nn.Linear(config.hidden_size, 3)
20
+ self.regressor = nn.Linear(config.hidden_size, 5)
21
+
22
+ def forward(self, input_ids, attention_mask=None, token_type_ids=None):
23
+ outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
24
+ pooled = outputs.pooler_output
25
+ sentiment_logits = self.classifier(pooled)
26
+ regression_outputs = self.regressor(pooled)
27
+ return sentiment_logits, regression_outputs
28
 
29
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
  print(f"Device: {device}")
31
 
32
+ model_path = "model1.safetensors"
33
+
34
  # Load tokenizer
35
  tokenizer = AutoTokenizer.from_pretrained(model_name)
36
+ config = AutoConfig.from_pretrained(model_name)
37
 
38
+ model = MultiTaskRoBert(config, model_name).to(device)
39
+ state_dict = load_file(model_path, device=device)
 
 
 
 
 
40
  model.load_state_dict(state_dict)
 
41
  model.eval()
42
 
43
  # Use half precision to reduce memory usage