arsath-sm commited on
Commit
61617c0
1 Parent(s): b113f1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -1,38 +1,35 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
  from huggingface_hub import hf_hub_download
6
- import os
7
 
 
8
  def load_model_from_hub(repo_id, filename):
9
  model_path = hf_hub_download(repo_id=repo_id, filename=filename)
10
  return tf.keras.models.load_model(model_path)
11
 
12
- # Load the models
13
- model1 = load_model_from_hub("arsath-sm/face_classification_model1", "face_classification_model1.h5")
14
- model2 = load_model_from_hub("arsath-sm/face_classification_model2", "face_classification_model2.h5")
15
 
16
- # Preprocess the image
17
- def preprocess_image(img):
18
- img = img.resize((224, 224)) # Adjust size as needed
19
- img = image.img_to_array(img)
20
- img = np.expand_dims(img, axis=0)
21
  img = img / 255.0
22
- return img
23
 
24
- # Make predictions
25
- def predict(img):
26
- preprocessed_img = preprocess_image(img)
27
 
28
- prediction1 = model1.predict(preprocessed_img)[0][0]
29
- prediction2 = model2.predict(preprocessed_img)[0][0]
 
30
 
31
- result1 = "Real" if prediction1 > 0.5 else "Fake"
32
- result2 = "Real" if prediction2 > 0.5 else "Fake"
33
-
34
- confidence1 = prediction1 if result1 == "Real" else 1 - prediction1
35
- confidence2 = prediction2 if result2 == "Real" else 1 - prediction2
36
 
37
  return {
38
  "Model 1 Prediction": f"{result1} (Confidence: {confidence1:.2f})",
@@ -41,8 +38,8 @@ def predict(img):
41
 
42
  # Create the Gradio interface
43
  iface = gr.Interface(
44
- fn=predict,
45
- inputs=gr.Image(type="pil"),
46
  outputs={
47
  "Model 1 Prediction": gr.Textbox(),
48
  "Model 2 Prediction": gr.Textbox()
 
1
  import gradio as gr
2
  import tensorflow as tf
 
3
  import numpy as np
4
  from huggingface_hub import hf_hub_download
 
5
 
6
+ # Function to load model from H5 file
7
  def load_model_from_hub(repo_id, filename):
8
  model_path = hf_hub_download(repo_id=repo_id, filename=filename)
9
  return tf.keras.models.load_model(model_path)
10
 
11
+ # Load models from Hugging Face Hub
12
+ model1 = load_model_from_hub("arsath-sm/face_classification_model1", "model.h5")
13
+ model2 = load_model_from_hub("arsath-sm/face_classification_model2", "model.h5")
14
 
15
+ def preprocess_image(image):
16
+ img = tf.convert_to_tensor(image)
17
+ img = tf.image.resize(img, (150, 150))
 
 
18
  img = img / 255.0
19
+ return tf.expand_dims(img, 0)
20
 
21
+ def predict_image(image):
22
+ preprocessed_image = preprocess_image(image)
 
23
 
24
+ # Make predictions using both models
25
+ pred1 = model1.predict(preprocessed_image)[0][0]
26
+ pred2 = model2.predict(preprocessed_image)[0][0]
27
 
28
+ # Prepare results for each model
29
+ result1 = "Real" if pred1 > 0.5 else "Fake"
30
+ confidence1 = pred1 if pred1 > 0.5 else 1 - pred1
31
+ result2 = "Real" if pred2 > 0.5 else "Fake"
32
+ confidence2 = pred2 if pred2 > 0.5 else 1 - pred2
33
 
34
  return {
35
  "Model 1 Prediction": f"{result1} (Confidence: {confidence1:.2f})",
 
38
 
39
  # Create the Gradio interface
40
  iface = gr.Interface(
41
+ fn=predict_image,
42
+ inputs=gr.Image(),
43
  outputs={
44
  "Model 1 Prediction": gr.Textbox(),
45
  "Model 2 Prediction": gr.Textbox()