Upload folder using huggingface_hub
Browse files- README.md +82 -0
- configuration_suave_multitask.py +17 -0
- modeling_suave_multitask.py +71 -0
- prepare_hf_artifacts.py +50 -0
- upload.py +48 -0
README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
library_name: pytorch
|
| 4 |
+
license: mit
|
| 5 |
+
pipeline_tag: text-classification
|
| 6 |
+
tags:
|
| 7 |
+
- pytorch
|
| 8 |
+
- multitask
|
| 9 |
+
- ai-detection
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# SuaveAI Detection Multitask Model V1
|
| 13 |
+
|
| 14 |
+
This repository contains a custom PyTorch multitask model checkpoint and auxiliary files.
|
| 15 |
+
|
| 16 |
+
## Files
|
| 17 |
+
|
| 18 |
+
- `multitask_model.pth`: model checkpoint weights
|
| 19 |
+
- `label_encoder.pkl`: label encoder used to map predictions to labels
|
| 20 |
+
- `tok.txt`: tokenizer/vocabulary artifact used during preprocessing
|
| 21 |
+
|
| 22 |
+
## Important
|
| 23 |
+
|
| 24 |
+
This is a **custom PyTorch checkpoint** and is not a native Transformers `AutoModel` package.
|
| 25 |
+
This repo now includes Hugging Face custom-code files so it can be loaded from Hub with
|
| 26 |
+
`trust_remote_code=True`.
|
| 27 |
+
|
| 28 |
+
## Load from Hugging Face Hub
|
| 29 |
+
|
| 30 |
+
```python
|
| 31 |
+
import torch
|
| 32 |
+
from transformers import AutoModel, AutoTokenizer
|
| 33 |
+
|
| 34 |
+
repo_id = "DaJulster/SuaveAI-Dectection-Multitask-Model-V1"
|
| 35 |
+
|
| 36 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
|
| 37 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True)
|
| 38 |
+
model.eval()
|
| 39 |
+
|
| 40 |
+
text = "This is a sample input"
|
| 41 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model(**inputs)
|
| 44 |
+
|
| 45 |
+
binary_logits = outputs.logits_binary
|
| 46 |
+
multiclass_logits = outputs.logits_multiclass
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
Binary prediction uses `logits_binary`, and AI-model classification uses `logits_multiclass`.
|
| 50 |
+
|
| 51 |
+
## Quick start
|
| 52 |
+
|
| 53 |
+
```python
|
| 54 |
+
import torch
|
| 55 |
+
import pickle
|
| 56 |
+
|
| 57 |
+
# 1) Recreate your model class exactly as in training
|
| 58 |
+
# from model_def import MultiTaskModel
|
| 59 |
+
# model = MultiTaskModel(...)
|
| 60 |
+
|
| 61 |
+
model = ... # instantiate your model architecture
|
| 62 |
+
state = torch.load("multitask_model.pth", map_location="cpu")
|
| 63 |
+
model.load_state_dict(state)
|
| 64 |
+
model.eval()
|
| 65 |
+
|
| 66 |
+
with open("label_encoder.pkl", "rb") as f:
|
| 67 |
+
label_encoder = pickle.load(f)
|
| 68 |
+
|
| 69 |
+
with open("tok.txt", "r", encoding="utf-8") as f:
|
| 70 |
+
tokenizer_artifact = f.read()
|
| 71 |
+
|
| 72 |
+
# Run your preprocessing + inference pipeline here
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
## Intended use
|
| 76 |
+
|
| 77 |
+
- Multitask AI detection inference in your custom pipeline.
|
| 78 |
+
|
| 79 |
+
## Limitations
|
| 80 |
+
|
| 81 |
+
- Requires matching model definition and preprocessing pipeline.
|
| 82 |
+
- Not plug-and-play with `transformers.AutoModel.from_pretrained`.
|
configuration_suave_multitask.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class SuaveMultitaskConfig(PretrainedConfig):
|
| 5 |
+
model_type = "suave_multitask"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
base_model_name="roberta-base",
|
| 10 |
+
num_ai_classes=2,
|
| 11 |
+
classifier_dropout=0.1,
|
| 12 |
+
**kwargs,
|
| 13 |
+
):
|
| 14 |
+
self.base_model_name = base_model_name
|
| 15 |
+
self.num_ai_classes = num_ai_classes
|
| 16 |
+
self.classifier_dropout = classifier_dropout
|
| 17 |
+
super().__init__(**kwargs)
|
modeling_suave_multitask.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Optional, Tuple
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
from transformers import AutoModel, PreTrainedModel
|
| 7 |
+
from transformers.modeling_outputs import ModelOutput
|
| 8 |
+
|
| 9 |
+
from configuration_suave_multitask import SuaveMultitaskConfig
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class SuaveMultitaskOutput(ModelOutput):
|
| 14 |
+
loss: Optional[torch.FloatTensor] = None
|
| 15 |
+
logits_binary: Optional[torch.FloatTensor] = None
|
| 16 |
+
logits_multiclass: Optional[torch.FloatTensor] = None
|
| 17 |
+
hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None
|
| 18 |
+
attentions: Optional[Tuple[torch.FloatTensor, ...]] = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class SuaveMultitaskModel(PreTrainedModel):
|
| 22 |
+
config_class = SuaveMultitaskConfig
|
| 23 |
+
base_model_prefix = "encoder"
|
| 24 |
+
|
| 25 |
+
def __init__(self, config: SuaveMultitaskConfig):
|
| 26 |
+
super().__init__(config)
|
| 27 |
+
self.encoder = AutoModel.from_pretrained(config.base_model_name)
|
| 28 |
+
hidden_size = self.encoder.config.hidden_size
|
| 29 |
+
|
| 30 |
+
self.dropout = nn.Dropout(config.classifier_dropout)
|
| 31 |
+
self.classifier_binary = nn.Linear(hidden_size, 2)
|
| 32 |
+
self.classifier_multiclass = nn.Linear(hidden_size, config.num_ai_classes)
|
| 33 |
+
|
| 34 |
+
self.post_init()
|
| 35 |
+
|
| 36 |
+
def forward(
|
| 37 |
+
self,
|
| 38 |
+
input_ids=None,
|
| 39 |
+
attention_mask=None,
|
| 40 |
+
labels_binary=None,
|
| 41 |
+
labels_multiclass=None,
|
| 42 |
+
**kwargs,
|
| 43 |
+
):
|
| 44 |
+
outputs = self.encoder(
|
| 45 |
+
input_ids=input_ids,
|
| 46 |
+
attention_mask=attention_mask,
|
| 47 |
+
output_hidden_states=kwargs.get("output_hidden_states", False),
|
| 48 |
+
output_attentions=kwargs.get("output_attentions", False),
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
pooled = outputs.last_hidden_state[:, 0]
|
| 52 |
+
pooled = self.dropout(pooled)
|
| 53 |
+
|
| 54 |
+
logits_binary = self.classifier_binary(pooled)
|
| 55 |
+
logits_multiclass = self.classifier_multiclass(pooled)
|
| 56 |
+
|
| 57 |
+
loss = None
|
| 58 |
+
if labels_binary is not None and labels_multiclass is not None:
|
| 59 |
+
loss_binary = nn.CrossEntropyLoss()(logits_binary, labels_binary)
|
| 60 |
+
loss_multiclass = nn.CrossEntropyLoss(ignore_index=-1)(
|
| 61 |
+
logits_multiclass, labels_multiclass
|
| 62 |
+
)
|
| 63 |
+
loss = loss_binary + 0.5 * loss_multiclass
|
| 64 |
+
|
| 65 |
+
return SuaveMultitaskOutput(
|
| 66 |
+
loss=loss,
|
| 67 |
+
logits_binary=logits_binary,
|
| 68 |
+
logits_multiclass=logits_multiclass,
|
| 69 |
+
hidden_states=outputs.hidden_states,
|
| 70 |
+
attentions=outputs.attentions,
|
| 71 |
+
)
|
prepare_hf_artifacts.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import pickle
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer
|
| 6 |
+
|
| 7 |
+
from configuration_suave_multitask import SuaveMultitaskConfig
|
| 8 |
+
from modeling_suave_multitask import SuaveMultitaskModel
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
model_ckpt = Path("multitask_model.pth")
|
| 13 |
+
label_encoder_path = Path("label_encoder.pkl")
|
| 14 |
+
|
| 15 |
+
if not model_ckpt.exists():
|
| 16 |
+
raise FileNotFoundError("multitask_model.pth not found")
|
| 17 |
+
if not label_encoder_path.exists():
|
| 18 |
+
raise FileNotFoundError("label_encoder.pkl not found")
|
| 19 |
+
|
| 20 |
+
with open(label_encoder_path, "rb") as file:
|
| 21 |
+
label_encoder = pickle.load(file)
|
| 22 |
+
|
| 23 |
+
num_ai_classes = len(label_encoder.classes_)
|
| 24 |
+
|
| 25 |
+
config = SuaveMultitaskConfig(
|
| 26 |
+
base_model_name="roberta-base",
|
| 27 |
+
num_ai_classes=num_ai_classes,
|
| 28 |
+
id2label={0: "human", 1: "ai"},
|
| 29 |
+
label2id={"human": 0, "ai": 1},
|
| 30 |
+
)
|
| 31 |
+
config.auto_map = {
|
| 32 |
+
"AutoConfig": "configuration_suave_multitask.SuaveMultitaskConfig",
|
| 33 |
+
"AutoModel": "modeling_suave_multitask.SuaveMultitaskModel",
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
model = SuaveMultitaskModel(config)
|
| 37 |
+
state_dict = torch.load(model_ckpt, map_location="cpu")
|
| 38 |
+
model.load_state_dict(state_dict, strict=True)
|
| 39 |
+
model.eval()
|
| 40 |
+
|
| 41 |
+
model.save_pretrained(".", safe_serialization=True)
|
| 42 |
+
|
| 43 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name)
|
| 44 |
+
tokenizer.save_pretrained(".")
|
| 45 |
+
|
| 46 |
+
print("HF artifacts generated: config.json, model.safetensors, tokenizer files")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
main()
|
upload.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from huggingface_hub import HfApi
|
| 5 |
+
|
| 6 |
+
api = HfApi()
|
| 7 |
+
|
| 8 |
+
# Replace with your desired repo name, e.g., "username/ai-detector-v1"
|
| 9 |
+
repo_id = "DaJulster/SuaveAI-Dectection-Multitask-Model-V1"
|
| 10 |
+
|
| 11 |
+
required_files = [
|
| 12 |
+
"multitask_model.pth",
|
| 13 |
+
"label_encoder.pkl",
|
| 14 |
+
"README.md",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
missing = [file_name for file_name in required_files if not Path(file_name).exists()]
|
| 18 |
+
if missing:
|
| 19 |
+
raise FileNotFoundError(f"Missing required files: {', '.join(missing)}")
|
| 20 |
+
|
| 21 |
+
# 1. Create the repository on the Hub (if it doesn't exist)
|
| 22 |
+
api.create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)
|
| 23 |
+
|
| 24 |
+
# 2. Generate HF-compatible artifacts from existing checkpoint (optional)
|
| 25 |
+
skip_prepare = os.environ.get("SKIP_HF_PREPARE", "0") == "1"
|
| 26 |
+
if not skip_prepare:
|
| 27 |
+
from prepare_hf_artifacts import main as prepare_hf_artifacts
|
| 28 |
+
|
| 29 |
+
prepare_hf_artifacts()
|
| 30 |
+
else:
|
| 31 |
+
print("Skipping HF artifact generation (SKIP_HF_PREPARE=1)")
|
| 32 |
+
|
| 33 |
+
# 3. Upload all local artifacts (model card + model files)
|
| 34 |
+
api.upload_folder(
|
| 35 |
+
folder_path=".",
|
| 36 |
+
repo_id=repo_id,
|
| 37 |
+
repo_type="model",
|
| 38 |
+
ignore_patterns=[
|
| 39 |
+
"*.pyc",
|
| 40 |
+
"__pycache__/*",
|
| 41 |
+
".git/*",
|
| 42 |
+
"*.ipynb",
|
| 43 |
+
"venv/*",
|
| 44 |
+
"tok.txt",
|
| 45 |
+
],
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
print(f"Model pushed successfully to: https://huggingface.co/{repo_id}")
|