Zeyadd-Mostaffa commited on
Commit
6b9b555
·
verified ·
1 Parent(s): 6de55da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -40
app.py CHANGED
@@ -1,40 +1,28 @@
1
- import gradio as gr
2
- import numpy as np
3
- from tensorflow.keras.models import load_model
4
- from PIL import Image
5
- import requests
6
- import h5py
7
- import os
8
-
9
- # Download model only if not already present
10
- model_path = "xception_model.h5"
11
- if not os.path.exists(model_path):
12
- url = "https://huggingface.co/Zeyadd-Mostaffa/cv_GP/resolve/main/xception_model.h5"
13
- with open(model_path, "wb") as f:
14
- f.write(requests.get(url).content)
15
-
16
- # Load model
17
- model = load_model(model_path)
18
-
19
- # Preprocess function
20
- def preprocess_image(image):
21
- image = image.resize((150, 150)).convert("RGB")
22
- arr = np.array(image) / 255.0
23
- return np.expand_dims(arr, axis=0)
24
-
25
- # Prediction function
26
- def predict(image):
27
- img = preprocess_image(image)
28
- prob = model.predict(img)[0][0]
29
- label = "REAL" if prob >= 0.5 else "FAKE"
30
- return {"REAL": 1 - prob, "FAKE": prob}, f"Prediction: {label}"
31
-
32
- # Gradio UI
33
- demo = gr.Interface(
34
- fn=predict,
35
- inputs=gr.Image(type="pil"),
36
- outputs=[gr.Label(num_top_classes=2), gr.Text()],
37
- title="Deepfake Detection (Xception Model)"
38
- )
39
-
40
- demo.launch()
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from tensorflow.keras.models import load_model
4
+ from PIL import Image
5
+
6
+ # Load pre-uploaded model
7
+ model = load_model("xception_model.h5")
8
+
9
+ def preprocess_image(image):
10
+ image = image.resize((299, 299)).convert("RGB") # ✅ Fix: match Xception input shape
11
+ img_array = np.array(image) / 255.0
12
+ return np.expand_dims(img_array, axis=0)
13
+
14
+ def predict(image):
15
+ img = preprocess_image(image)
16
+ prob = model.predict(img)[0][0]
17
+ label = "REAL" if prob >= 0.5 else "FAKE"
18
+ return {"REAL": 1 - prob, "FAKE": prob}, f"Prediction: {label}"
19
+
20
+ demo = gr.Interface(
21
+ fn=predict,
22
+ inputs=gr.Image(type="pil"),
23
+ outputs=[gr.Label(num_top_classes=2), gr.Text()],
24
+ title="Deepfake Detection (Xception Model)"
25
+ )
26
+
27
+ demo.launch()
28
+