Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,7 +37,9 @@ st.caption("Multi-Modal Fake News & AI Image Detection System")
|
|
| 37 |
# ---------------------------
|
| 38 |
@st.cache_resource
|
| 39 |
def load_text_model():
|
| 40 |
-
model_name = "Maheentouqeer1/truthguard-fake-news-detector"
|
|
|
|
|
|
|
| 41 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 42 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 43 |
model_name, low_cpu_mem_usage=True
|
|
@@ -64,10 +66,24 @@ def load_image_model():
|
|
| 64 |
model.classifier[1] = nn.Linear(model.last_channel, 1)
|
| 65 |
model.eval()
|
| 66 |
return model
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def predict_news(text, tokenizer, text_model):
|
| 72 |
inputs = tokenizer(
|
| 73 |
text, return_tensors="pt",
|
|
@@ -76,12 +92,9 @@ def predict_news(text, tokenizer, text_model):
|
|
| 76 |
with torch.no_grad():
|
| 77 |
outputs = text_model(**inputs)
|
| 78 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 79 |
-
# ADD THIS to see raw scores
|
| 80 |
-
st.write(f"Debug → Label 0 score: {probs[0][0]:.2f}, Label 1 score: {probs[0][1]:.2f}")
|
| 81 |
prediction = torch.argmax(probs).item()
|
| 82 |
confidence = torch.max(probs).item() * 100
|
| 83 |
return prediction, confidence
|
| 84 |
-
|
| 85 |
# ---------------------------
|
| 86 |
# PREDICT IMAGE
|
| 87 |
# ---------------------------
|
|
@@ -118,8 +131,10 @@ with tab1:
|
|
| 118 |
pred, conf = predict_news(user_input, tokenizer, text_model)
|
| 119 |
if pred == 0:
|
| 120 |
st.error(f"⚠️ FAKE NEWS ({conf:.2f}%)")
|
| 121 |
-
|
| 122 |
st.success(f"✅ REAL NEWS ({conf:.2f}%)")
|
|
|
|
|
|
|
| 123 |
st.progress(int(conf))
|
| 124 |
|
| 125 |
with tab2:
|
|
|
|
| 37 |
# ---------------------------
|
| 38 |
@st.cache_resource
|
| 39 |
def load_text_model():
|
| 40 |
+
# model_name = "Maheentouqeer1/truthguard-fake-news-detector"
|
| 41 |
+
# NEW - replace with this well-trained model
|
| 42 |
+
model_name = "GonzaloA/fake-news-detector"
|
| 43 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 44 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 45 |
model_name, low_cpu_mem_usage=True
|
|
|
|
| 66 |
model.classifier[1] = nn.Linear(model.last_channel, 1)
|
| 67 |
model.eval()
|
| 68 |
return model
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# # ---------------------------
|
| 72 |
+
# # PREDICT TEXT
|
| 73 |
+
# # ---------------------------
|
| 74 |
+
# def predict_news(text, tokenizer, text_model):
|
| 75 |
+
# inputs = tokenizer(
|
| 76 |
+
# text, return_tensors="pt",
|
| 77 |
+
# truncation=True, padding=True, max_length=512
|
| 78 |
+
# )
|
| 79 |
+
# with torch.no_grad():
|
| 80 |
+
# outputs = text_model(**inputs)
|
| 81 |
+
# probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 82 |
+
# # ADD THIS to see raw scores
|
| 83 |
+
# st.write(f"Debug → Label 0 score: {probs[0][0]:.2f}, Label 1 score: {probs[0][1]:.2f}")
|
| 84 |
+
# prediction = torch.argmax(probs).item()
|
| 85 |
+
# confidence = torch.max(probs).item() * 100
|
| 86 |
+
# return prediction, confidence
|
| 87 |
def predict_news(text, tokenizer, text_model):
|
| 88 |
inputs = tokenizer(
|
| 89 |
text, return_tensors="pt",
|
|
|
|
| 92 |
with torch.no_grad():
|
| 93 |
outputs = text_model(**inputs)
|
| 94 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
|
|
|
|
|
|
| 95 |
prediction = torch.argmax(probs).item()
|
| 96 |
confidence = torch.max(probs).item() * 100
|
| 97 |
return prediction, confidence
|
|
|
|
| 98 |
# ---------------------------
|
| 99 |
# PREDICT IMAGE
|
| 100 |
# ---------------------------
|
|
|
|
| 131 |
pred, conf = predict_news(user_input, tokenizer, text_model)
|
| 132 |
if pred == 0:
|
| 133 |
st.error(f"⚠️ FAKE NEWS ({conf:.2f}%)")
|
| 134 |
+
elif pred == 1:
|
| 135 |
st.success(f"✅ REAL NEWS ({conf:.2f}%)")
|
| 136 |
+
else:
|
| 137 |
+
st.warning(f"🤔 UNCERTAIN ({conf:.2f}%)")
|
| 138 |
st.progress(int(conf))
|
| 139 |
|
| 140 |
with tab2:
|