File size: 5,202 Bytes
a911010
 
 
 
928e0f8
a911010
 
 
 
 
 
 
94a6ffa
ca9580f
 
 
 
 
928e0f8
 
d0c4209
 
a911010
94a6ffa
a911010
 
 
 
 
928e0f8
a911010
 
9bdc558
928e0f8
 
a911010
 
 
 
 
 
928e0f8
 
 
94a6ffa
 
 
 
 
 
928e0f8
 
 
 
 
 
94a6ffa
928e0f8
 
 
e064416
 
 
 
 
 
 
a911010
e064416
928e0f8
94a6ffa
a911010
 
 
 
 
 
 
 
 
 
 
928e0f8
 
 
94a6ffa
928e0f8
94a6ffa
 
 
 
 
928e0f8
 
94a6ffa
928e0f8
 
a911010
 
 
e064416
a911010
 
e064416
723860c
94a6ffa
723860c
 
a911010
 
723860c
928e0f8
a911010
928e0f8
 
a911010
928e0f8
 
 
a911010
 
928e0f8
723860c
a911010
928e0f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
from flask import Flask, request, render_template_string
from PIL import Image
import torch
from transformers import pipeline, CLIPProcessor, CLIPModel

app = Flask(__name__)

# Create the 'static/uploads' folder if it doesn't exist
upload_folder = os.path.join('static', 'uploads')
os.makedirs(upload_folder, exist_ok=True)

# Fake News Detection Models
news_models = {
    "mrm8488": pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection"),
    "google-electra": pipeline("text-classification", model="google/electra-base-discriminator"),
    "bert-base": pipeline("text-classification", model="bert-base-uncased")
}

# Image Detection Model (CLIP-based)
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

# HTML Template with both Fake News and Image Detection
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AI & News Detection</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f5f5f5; padding: 20px; }
        .container { background: white; padding: 30px; border-radius: 12px; max-width: 850px; margin: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
        textarea, select, input[type='file'] { width: 100%; padding: 12px; margin-top: 10px; border-radius: 8px; border: 1px solid #ccc; }
        button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; margin-top: 10px; }
        button:hover { background-color: #45a049; }
        .result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>๐Ÿ“ฐ Fake News Detection</h1>
        <form method="POST" action="/detect">
            <textarea name="text" placeholder="Enter news text..." required></textarea>
            <label for="model">Select Fake News Model:</label>
            <select name="model" required>
                <option value="mrm8488">MRM8488 (BERT-Tiny)</option>
                <option value="google-electra">Google Electra (Base Discriminator)</option>
                <option value="bert-base">BERT-Base Uncased</option>
            </select>
            <button type="submit">Detect News Authenticity</button>
        </form>

        {% if news_prediction %}
        <div class="result">
            <h2>๐Ÿง  News Detection Result:</h2>
            <p>{{ news_prediction }}</p>
        </div>
        {% endif %}

        <h1>๐Ÿ–ผ๏ธ AI vs. Human Image Detection</h1>
        <form method="POST" action="/detect_image" enctype="multipart/form-data">
            <input type="file" name="image" required>
            <button type="submit">Upload and Detect</button>
        </form>

        {% if image_prediction %}
        <div class="result">
            <h2>๐Ÿ“ท Image Detection Result:</h2>
            <p>{{ image_prediction|safe }}</p>
            <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>
        </div>
        {% endif %}
    </div>
</body>
</html>
"""

@app.route("/", methods=["GET"])
def home():
    return render_template_string(HTML_TEMPLATE)

@app.route("/detect", methods=["POST"])
def detect():
    text = request.form.get("text")
    model_key = request.form.get("model")

    if not text or model_key not in news_models:
        return render_template_string(HTML_TEMPLATE, news_prediction="Invalid input or model selection.")

    result = news_models[model_key](text)[0]
    label = "REAL" if result['label'].lower() in ["real", "label_1", "neutral"] else "FAKE"
    confidence = result['score'] * 100

    prediction_text = f"News is {label} (Confidence: {confidence:.2f}%)"
    return render_template_string(HTML_TEMPLATE, news_prediction=prediction_text)

@app.route("/detect_image", methods=["POST"])
def detect_image():
    if "image" not in request.files:
        return render_template_string(HTML_TEMPLATE, image_prediction="No image uploaded.")

    file = request.files["image"]
    img = Image.open(file).convert("RGB")

    # Compare with AI and Human prompts
    prompts = ["AI-generated image", "Human-created image"]
    inputs = clip_processor(text=prompts, images=img, return_tensors="pt", padding=True)

    with torch.no_grad():
        outputs = clip_model(**inputs)
        similarity = outputs.logits_per_image.softmax(dim=1).squeeze().tolist()

    ai_similarity, human_similarity = similarity
    prediction = "AI-Generated" if ai_similarity > human_similarity else "Human-Created"

    prediction_text = (
        f"Prediction: <strong>{prediction}</strong><br>"
        f"AI Similarity: {ai_similarity * 100:.2f}% | Human Similarity: {human_similarity * 100:.2f}%"
    )

    return render_template_string(HTML_TEMPLATE, image_prediction=prediction_text)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)