Delete app.py
Browse files
app.py
DELETED
|
@@ -1,147 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from flask import Flask, request, render_template_string
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import torch
|
| 5 |
-
from torchvision import models, transforms
|
| 6 |
-
import requests
|
| 7 |
-
from transformers import pipeline, CLIPProcessor, CLIPModel
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
|
| 11 |
-
# Create the 'static/uploads' folder if it doesn't exist
|
| 12 |
-
upload_folder = os.path.join('static', 'uploads')
|
| 13 |
-
os.makedirs(upload_folder, exist_ok=True)
|
| 14 |
-
|
| 15 |
-
# Load Fake News Detection Pipelines
|
| 16 |
-
news_models = {
|
| 17 |
-
"nlptown": pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
# Load Image Models for AI vs. Human Detection
|
| 21 |
-
clip_model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
|
| 22 |
-
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
| 23 |
-
|
| 24 |
-
ai_image_models = {
|
| 25 |
-
"hila-chefer": CLIPModel.from_pretrained("hila-chefer/ai-generated-image-detector"),
|
| 26 |
-
"openai": clip_model
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
# Image transformation pipeline
|
| 30 |
-
transform = transforms.Compose([
|
| 31 |
-
transforms.Resize((224, 224)),
|
| 32 |
-
transforms.ToTensor(),
|
| 33 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 34 |
-
])
|
| 35 |
-
|
| 36 |
-
# HTML Template with Model Selection
|
| 37 |
-
HTML_TEMPLATE = """
|
| 38 |
-
<!DOCTYPE html>
|
| 39 |
-
<html lang="en">
|
| 40 |
-
<head>
|
| 41 |
-
<meta charset="UTF-8">
|
| 42 |
-
<title>AI & News Detection</title>
|
| 43 |
-
<style>
|
| 44 |
-
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f5f5f5; padding: 20px; }
|
| 45 |
-
.container { background: white; padding: 30px; border-radius: 12px; max-width: 800px; margin: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
|
| 46 |
-
textarea, select { width: 100%; padding: 12px; margin-top: 10px; border-radius: 8px; border: 1px solid #ccc; }
|
| 47 |
-
button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; margin-top: 10px; }
|
| 48 |
-
button:hover { background-color: #45a049; }
|
| 49 |
-
.result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
|
| 50 |
-
</style>
|
| 51 |
-
</head>
|
| 52 |
-
<body>
|
| 53 |
-
<div class="container">
|
| 54 |
-
<h1>📰 Fake News Detection</h1>
|
| 55 |
-
<form method="POST" action="/detect">
|
| 56 |
-
<textarea name="text" placeholder="Enter news text..." required></textarea>
|
| 57 |
-
<label for="model">Select Fake News Model:</label>
|
| 58 |
-
<select name="model" required>
|
| 59 |
-
<option value="anishathalye">Anishathalye</option>
|
| 60 |
-
<option value="liam168">Liam168</option>
|
| 61 |
-
<option value="joeddav">Joeddav (Multilingual)</option>
|
| 62 |
-
<option value="nlptown">NLPTown (Sentiment-Based)</option>
|
| 63 |
-
</select>
|
| 64 |
-
<button type="submit">Detect News Authenticity</button>
|
| 65 |
-
</form>
|
| 66 |
-
|
| 67 |
-
{% if news_prediction %}
|
| 68 |
-
<div class="result">
|
| 69 |
-
<h2>🧠 News Detection Result:</h2>
|
| 70 |
-
<p>{{ news_prediction }}</p>
|
| 71 |
-
</div>
|
| 72 |
-
{% endif %}
|
| 73 |
-
|
| 74 |
-
<h1>🖼️ AI vs. Human Image Detection</h1>
|
| 75 |
-
<form method="POST" action="/detect_image" enctype="multipart/form-data">
|
| 76 |
-
<input type="file" name="image" required>
|
| 77 |
-
<label for="image_model">Select Image Detection Model:</label>
|
| 78 |
-
<select name="image_model" required>
|
| 79 |
-
<option value="osanseviero">Osanseviero (CLIP)</option>
|
| 80 |
-
<option value="hila-chefer">Hila-Chefer (AI Detector)</option>
|
| 81 |
-
<option value="openai">OpenAI (CLIP Large)</option>
|
| 82 |
-
</select>
|
| 83 |
-
<button type="submit">Upload and Detect</button>
|
| 84 |
-
</form>
|
| 85 |
-
|
| 86 |
-
{% if image_prediction %}
|
| 87 |
-
<div class="result">
|
| 88 |
-
<h2>📷 Image Detection Result:</h2>
|
| 89 |
-
<p>{{ image_prediction }}</p>
|
| 90 |
-
</div>
|
| 91 |
-
{% endif %}
|
| 92 |
-
</div>
|
| 93 |
-
</body>
|
| 94 |
-
</html>
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
-
@app.route("/", methods=["GET"])
|
| 98 |
-
def home():
|
| 99 |
-
return render_template_string(HTML_TEMPLATE)
|
| 100 |
-
|
| 101 |
-
@app.route("/detect", methods=["POST"])
|
| 102 |
-
def detect():
|
| 103 |
-
text = request.form.get("text")
|
| 104 |
-
model_key = request.form.get("model")
|
| 105 |
-
|
| 106 |
-
if not text or model_key not in news_models:
|
| 107 |
-
return render_template_string(HTML_TEMPLATE, news_prediction="Invalid input or model selection.")
|
| 108 |
-
|
| 109 |
-
result = news_models[model_key](text)[0]
|
| 110 |
-
label = "REAL" if result['label'] == "LABEL_1" else "FAKE"
|
| 111 |
-
confidence = result['score'] * 100
|
| 112 |
-
|
| 113 |
-
return render_template_string(
|
| 114 |
-
HTML_TEMPLATE,
|
| 115 |
-
news_prediction=f"News is {label} (Confidence: {confidence:.2f}%)"
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
@app.route("/detect_image", methods=["POST"])
|
| 119 |
-
def detect_image():
|
| 120 |
-
if "image" not in request.files:
|
| 121 |
-
return render_template_string(HTML_TEMPLATE, image_prediction="No image uploaded.")
|
| 122 |
-
|
| 123 |
-
file = request.files["image"]
|
| 124 |
-
model_key = request.form.get("image_model")
|
| 125 |
-
|
| 126 |
-
if not model_key or model_key not in ai_image_models:
|
| 127 |
-
return render_template_string(HTML_TEMPLATE, image_prediction="Invalid model selection.")
|
| 128 |
-
|
| 129 |
-
img = Image.open(file).convert("RGB")
|
| 130 |
-
inputs = clip_processor(images=img, return_tensors="pt")
|
| 131 |
-
|
| 132 |
-
with torch.no_grad():
|
| 133 |
-
image_features = ai_image_models[model_key].get_image_features(**inputs)
|
| 134 |
-
|
| 135 |
-
prediction = "AI-Generated" if torch.mean(image_features).item() > 0 else "Human-Created"
|
| 136 |
-
|
| 137 |
-
return render_template_string(
|
| 138 |
-
HTML_TEMPLATE,
|
| 139 |
-
image_prediction=f"Prediction: {prediction}"
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
if __name__ == "__main__":
|
| 143 |
-
app.run(host="0.0.0.0", port=7860) # Suitable for Hugging Face Spaces
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|