TruthLens commited on
Commit
94a6ffa
Β·
verified Β·
1 Parent(s): ca9580f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -10,6 +10,7 @@ app = Flask(__name__)
10
  upload_folder = os.path.join('static', 'uploads')
11
  os.makedirs(upload_folder, exist_ok=True)
12
 
 
13
  news_models = {
14
  "mrm8488": pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection"),
15
  "google-electra": pipeline("text-classification", model="google/electra-base-discriminator"),
@@ -20,7 +21,7 @@ news_models = {
20
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
21
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
22
 
23
- # HTML Template with enhanced explanations
24
  HTML_TEMPLATE = """
25
  <!DOCTYPE html>
26
  <html lang="en">
@@ -41,14 +42,19 @@ HTML_TEMPLATE = """
41
  <h1>πŸ“° Fake News Detection</h1>
42
  <form method="POST" action="/detect">
43
  <textarea name="text" placeholder="Enter news text..." required></textarea>
 
 
 
 
 
 
44
  <button type="submit">Detect News Authenticity</button>
45
  </form>
46
 
47
  {% if news_prediction %}
48
  <div class="result">
49
  <h2>🧠 News Detection Result:</h2>
50
- <p>{{ news_prediction|safe }}</p>
51
- <p><strong>Explanation:</strong> The model analyzes the text and classifies it as REAL or FAKE based on linguistic patterns, fact alignment, and contextual cues. High confidence indicates stronger evidence for the classification.</p>
52
  </div>
53
  {% endif %}
54
 
@@ -62,7 +68,7 @@ HTML_TEMPLATE = """
62
  <div class="result">
63
  <h2>πŸ“· Image Detection Result:</h2>
64
  <p>{{ image_prediction|safe }}</p>
65
- <p><strong>Explanation:</strong> The model compares the uploaded image against prompts like "AI-generated image" and "Human-created image." Higher similarity to the AI prompt suggests an AI-generated image, and vice versa.</p>
66
  </div>
67
  {% endif %}
68
  </div>
@@ -77,17 +83,16 @@ def home():
77
  @app.route("/detect", methods=["POST"])
78
  def detect():
79
  text = request.form.get("text")
80
- if not text:
81
- return render_template_string(HTML_TEMPLATE, news_prediction="Invalid input.")
82
 
83
- result = news_model(text)[0]
84
- label = "REAL" if result['label'].lower() in ["entailment", "neutral"] else "FAKE"
 
 
 
85
  confidence = result['score'] * 100
86
 
87
- prediction_text = (
88
- f"News is <strong>{label}</strong> (Confidence: {confidence:.2f}%)<br>"
89
- f"Reasoning: The model evaluated the likelihood that the provided text aligns with factual information versus being contradictory or fabricated."
90
- )
91
  return render_template_string(HTML_TEMPLATE, news_prediction=prediction_text)
92
 
93
  @app.route("/detect_image", methods=["POST"])
@@ -98,6 +103,7 @@ def detect_image():
98
  file = request.files["image"]
99
  img = Image.open(file).convert("RGB")
100
 
 
101
  prompts = ["AI-generated image", "Human-created image"]
102
  inputs = clip_processor(text=prompts, images=img, return_tensors="pt", padding=True)
103
 
 
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"),
 
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">
 
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
 
 
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>
 
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 {label} (Confidence: {confidence:.2f}%)"
 
 
 
96
  return render_template_string(HTML_TEMPLATE, news_prediction=prediction_text)
97
 
98
  @app.route("/detect_image", methods=["POST"])
 
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