Instructions to use NotXia/pubmedbert-bio-ext-summ with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use NotXia/pubmedbert-bio-ext-summ with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "summarization" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # 'pip install "transformers<5.0.0' from transformers import pipeline pipe = pipeline("summarization", model="NotXia/pubmedbert-bio-ext-summ", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("NotXia/pubmedbert-bio-ext-summ", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
| import torch | |
| from .transformerutils import TransformerInterEncoder | |
| from transformers import PreTrainedModel, AutoModel, BertConfig | |
| from .configuration import ExtSummConfig | |
| class BERTSummarizer(PreTrainedModel): | |
| config_class = ExtSummConfig | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.bert = AutoModel.from_config(BertConfig.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")) | |
| self.input_size = config.input_size | |
| self.encoder = TransformerInterEncoder(self.bert.config.hidden_size, max_len=512) | |
| def forward(self, batch): | |
| document_ids = batch["ids"].to(self.bert.device) | |
| segments_ids = batch["segments_ids"].to(self.bert.device) | |
| clss_mask = batch["clss_mask"].to(self.bert.device) | |
| attn_mask = batch["attn_mask"].to(self.bert.device) | |
| tokens_out, _ = self.bert(input_ids=document_ids, token_type_ids=segments_ids, attention_mask=attn_mask, return_dict=False) | |
| out = [] | |
| logits_out = [] | |
| for i in range(len(tokens_out)): # Batch handling | |
| clss_out = tokens_out[i][clss_mask[i], :] | |
| sentences_scores, logits = self.encoder(clss_out) | |
| padding = torch.zeros(self.input_size - sentences_scores.shape[0]).to(sentences_scores.device) | |
| out.append( torch.cat((sentences_scores, padding)) ) | |
| logits_out.append( torch.cat((logits, padding)) ) | |
| return torch.stack(out), torch.stack(logits_out) |