TruthLens commited on
Commit
d59602f
·
verified ·
1 Parent(s): 9bdc558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
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 torchvision import models, transforms
6
  from transformers import pipeline, CLIPProcessor, CLIPModel
7
 
8
  app = Flask(__name__)
@@ -11,14 +11,14 @@ app = Flask(__name__)
11
  upload_folder = os.path.join('static', 'uploads')
12
  os.makedirs(upload_folder, exist_ok=True)
13
 
14
- # Updated Fake News Detection Models
15
  news_models = {
16
  "mrm8488": pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection"),
17
  "google-electra": pipeline("text-classification", model="google/electra-base-discriminator"),
18
  "bert-base": pipeline("text-classification", model="bert-base-uncased")
19
  }
20
 
21
- # Updated Image Model for AI vs. Human Detection
22
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
23
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
24
 
@@ -29,7 +29,7 @@ transform = transforms.Compose([
29
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
30
  ])
31
 
32
- # HTML Template with Model Selection and Explanations
33
  HTML_TEMPLATE = """
34
  <!DOCTYPE html>
35
  <html lang="en">
@@ -76,7 +76,7 @@ HTML_TEMPLATE = """
76
  <div class="result">
77
  <h2>📷 Image Detection Result:</h2>
78
  <p>{{ image_prediction }}</p>
79
- <p><strong>Explanation:</strong> The model compares visual features against known AI-generated and human-created image patterns. High feature uniformity often indicates AI generation, while more varied features suggest human-created content.</p>
80
  </div>
81
  {% endif %}
82
  </div>
@@ -116,12 +116,12 @@ def detect_image():
116
 
117
  with torch.no_grad():
118
  image_features = clip_model.get_image_features(**inputs)
119
- mean_feature_value = torch.mean(image_features).item()
120
- prediction = "AI-Generated" if mean_feature_value > 0 else "Human-Created"
121
 
122
  explanation = (
123
- f"Prediction: {prediction} (Feature Value: {mean_feature_value:.4f}). "
124
- "Higher feature uniformity suggests AI generation, while more variability indicates human creation."
125
  )
126
 
127
  return render_template_string(
@@ -130,4 +130,4 @@ def detect_image():
130
  )
131
 
132
  if __name__ == "__main__":
133
- app.run(host="0.0.0.0", port=7860) # Suitable for Hugging Face Spaces
 
2
  from flask import Flask, request, render_template_string
3
  from PIL import Image
4
  import torch
5
+ from torchvision import transforms
6
  from transformers import pipeline, CLIPProcessor, CLIPModel
7
 
8
  app = Flask(__name__)
 
11
  upload_folder = os.path.join('static', 'uploads')
12
  os.makedirs(upload_folder, exist_ok=True)
13
 
14
+ # Fake News Detection Models
15
  news_models = {
16
  "mrm8488": pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection"),
17
  "google-electra": pipeline("text-classification", model="google/electra-base-discriminator"),
18
  "bert-base": pipeline("text-classification", model="bert-base-uncased")
19
  }
20
 
21
+ # Image Model for AI vs. Human Detection
22
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
23
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
24
 
 
29
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
30
  ])
31
 
32
+ # HTML Template
33
  HTML_TEMPLATE = """
34
  <!DOCTYPE html>
35
  <html lang="en">
 
76
  <div class="result">
77
  <h2>📷 Image Detection Result:</h2>
78
  <p>{{ image_prediction }}</p>
79
+ <p><strong>Explanation:</strong> The model estimates image complexity by analyzing feature variability. Higher complexity typically indicates human-created content, while smoother, less varied features suggest AI generation.</p>
80
  </div>
81
  {% endif %}
82
  </div>
 
116
 
117
  with torch.no_grad():
118
  image_features = clip_model.get_image_features(**inputs)
119
+ feature_variance = torch.var(image_features).item()
120
+ prediction = "Human-Created" if feature_variance > 0.05 else "AI-Generated"
121
 
122
  explanation = (
123
+ f"Prediction: {prediction} (Feature Variance: {feature_variance:.4f}). "
124
+ "Higher variance indicates complex, diverse features typical of human-created images, while lower variance suggests smoother, AI-generated patterns."
125
  )
126
 
127
  return render_template_string(
 
130
  )
131
 
132
  if __name__ == "__main__":
133
+ app.run(host="0.0.0.0", port=7860)