Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,74 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
-
import torch
|
| 3 |
|
| 4 |
-
|
| 5 |
-
model
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
with torch.no_grad():
|
| 12 |
-
outputs = model(**
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BertConfig
|
|
|
|
| 2 |
|
| 3 |
+
def save_model(model, tokenizer, out_dir: str):
|
| 4 |
+
"""Saves model in Hugging Face-compatible format"""
|
| 5 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 6 |
+
|
| 7 |
+
# 1. Save full model architecture
|
| 8 |
+
model.bert.save_pretrained(out_dir)
|
| 9 |
+
|
| 10 |
+
# 2. Save custom classifier weights with standard name
|
| 11 |
+
torch.save(model.state_dict(), os.path.join(out_dir, "pytorch_model.bin"))
|
| 12 |
+
|
| 13 |
+
# 3. Create compatible config
|
| 14 |
+
config = BertConfig.from_pretrained("bert-base-uncased")
|
| 15 |
+
config.update({
|
| 16 |
+
"num_labels": 2,
|
| 17 |
+
"id2label": {0: "REAL", 1: "FAKE"},
|
| 18 |
+
"hidden_dropout_prob": 0.1,
|
| 19 |
+
"classifier_dropout": 0.1,
|
| 20 |
+
"model_type": "bert-for-sequence-classification"
|
| 21 |
+
})
|
| 22 |
+
config.save_pretrained(out_dir)
|
| 23 |
+
|
| 24 |
+
# 4. Save tokenizer
|
| 25 |
+
tokenizer.save_pretrained(out_dir)
|
| 26 |
+
print(f"✅ Model saved in HF format to {out_dir}")
|
| 27 |
|
| 28 |
+
def load_model(model_dir: str, device=torch.device("cpu")):
|
| 29 |
+
"""Loads model using AutoModelForSequenceClassification"""
|
| 30 |
+
from transformers import AutoModelForSequenceClassification
|
| 31 |
+
|
| 32 |
+
# Load with custom config
|
| 33 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 34 |
+
model_dir,
|
| 35 |
+
config=BertConfig.from_pretrained(model_dir)
|
| 36 |
+
|
| 37 |
+
# Load custom weights
|
| 38 |
+
state_dict = torch.load(os.path.join(model_dir, "pytorch_model.bin"), map_location=device)
|
| 39 |
+
model.load_state_dict(state_dict)
|
| 40 |
+
|
| 41 |
+
# Load tokenizer
|
| 42 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_dir)
|
| 43 |
+
|
| 44 |
+
model.to(device).eval()
|
| 45 |
+
print(f"✅ Model loaded from {model_dir}")
|
| 46 |
+
return model, tokenizer
|
| 47 |
+
|
| 48 |
+
# ------------------------
|
| 49 |
+
# 7) Updated Inference
|
| 50 |
+
# ------------------------
|
| 51 |
+
def predict(texts, model, tokenizer, device, max_length=15):
|
| 52 |
+
"""Batch prediction with proper HF formatting"""
|
| 53 |
+
encodings = tokenizer(
|
| 54 |
+
texts,
|
| 55 |
+
max_length=max_length,
|
| 56 |
+
truncation=True,
|
| 57 |
+
padding="max_length",
|
| 58 |
+
return_tensors="pt"
|
| 59 |
+
).to(device)
|
| 60 |
+
|
| 61 |
with torch.no_grad():
|
| 62 |
+
outputs = model(**encodings)
|
| 63 |
+
|
| 64 |
+
return torch.argmax(outputs.logits, dim=1).cpu().numpy()
|
| 65 |
+
|
| 66 |
+
# Usage
|
| 67 |
+
best_model, best_tokenizer = load_model("KenLumod/ML-Fake-Real-News-Detector-Final", device)
|
| 68 |
+
|
| 69 |
+
new_texts = [
|
| 70 |
+
"NASA announces alien life discovery",
|
| 71 |
+
"Congress passes new infrastructure bill"
|
| 72 |
+
]
|
| 73 |
+
predictions = predict(new_texts, best_model, best_tokenizer, device)
|
| 74 |
+
print("Predictions:", ["FAKE" if p == 1 else "REAL" for p in predictions])
|