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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -1,28 +1,35 @@
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
 
 
1
  import gradio as gr
2
  import numpy as np
3
  from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
  from PIL import Image
6
+ from huggingface_hub import hf_hub_download
7
 
8
+ # Download the model from the Hugging Face model hub
9
+ model_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/cv_GP", filename="xception_model.h5")
10
+ model = load_model(model_path)
 
 
 
 
11
 
12
+ # Preprocessing and prediction
13
  def predict(image):
14
+ # Resize image to expected shape (299x299x3 for Xception)
15
+ image = image.resize((299, 299))
16
+ image = img_to_array(image)
17
+ image = np.expand_dims(image, axis=0)
18
+ image = image / 255.0 # Normalize
19
+
20
+ prob = model.predict(image)[0][0]
21
+ label = "Fake" if prob > 0.5 else "Real"
22
+ confidence = round(float(prob if prob > 0.5 else 1 - prob), 3)
23
+ return f"{label} ({confidence})"
24
 
25
+ # Gradio UI
26
+ iface = gr.Interface(
27
  fn=predict,
28
  inputs=gr.Image(type="pil"),
29
+ outputs=gr.Text(),
30
  title="Deepfake Detection (Xception Model)"
31
  )
32
 
33
+ iface.launch()
34
+
35