nicolasrl commited on
Commit
e6030b4
·
verified ·
1 Parent(s): e618b3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -1,37 +1,32 @@
1
- import gradio as gr
2
- from transformers import AutoModelForImageClassification, AutoImageProcessor
3
- from PIL import Image
4
- import torch
5
  import os
6
-
7
- MODEL_ID = "nicolasrl/deepfake_vs_real_ViTlarge"
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
-
10
- model = AutoModelForImageClassification.from_pretrained(MODEL_ID, use_auth_token=HF_TOKEN)
11
  processor = AutoImageProcessor.from_pretrained(MODEL_ID, use_auth_token=HF_TOKEN)
12
-
13
- def predict(image_path: str):
14
- image = Image.open(image_path)
15
-
16
- inputs = processor(images=image, return_tensors="pt")
17
-
18
- with torch.no_grad():
19
- outputs = model(**inputs)
20
-
21
- logits = outputs.logits
22
- probs = torch.nn.functional.softmax(logits, dim=-1)[0]
23
- results = {model.config.id2label[i]: float(probs[i]) for i in range(len(probs))}
24
-
25
  return dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
26
-
27
- iface = gr.Interface(
28
- fn=predict,
29
- inputs=gr.Image(type="filepath"), #PIL
30
- outputs=gr.Label(num_top_classes=2),
31
- title="Deepfake o Real",
32
- description="Sube una imagen y el modelo hace magia para predecir si es Deepfake o Real",
33
- api_name="predict"
34
  )
35
-
36
- if __name__ == "__main__":
37
  iface.launch(show_error=True)
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
3
+ from PIL import Image
4
+ import torch
5
  import os
6
+
7
+ MODEL_ID = "nicolasrl/deepfake_vs_real_ViTlarge"
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
+
10
+ model = AutoModelForImageClassification.from_pretrained(MODEL_ID, use_auth_token=HF_TOKEN)
11
  processor = AutoImageProcessor.from_pretrained(MODEL_ID, use_auth_token=HF_TOKEN)
12
+
13
+ def predict(image: Image.Image):
14
+ inputs = processor(images=image, return_tensors="pt")
15
+ with torch.no_grad():
16
+ outputs = model(**inputs)
17
+ logits = outputs.logits
18
+ probs = torch.nn.functional.softmax(logits, dim=-1)[0]
19
+ results = {model.config.id2label[i]: float(probs[i]) for i in range(len(probs))}
 
 
 
 
 
20
  return dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
21
+
22
+ iface = gr.Interface(
23
+ fn=predict,
24
+ inputs=gr.Image(type="pil"),
25
+ outputs=gr.Label(num_top_classes=2),
26
+ title="Deepfake o Real",
27
+ description="Sube una imagen y el modelo hace magia para predecir si es Deepfake o Real",
28
+ api_name="predict"
29
  )
30
+
31
+ if __name__ == "__main__":
32
  iface.launch(show_error=True)