Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
from flask import Flask, request, render_template_string
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
-
from transformers import CLIPProcessor, CLIPModel
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
@@ -10,28 +10,54 @@ app = Flask(__name__)
|
|
| 10 |
upload_folder = os.path.join('static', 'uploads')
|
| 11 |
os.makedirs(upload_folder, exist_ok=True)
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 15 |
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 16 |
|
| 17 |
-
# HTML Template with
|
| 18 |
HTML_TEMPLATE = """
|
| 19 |
<!DOCTYPE html>
|
| 20 |
<html lang="en">
|
| 21 |
<head>
|
| 22 |
<meta charset="UTF-8">
|
| 23 |
-
<title>AI
|
| 24 |
<style>
|
| 25 |
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f5f5f5; padding: 20px; }
|
| 26 |
.container { background: white; padding: 30px; border-radius: 12px; max-width: 850px; margin: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
|
| 27 |
-
input[type=
|
| 28 |
-
button { background-color: #4CAF50; color: white; border: none; font-size: 16px;
|
| 29 |
button:hover { background-color: #45a049; }
|
| 30 |
.result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
|
| 31 |
</style>
|
| 32 |
</head>
|
| 33 |
<body>
|
| 34 |
<div class="container">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
<h1>🖼️ AI vs. Human Image Detection</h1>
|
| 36 |
<form method="POST" action="/detect_image" enctype="multipart/form-data">
|
| 37 |
<input type="file" name="image" required>
|
|
@@ -41,8 +67,8 @@ HTML_TEMPLATE = """
|
|
| 41 |
{% if image_prediction %}
|
| 42 |
<div class="result">
|
| 43 |
<h2>📷 Image Detection Result:</h2>
|
| 44 |
-
<p>{{ image_prediction }}</p>
|
| 45 |
-
<p><strong>Explanation:</strong> The model compares the image
|
| 46 |
</div>
|
| 47 |
{% endif %}
|
| 48 |
</div>
|
|
@@ -54,6 +80,21 @@ HTML_TEMPLATE = """
|
|
| 54 |
def home():
|
| 55 |
return render_template_string(HTML_TEMPLATE)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.route("/detect_image", methods=["POST"])
|
| 58 |
def detect_image():
|
| 59 |
if "image" not in request.files:
|
|
@@ -62,26 +103,23 @@ def detect_image():
|
|
| 62 |
file = request.files["image"]
|
| 63 |
img = Image.open(file).convert("RGB")
|
| 64 |
|
| 65 |
-
#
|
| 66 |
prompts = ["AI-generated image", "Human-created image"]
|
| 67 |
-
|
| 68 |
-
# Process image and text inputs
|
| 69 |
inputs = clip_processor(text=prompts, images=img, return_tensors="pt", padding=True)
|
| 70 |
|
| 71 |
with torch.no_grad():
|
| 72 |
outputs = clip_model(**inputs)
|
| 73 |
-
|
| 74 |
-
probs = logits_per_image.softmax(dim=0)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
prediction = "AI-Generated" if
|
| 78 |
|
| 79 |
-
|
| 80 |
-
f"Prediction: {prediction}
|
| 81 |
-
f"AI Similarity: {
|
| 82 |
)
|
| 83 |
|
| 84 |
-
return render_template_string(HTML_TEMPLATE, image_prediction=
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
from flask import Flask, request, render_template_string
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
from transformers import pipeline, CLIPProcessor, CLIPModel
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
|
|
| 10 |
upload_folder = os.path.join('static', 'uploads')
|
| 11 |
os.makedirs(upload_folder, exist_ok=True)
|
| 12 |
|
| 13 |
+
# Fake News Detection Models
|
| 14 |
+
news_models = {
|
| 15 |
+
"mrm8488": pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection"),
|
| 16 |
+
"google-electra": pipeline("text-classification", model="google/electra-base-discriminator"),
|
| 17 |
+
"bert-base": pipeline("text-classification", model="bert-base-uncased")
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
# Image Detection Model (CLIP-based)
|
| 21 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 22 |
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 23 |
|
| 24 |
+
# HTML Template with both Fake News and Image Detection
|
| 25 |
HTML_TEMPLATE = """
|
| 26 |
<!DOCTYPE html>
|
| 27 |
<html lang="en">
|
| 28 |
<head>
|
| 29 |
<meta charset="UTF-8">
|
| 30 |
+
<title>AI & News Detection</title>
|
| 31 |
<style>
|
| 32 |
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f5f5f5; padding: 20px; }
|
| 33 |
.container { background: white; padding: 30px; border-radius: 12px; max-width: 850px; margin: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
|
| 34 |
+
textarea, select, input[type='file'] { width: 100%; padding: 12px; margin-top: 10px; border-radius: 8px; border: 1px solid #ccc; }
|
| 35 |
+
button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; margin-top: 10px; }
|
| 36 |
button:hover { background-color: #45a049; }
|
| 37 |
.result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
|
| 38 |
</style>
|
| 39 |
</head>
|
| 40 |
<body>
|
| 41 |
<div class="container">
|
| 42 |
+
<h1>📰 Fake News Detection</h1>
|
| 43 |
+
<form method="POST" action="/detect">
|
| 44 |
+
<textarea name="text" placeholder="Enter news text..." required></textarea>
|
| 45 |
+
<label for="model">Select Fake News Model:</label>
|
| 46 |
+
<select name="model" required>
|
| 47 |
+
<option value="mrm8488">MRM8488 (BERT-Tiny)</option>
|
| 48 |
+
<option value="google-electra">Google Electra (Base Discriminator)</option>
|
| 49 |
+
<option value="bert-base">BERT-Base Uncased</option>
|
| 50 |
+
</select>
|
| 51 |
+
<button type="submit">Detect News Authenticity</button>
|
| 52 |
+
</form>
|
| 53 |
+
|
| 54 |
+
{% if news_prediction %}
|
| 55 |
+
<div class="result">
|
| 56 |
+
<h2>🧠 News Detection Result:</h2>
|
| 57 |
+
<p>{{ news_prediction }}</p>
|
| 58 |
+
</div>
|
| 59 |
+
{% endif %}
|
| 60 |
+
|
| 61 |
<h1>🖼️ AI vs. Human Image Detection</h1>
|
| 62 |
<form method="POST" action="/detect_image" enctype="multipart/form-data">
|
| 63 |
<input type="file" name="image" required>
|
|
|
|
| 67 |
{% if image_prediction %}
|
| 68 |
<div class="result">
|
| 69 |
<h2>📷 Image Detection Result:</h2>
|
| 70 |
+
<p>{{ image_prediction|safe }}</p>
|
| 71 |
+
<p><strong>Explanation:</strong> The model compares the uploaded image against the text prompts "AI-generated image" and "Human-created image" to determine similarity. Higher similarity to the AI prompt suggests an AI-generated image, and vice versa.</p>
|
| 72 |
</div>
|
| 73 |
{% endif %}
|
| 74 |
</div>
|
|
|
|
| 80 |
def home():
|
| 81 |
return render_template_string(HTML_TEMPLATE)
|
| 82 |
|
| 83 |
+
@app.route("/detect", methods=["POST"])
|
| 84 |
+
def detect():
|
| 85 |
+
text = request.form.get("text")
|
| 86 |
+
model_key = request.form.get("model")
|
| 87 |
+
|
| 88 |
+
if not text or model_key not in news_models:
|
| 89 |
+
return render_template_string(HTML_TEMPLATE, news_prediction="Invalid input or model selection.")
|
| 90 |
+
|
| 91 |
+
result = news_models[model_key](text)[0]
|
| 92 |
+
label = "REAL" if result['label'].lower() in ["real", "label_1", "neutral"] else "FAKE"
|
| 93 |
+
confidence = result['score'] * 100
|
| 94 |
+
|
| 95 |
+
prediction_text = f"News is <strong>{label}</strong> (Confidence: {confidence:.2f}%)"
|
| 96 |
+
return render_template_string(HTML_TEMPLATE, news_prediction=prediction_text)
|
| 97 |
+
|
| 98 |
@app.route("/detect_image", methods=["POST"])
|
| 99 |
def detect_image():
|
| 100 |
if "image" not in request.files:
|
|
|
|
| 103 |
file = request.files["image"]
|
| 104 |
img = Image.open(file).convert("RGB")
|
| 105 |
|
| 106 |
+
# Compare with AI and Human prompts
|
| 107 |
prompts = ["AI-generated image", "Human-created image"]
|
|
|
|
|
|
|
| 108 |
inputs = clip_processor(text=prompts, images=img, return_tensors="pt", padding=True)
|
| 109 |
|
| 110 |
with torch.no_grad():
|
| 111 |
outputs = clip_model(**inputs)
|
| 112 |
+
similarity = outputs.logits_per_image.softmax(dim=1).squeeze().tolist()
|
|
|
|
| 113 |
|
| 114 |
+
ai_similarity, human_similarity = similarity
|
| 115 |
+
prediction = "AI-Generated" if ai_similarity > human_similarity else "Human-Created"
|
| 116 |
|
| 117 |
+
prediction_text = (
|
| 118 |
+
f"Prediction: <strong>{prediction}</strong><br>"
|
| 119 |
+
f"AI Similarity: {ai_similarity * 100:.2f}% | Human Similarity: {human_similarity * 100:.2f}%"
|
| 120 |
)
|
| 121 |
|
| 122 |
+
return render_template_string(HTML_TEMPLATE, image_prediction=prediction_text)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
app.run(host="0.0.0.0", port=7860)
|