Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,50 @@
|
|
| 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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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.
|
| 50 |
-
img = tf.
|
| 51 |
-
img
|
| 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 |
-
|
| 64 |
-
|
| 65 |
-
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
else:
|
| 77 |
-
results["Model 2 Prediction"] = "Model failed to load"
|
| 78 |
|
| 79 |
-
return
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Create the Gradio interface
|
| 82 |
iface = gr.Interface(
|
| 83 |
fn=predict_image,
|
| 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."
|
| 92 |
)
|
| 93 |
|
| 94 |
# Launch the app
|
|
|
|
| 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 Hugging Face Hub
|
| 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", "face_classification_model1.h5")
|
| 13 |
+
model2 = load_model_from_hub("arsath-sm/face_classification_model2", "face_classification_model2.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def preprocess_image(image):
|
| 16 |
+
img = tf.image.resize(image, (224, 224)) # Resize to match the input size of your models
|
| 17 |
+
img = tf.cast(img, tf.float32) / 255.0 # Normalize pixel values
|
| 18 |
+
return tf.expand_dims(img, 0) # Add batch dimension
|
|
|
|
| 19 |
|
| 20 |
def predict_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
preprocessed_image = preprocess_image(image)
|
|
|
|
| 22 |
|
| 23 |
+
# Make predictions using both models
|
| 24 |
+
pred1 = model1.predict(preprocessed_image)[0][0]
|
| 25 |
+
pred2 = model2.predict(preprocessed_image)[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Prepare results for each model
|
| 28 |
+
result1 = "Real" if pred1 > 0.5 else "Fake"
|
| 29 |
+
confidence1 = pred1 if pred1 > 0.5 else 1 - pred1
|
| 30 |
+
result2 = "Real" if pred2 > 0.5 else "Fake"
|
| 31 |
+
confidence2 = pred2 if pred2 > 0.5 else 1 - pred2
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
return {
|
| 34 |
+
"Model 1 (ResNet) Prediction": f"{result1} (Confidence: {confidence1:.2f})",
|
| 35 |
+
"Model 2 (Inception) Prediction": f"{result2} (Confidence: {confidence2:.2f})"
|
| 36 |
+
}
|
| 37 |
|
| 38 |
# Create the Gradio interface
|
| 39 |
iface = gr.Interface(
|
| 40 |
fn=predict_image,
|
| 41 |
inputs=gr.Image(),
|
| 42 |
outputs={
|
| 43 |
+
"Model 1 (ResNet) Prediction": gr.Textbox(),
|
| 44 |
+
"Model 2 (Inception) Prediction": gr.Textbox()
|
|
|
|
| 45 |
},
|
| 46 |
+
title="Real vs AI-Generated Face Classification",
|
| 47 |
+
description="Upload an image to classify whether it's a real face or an AI-generated face using two different models: ResNet-style and Inception-style."
|
| 48 |
)
|
| 49 |
|
| 50 |
# Launch the app
|