Instructions to use souzatya/ScoreCLIQ_v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use souzatya/ScoreCLIQ_v1 with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, BertForRegression tokenizer = AutoTokenizer.from_pretrained("souzatya/ScoreCLIQ_v1") model = BertForRegression.from_pretrained("souzatya/ScoreCLIQ_v1", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from souzatya/ScoreCLIQ_v1: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/souzatya/ScoreCLIQ_v1/resolve/main/handler.py
- Command line
-
hf download hf://souzatya/ScoreCLIQ_v1/handler.py
-
curl -L -o handler.py https://huggingface.co/souzatya/ScoreCLIQ_v1/resolve/main/handler.py
1.65 kB
| import torch | |
| import torch.nn as nn | |
| import math | |
| from transformers import BertModel, BertTokenizer, BertPreTrainedModel | |
| class BertForRegression(BertPreTrainedModel): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.bert = BertModel(config) | |
| self.regressor = nn.Linear(config.hidden_size, 1) | |
| self.init_weights() | |
| def forward(self, input_ids, attention_mask=None, token_type_ids=None, labels=None): | |
| outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids) | |
| logits = self.regressor(outputs.pooler_output) | |
| loss = None | |
| if labels is not None: | |
| loss_fct = nn.MSELoss() | |
| loss = loss_fct(logits.squeeze(), labels.float()) | |
| return (loss, logits) if loss is not None else logits | |
| class EndpointHandler(): | |
| def __init__(self, path: str): | |
| self.model = BertForRegression.from_pretrained(path) | |
| self.tokenizer = BertTokenizer.from_pretrained(path) | |
| def __call__(self, data): | |
| self.model.eval() | |
| # Accept JSON input: {"inputs": "text string"} | |
| if isinstance(data, dict): | |
| text = data.get("inputs", "") | |
| else: | |
| text = data | |
| if not isinstance(text, str): | |
| raise ValueError("Input text must be a string under the 'inputs' key.") | |
| inputs = self.tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512) | |
| with torch.no_grad(): | |
| logits = self.model(**inputs) | |
| prediction = logits[0].item() | |
| prediction = math.trunc(prediction * 100) / 100 | |
| return str(prediction) |