Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from PIL import Image
|
|
| 4 |
import torch
|
| 5 |
from torchvision import models, transforms
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
@@ -20,10 +21,13 @@ imagenet_class_labels = response.json()
|
|
| 20 |
resnet50_model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 21 |
resnet50_model.eval()
|
| 22 |
|
| 23 |
-
# Load ResNet18 for AI vs. Human detection
|
| 24 |
resnet18_model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
|
| 25 |
resnet18_model.eval()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
# Image transformation pipeline
|
| 28 |
transform = transforms.Compose([
|
| 29 |
transforms.Resize((224, 224)),
|
|
@@ -31,62 +35,43 @@ transform = transforms.Compose([
|
|
| 31 |
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 32 |
])
|
| 33 |
|
| 34 |
-
# HTML Template with improved UI
|
| 35 |
HTML_TEMPLATE = """
|
| 36 |
<!DOCTYPE html>
|
| 37 |
<html lang="en">
|
| 38 |
<head>
|
| 39 |
<meta charset="UTF-8">
|
| 40 |
-
<title>AI &
|
| 41 |
<style>
|
| 42 |
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f5f5f5; padding: 20px; }
|
| 43 |
.container { background: white; padding: 30px; border-radius: 12px; max-width: 750px; margin: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
|
| 44 |
h1, h2 { color: #333; }
|
| 45 |
-
textarea
|
| 46 |
button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; }
|
| 47 |
button:hover { background-color: #45a049; }
|
| 48 |
.result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
|
| 49 |
-
ul { text-align: left; }
|
| 50 |
</style>
|
| 51 |
</head>
|
| 52 |
<body>
|
| 53 |
<div class="container">
|
| 54 |
-
<h1>📰 Fake News
|
| 55 |
<form method="POST" action="/detect">
|
| 56 |
<textarea name="text" placeholder="Enter news text..." required></textarea>
|
| 57 |
<button type="submit">Detect News Authenticity</button>
|
| 58 |
</form>
|
| 59 |
|
| 60 |
-
|
| 61 |
-
<form method="POST" action="/detect_image" enctype="multipart/form-data">
|
| 62 |
-
<input type="file" name="image" required>
|
| 63 |
-
<button type="submit">Upload and Analyze</button>
|
| 64 |
-
</form>
|
| 65 |
-
|
| 66 |
-
<div style="margin-top: 30px;">
|
| 67 |
-
<h2>🤖 What is ResNet50?</h2>
|
| 68 |
-
<p>ResNet50 is a 50-layer deep convolutional neural network designed for image classification tasks. It can recognize thousands of objects from the ImageNet dataset.</p>
|
| 69 |
-
</div>
|
| 70 |
-
|
| 71 |
-
{% if ai_prediction %}
|
| 72 |
<div class="result">
|
| 73 |
-
<h2>🧠
|
| 74 |
-
<p>{{
|
| 75 |
-
<p><strong>Interpretation:</strong> This result indicates whether the
|
| 76 |
</div>
|
| 77 |
{% endif %}
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
<
|
| 82 |
-
<ul>
|
| 83 |
-
{% for result in classification_results %}
|
| 84 |
-
<li>• {{ result.label }} ({{ (result.score * 100) | round(2) }}%) - Detected object category.</li>
|
| 85 |
-
{% endfor %}
|
| 86 |
-
</ul>
|
| 87 |
-
<p><strong>Interpretation:</strong> The model predicts the most probable object categories in the uploaded image along with confidence scores. Higher percentages indicate stronger matches.</p>
|
| 88 |
</div>
|
| 89 |
-
{% endif %}
|
| 90 |
</div>
|
| 91 |
</body>
|
| 92 |
</html>
|
|
@@ -99,43 +84,21 @@ def home():
|
|
| 99 |
@app.route("/detect", methods=["POST"])
|
| 100 |
def detect():
|
| 101 |
text = request.form.get("text")
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if "
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
file = request.files["image"]
|
| 111 |
-
img_path = os.path.join(upload_folder, file.filename)
|
| 112 |
-
file.save(img_path)
|
| 113 |
-
|
| 114 |
-
img = Image.open(img_path).convert("RGB")
|
| 115 |
-
img_tensor = transform(img).unsqueeze(0)
|
| 116 |
-
|
| 117 |
-
# AI vs. Human detection
|
| 118 |
-
with torch.no_grad():
|
| 119 |
-
ai_output = resnet18_model(img_tensor)
|
| 120 |
-
ai_confidence = torch.softmax(ai_output, dim=1).max().item()
|
| 121 |
-
ai_label = "AI-Generated" if ai_confidence > 0.55 else "Human-Created"
|
| 122 |
-
|
| 123 |
-
# Object classification with ResNet50
|
| 124 |
-
with torch.no_grad():
|
| 125 |
-
outputs = resnet50_model(img_tensor)
|
| 126 |
-
probs = torch.softmax(outputs, dim=1)[0]
|
| 127 |
-
top5_probs, top5_indices = torch.topk(probs, 5)
|
| 128 |
-
classification_results = [
|
| 129 |
-
{"label": imagenet_class_labels[idx], "score": prob.item()} for idx, prob in zip(top5_indices, top5_probs)
|
| 130 |
-
]
|
| 131 |
|
| 132 |
return render_template_string(
|
| 133 |
HTML_TEMPLATE,
|
| 134 |
-
|
| 135 |
-
classification_results=classification_results
|
| 136 |
)
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
-
app.run(host="0.0.0.0", port=7860) #
|
|
|
|
| 140 |
|
| 141 |
|
|
|
|
| 4 |
import torch
|
| 5 |
from torchvision import models, transforms
|
| 6 |
import requests
|
| 7 |
+
from transformers import pipeline
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
|
|
| 21 |
resnet50_model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 22 |
resnet50_model.eval()
|
| 23 |
|
| 24 |
+
# Load ResNet18 for AI vs. Human detection
|
| 25 |
resnet18_model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
|
| 26 |
resnet18_model.eval()
|
| 27 |
|
| 28 |
+
# Load fake news detection model from Hugging Face
|
| 29 |
+
news_classifier = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
|
| 30 |
+
|
| 31 |
# Image transformation pipeline
|
| 32 |
transform = transforms.Compose([
|
| 33 |
transforms.Resize((224, 224)),
|
|
|
|
| 35 |
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 36 |
])
|
| 37 |
|
| 38 |
+
# HTML Template with improved UI
|
| 39 |
HTML_TEMPLATE = """
|
| 40 |
<!DOCTYPE html>
|
| 41 |
<html lang="en">
|
| 42 |
<head>
|
| 43 |
<meta charset="UTF-8">
|
| 44 |
+
<title>AI & News Detection</title>
|
| 45 |
<style>
|
| 46 |
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f5f5f5; padding: 20px; }
|
| 47 |
.container { background: white; padding: 30px; border-radius: 12px; max-width: 750px; margin: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
|
| 48 |
h1, h2 { color: #333; }
|
| 49 |
+
textarea { width: 100%; padding: 12px; margin-top: 10px; border-radius: 8px; border: 1px solid #ccc; }
|
| 50 |
button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; }
|
| 51 |
button:hover { background-color: #45a049; }
|
| 52 |
.result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
|
|
|
|
| 53 |
</style>
|
| 54 |
</head>
|
| 55 |
<body>
|
| 56 |
<div class="container">
|
| 57 |
+
<h1>📰 Fake News Detection</h1>
|
| 58 |
<form method="POST" action="/detect">
|
| 59 |
<textarea name="text" placeholder="Enter news text..." required></textarea>
|
| 60 |
<button type="submit">Detect News Authenticity</button>
|
| 61 |
</form>
|
| 62 |
|
| 63 |
+
{% if news_prediction %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
<div class="result">
|
| 65 |
+
<h2>🧠 News Detection Result:</h2>
|
| 66 |
+
<p>{{ news_prediction }}</p>
|
| 67 |
+
<p><strong>Interpretation:</strong> This result indicates whether the submitted news text is likely real or fake. Higher confidence suggests stronger model certainty.</p>
|
| 68 |
</div>
|
| 69 |
{% endif %}
|
| 70 |
|
| 71 |
+
<div style="margin-top: 30px;">
|
| 72 |
+
<h2>🤖 What is ResNet50?</h2>
|
| 73 |
+
<p>ResNet50 is a 50-layer deep convolutional neural network designed for image classification tasks. It can recognize thousands of objects from the ImageNet dataset.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
</div>
|
|
|
|
| 75 |
</div>
|
| 76 |
</body>
|
| 77 |
</html>
|
|
|
|
| 84 |
@app.route("/detect", methods=["POST"])
|
| 85 |
def detect():
|
| 86 |
text = request.form.get("text")
|
| 87 |
+
if not text:
|
| 88 |
+
return render_template_string(HTML_TEMPLATE, news_prediction="No text provided.")
|
| 89 |
+
|
| 90 |
+
# Use the model for prediction
|
| 91 |
+
result = news_classifier(text)[0]
|
| 92 |
+
label = "REAL" if result['label'] == "LABEL_1" else "FAKE"
|
| 93 |
+
confidence = result['score'] * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
return render_template_string(
|
| 96 |
HTML_TEMPLATE,
|
| 97 |
+
news_prediction=f"News is {label} (Confidence: {confidence:.2f}%)"
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
app.run(host="0.0.0.0", port=7860) # Suitable for Hugging Face Spaces
|
| 102 |
+
|
| 103 |
|
| 104 |
|