Jabrave commited on
Commit
73a3b21
·
verified ·
1 Parent(s): cc31707

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -5
app.py CHANGED
@@ -4,33 +4,56 @@ from PIL import Image
4
  import torch
5
  import gradio as gr
6
 
7
- model = AutoModelForImageClassification.from_pretrained("./")
8
- processor = AutoImageProcessor.from_pretrained("./")
 
 
 
 
 
 
 
 
 
9
 
10
- labels = ["real", "fake"]
11
 
12
  def predict(image):
 
 
13
  image = Image.fromarray(image)
14
 
 
15
  inputs = processor(images=image, return_tensors="pt")
16
 
 
17
  with torch.no_grad():
18
  outputs = model(**inputs)
19
 
 
20
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
21
 
 
22
  pred = probs.argmax().item()
 
 
23
  confidence = probs[0][pred].item()
24
 
 
 
 
25
  return {
26
- "label": labels[pred],
27
  "confidence": round(confidence * 100, 2)
28
  }
29
 
 
30
  demo = gr.Interface(
31
  fn=predict,
32
  inputs=gr.Image(),
33
- outputs=gr.JSON()
 
 
34
  )
35
 
36
  demo.launch()
 
4
  import torch
5
  import gradio as gr
6
 
7
+ # โหลดโมเดล
8
+ model = AutoModelForImageClassification.from_pretrained(
9
+ "Jabrave/deepfake-detector"
10
+ )
11
+
12
+ processor = AutoImageProcessor.from_pretrained(
13
+ "Jabrave/deepfake-detector"
14
+ )
15
+
16
+ # โหลด labels จาก config อัตโนมัติ
17
+ id2label = model.config.id2label
18
 
19
+ print("Loaded labels:", id2label)
20
 
21
  def predict(image):
22
+
23
+ # แปลงเป็น PIL Image
24
  image = Image.fromarray(image)
25
 
26
+ # preprocess
27
  inputs = processor(images=image, return_tensors="pt")
28
 
29
+ # inference
30
  with torch.no_grad():
31
  outputs = model(**inputs)
32
 
33
+ # softmax
34
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
35
 
36
+ # class ที่มั่นใจสุด
37
  pred = probs.argmax().item()
38
+
39
+ # confidence
40
  confidence = probs[0][pred].item()
41
 
42
+ # label จริงจาก model
43
+ label = id2label[pred]
44
+
45
  return {
46
+ "label": label,
47
  "confidence": round(confidence * 100, 2)
48
  }
49
 
50
+ # UI
51
  demo = gr.Interface(
52
  fn=predict,
53
  inputs=gr.Image(),
54
+ outputs=gr.JSON(),
55
+ title="Deepfake Detector",
56
+ description="Upload image to detect fake or real"
57
  )
58
 
59
  demo.launch()