prithivMLmods commited on
Commit
5207507
·
verified ·
1 Parent(s): 24a8f23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -3,6 +3,8 @@ from transformers import ViTForImageClassification, ViTImageProcessor
3
  from PIL import Image
4
  import torch
5
  import os
 
 
6
 
7
  # Load model and processor
8
  model_name = "prithivMLmods/Deepfake-QualityAssess-88M"
@@ -10,11 +12,14 @@ model = ViTForImageClassification.from_pretrained(model_name)
10
  processor = ViTImageProcessor.from_pretrained(model_name)
11
 
12
  # Create output directories
13
- os.makedirs("Issue In Deepfake", exist_ok=True)
14
- os.makedirs("High Quality Deepfake", exist_ok=True)
 
 
 
15
 
16
  def deepfake_detection(images):
17
- """Predicts deepfake probability scores for multiple images and saves them in respective folders."""
18
  results = {}
19
 
20
  for image in images:
@@ -36,20 +41,28 @@ def deepfake_detection(images):
36
 
37
  # Save image in respective folder
38
  filename = f"{max_label}_{max_score:.3f}.png"
39
- save_path = os.path.join("Issue In Deepfake" if "issue" in max_label.lower() else "High Quality Deepfake", filename)
 
40
  image.save(save_path)
41
 
42
  results[image_name] = {"predictions": predictions, "saved_as": filename}
43
 
44
- return results
 
 
 
 
 
 
 
45
 
46
  # Create Gradio interface
47
  iface = gr.Interface(
48
  fn=deepfake_detection,
49
  inputs=gr.File(file_types=["image"], file_count="multiple", label="Upload Images"),
50
- outputs=gr.JSON(label="Prediction Scores"),
51
  title="Deepfake Quality Detection",
52
- description="Upload multiple images to check their deepfake probability scores. Images will be classified and stored accordingly."
53
  )
54
 
55
  # Launch the app
 
3
  from PIL import Image
4
  import torch
5
  import os
6
+ import shutil
7
+ import zipfile
8
 
9
  # Load model and processor
10
  model_name = "prithivMLmods/Deepfake-QualityAssess-88M"
 
12
  processor = ViTImageProcessor.from_pretrained(model_name)
13
 
14
  # Create output directories
15
+ issue_dir = "Issue In Deepfake"
16
+ high_quality_dir = "High Quality Deepfake"
17
+ zip_filename = "classified_images.zip"
18
+ os.makedirs(issue_dir, exist_ok=True)
19
+ os.makedirs(high_quality_dir, exist_ok=True)
20
 
21
  def deepfake_detection(images):
22
+ """Predicts deepfake probability scores for multiple images, saves them in respective folders, and returns a zip file."""
23
  results = {}
24
 
25
  for image in images:
 
41
 
42
  # Save image in respective folder
43
  filename = f"{max_label}_{max_score:.3f}.png"
44
+ save_dir = issue_dir if "issue" in max_label.lower() else high_quality_dir
45
+ save_path = os.path.join(save_dir, filename)
46
  image.save(save_path)
47
 
48
  results[image_name] = {"predictions": predictions, "saved_as": filename}
49
 
50
+ # Create a zip file
51
+ with zipfile.ZipFile(zip_filename, "w") as zipf:
52
+ for folder in [issue_dir, high_quality_dir]:
53
+ for root, _, files in os.walk(folder):
54
+ for file in files:
55
+ zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file)))
56
+
57
+ return results, zip_filename
58
 
59
  # Create Gradio interface
60
  iface = gr.Interface(
61
  fn=deepfake_detection,
62
  inputs=gr.File(file_types=["image"], file_count="multiple", label="Upload Images"),
63
+ outputs=[gr.JSON(label="Prediction Scores"), gr.File(label="Download Classified Images (ZIP)")],
64
  title="Deepfake Quality Detection",
65
+ description="Upload multiple images to check their deepfake probability scores. Images will be classified, stored, and available for download as a zip file."
66
  )
67
 
68
  # Launch the app