MetaQu commited on
Commit
d0ca36e
·
verified ·
1 Parent(s): 393b319

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -32
app.py CHANGED
@@ -1,49 +1,32 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
- import numpy as np
5
 
6
- # ====== MODEL LIST ======
7
  model_ids = [
8
- "vikhyatk/moondream2", # butuh trust_remote_code=True
9
- "umm-maybe/synthetic-image-detector",
10
- "falconsai/nsfw_image_detection"
11
  ]
12
 
13
- # Load semua model dengan trust_remote_code
14
- detectors = [pipeline("image-classification", model=m, trust_remote_code=True) for m in model_ids]
15
 
16
- def detect_image(img: Image.Image):
17
  results = []
18
- scores = []
19
-
20
  for det in detectors:
21
- try:
22
- out = det(img)
23
- # Ambil label & skor tertinggi
24
- top = max(out, key=lambda x: x["score"])
25
- results.append(f"{det.model.config.name_or_path}: {top['label']} ({top['score']:.2f})")
26
- scores.append(top["score"] if "ai" in top["label"].lower() or "fake" in top["label"].lower() or "artificial" in top["label"].lower() else 1 - top["score"])
27
- except Exception as e:
28
- results.append(f"Error {det.model.config.name_or_path}: {str(e)}")
29
-
30
- if scores:
31
- avg_score = np.mean(scores) * 100
32
- else:
33
- avg_score = 0
34
-
35
- verdict = "AI" if avg_score > 50 else "Asli"
36
-
37
- return f"🔎 Hasil Deteksi: {verdict}\nPersentase AI: {avg_score:.2f}%\n\nDetail:\n" + "\n".join(results)
38
-
39
- # ====== GRADIO UI ======
40
- demo = gr.Interface(
41
  fn=detect_image,
42
  inputs=gr.Image(type="pil"),
43
  outputs="text",
44
  title="AI vs Real Image Detector",
45
- description="Upload foto untuk mendeteksi apakah itu AI-generated atau asli. Menggunakan 3 model publik dengan trust_remote_code=True."
46
  )
47
 
48
  if __name__ == "__main__":
49
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
 
4
 
5
+ # Model deteksi AI vs Asli (tanpa moondream2, biar tidak error trust_remote_code)
6
  model_ids = [
7
+ "umm-maybe/AI-image-detector",
8
+ "roberta-base-openai-detector"
 
9
  ]
10
 
11
+ detectors = [pipeline("image-classification", model=m) for m in model_ids]
 
12
 
13
+ def detect_image(image):
14
  results = []
 
 
15
  for det in detectors:
16
+ preds = det(image)
17
+ if isinstance(preds, list) and len(preds) > 0:
18
+ label = preds[0]["label"]
19
+ score = preds[0]["score"]
20
+ results.append(f"{label}: {score:.2f}")
21
+ return "\n".join(results)
22
+
23
+ iface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
24
  fn=detect_image,
25
  inputs=gr.Image(type="pil"),
26
  outputs="text",
27
  title="AI vs Real Image Detector",
28
+ description="Upload a photo to check if it's AI-generated or a real one using multiple models."
29
  )
30
 
31
  if __name__ == "__main__":
32
+ iface.launch()