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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -5,24 +5,26 @@ 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"),
 
5
  from PIL import Image
6
  from huggingface_hub import hf_hub_download
7
 
8
+ # Download and load the model
9
  model_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/cv_GP", filename="xception_model.h5")
10
  model = load_model(model_path)
11
 
12
+ # Inference function
13
  def predict(image):
 
14
  image = image.resize((299, 299))
15
  image = img_to_array(image)
16
  image = np.expand_dims(image, axis=0)
17
+ image = image / 255.0 # normalize to match training
18
 
19
  prob = model.predict(image)[0][0]
20
+
21
+ # Based on your notebook: label 0 = Fake, label 1 = Real
22
+ label = "Real" if prob > 0.5 else "Fake"
23
  confidence = round(float(prob if prob > 0.5 else 1 - prob), 3)
24
+
25
  return f"{label} ({confidence})"
26
 
27
+ # Gradio interface
28
  iface = gr.Interface(
29
  fn=predict,
30
  inputs=gr.Image(type="pil"),