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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -21
app.py CHANGED
@@ -1,16 +1,49 @@
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)
@@ -19,22 +52,31 @@ def preprocess_image(image):
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})",
36
- "Model 2 Prediction": f"{result2} (Confidence: {confidence2:.2f})"
37
- }
38
 
39
  # Create the Gradio interface
40
  iface = gr.Interface(
@@ -42,7 +84,8 @@ iface = gr.Interface(
42
  inputs=gr.Image(),
43
  outputs={
44
  "Model 1 Prediction": gr.Textbox(),
45
- "Model 2 Prediction": gr.Textbox()
 
46
  },
47
  title="Real vs AI Face Classification",
48
  description="Upload an image to classify whether it's a real face or an AI-generated face using two different models."
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
+ from huggingface_hub import hf_hub_download, list_repo_files
5
 
6
+ def list_files_in_repo(repo_id):
7
+ try:
8
+ files = list_repo_files(repo_id)
9
+ print(f"Files in {repo_id}:")
10
+ for file in files:
11
+ print(file)
12
+ return files
13
+ except Exception as e:
14
+ print(f"Error listing files in {repo_id}: {str(e)}")
15
+ return []
16
 
17
+ def load_model_from_hub(repo_id):
18
+ files = list_files_in_repo(repo_id)
19
+ model_file = next((f for f in files if f.endswith('.h5') or f.endswith('.keras')), None)
20
+
21
+ if model_file is None:
22
+ raise ValueError(f"No .h5 or .keras file found in {repo_id}")
23
+
24
+ try:
25
+ model_path = hf_hub_download(repo_id=repo_id, filename=model_file)
26
+ return tf.keras.models.load_model(model_path)
27
+ except Exception as e:
28
+ print(f"Error loading model from {repo_id}: {str(e)}")
29
+ raise
30
+
31
+ # Try to load models
32
+ try:
33
+ print("Attempting to load Model 1...")
34
+ model1 = load_model_from_hub("arsath-sm/face_classification_model1")
35
+ print("Model 1 loaded successfully.")
36
+ except Exception as e:
37
+ print(f"Failed to load Model 1: {str(e)}")
38
+ model1 = None
39
+
40
+ try:
41
+ print("Attempting to load Model 2...")
42
+ model2 = load_model_from_hub("arsath-sm/face_classification_model2")
43
+ print("Model 2 loaded successfully.")
44
+ except Exception as e:
45
+ print(f"Failed to load Model 2: {str(e)}")
46
+ model2 = None
47
 
48
  def preprocess_image(image):
49
  img = tf.convert_to_tensor(image)
 
52
  return tf.expand_dims(img, 0)
53
 
54
  def predict_image(image):
55
+ if model1 is None and model2 is None:
56
+ return {
57
+ "Error": "Both models failed to load. Please check the model repositories and try again."
58
+ }
59
+
60
  preprocessed_image = preprocess_image(image)
61
+ results = {}
62
 
63
+ if model1 is not None:
64
+ pred1 = model1.predict(preprocessed_image)[0][0]
65
+ result1 = "Real" if pred1 > 0.5 else "Fake"
66
+ confidence1 = pred1 if pred1 > 0.5 else 1 - pred1
67
+ results["Model 1 Prediction"] = f"{result1} (Confidence: {confidence1:.2f})"
68
+ else:
69
+ results["Model 1 Prediction"] = "Model failed to load"
70
 
71
+ if model2 is not None:
72
+ pred2 = model2.predict(preprocessed_image)[0][0]
73
+ result2 = "Real" if pred2 > 0.5 else "Fake"
74
+ confidence2 = pred2 if pred2 > 0.5 else 1 - pred2
75
+ results["Model 2 Prediction"] = f"{result2} (Confidence: {confidence2:.2f})"
76
+ else:
77
+ results["Model 2 Prediction"] = "Model failed to load"
78
 
79
+ return results
 
 
 
80
 
81
  # Create the Gradio interface
82
  iface = gr.Interface(
 
84
  inputs=gr.Image(),
85
  outputs={
86
  "Model 1 Prediction": gr.Textbox(),
87
+ "Model 2 Prediction": gr.Textbox(),
88
+ "Error": gr.Textbox()
89
  },
90
  title="Real vs AI Face Classification",
91
  description="Upload an image to classify whether it's a real face or an AI-generated face using two different models."