Spaces:
Running
Running
Download utils.py from rusen/gpt-detector: direct link, hf CLI and curl.
- Browser
- Download file 666 Bytes
-
https://huggingface.co/spaces/rusen/gpt-detector/resolve/main/utils.py
- Command line
-
hf download hf://spaces/rusen/gpt-detector/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/rusen/gpt-detector/resolve/main/utils.py
666 Bytes
| import torch | |
| import numpy as np | |
| def model_predict(model, tokenizer, sentences): | |
| """ | |
| Predict the labels of the sentences using the model and tokenizer | |
| Args: | |
| model: Model (transformers) | |
| tokenizer: Tokenizer (transformers tokenizer) | |
| sentences: Sentences to predict (ndarray) | |
| Returns: | |
| predictions: Predicted labels | |
| """ | |
| inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors="pt", max_length=512).to("cpu") | |
| # Classify sentences | |
| with torch.no_grad(): | |
| outputs = model(**inputs) # get the logits | |
| label = np.argmax(outputs.logits.to("cpu")) | |
| return int(label) |