Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,55 +4,70 @@ from transformers import CLIPModel, CLIPProcessor
|
|
| 4 |
# Step 1: Load Fine-Tuned Model from Hugging Face Model Hub
|
| 5 |
model_name = "quadranttechnologies/retail-content-safety-clip-finetuned"
|
| 6 |
|
| 7 |
-
print("
|
|
|
|
| 8 |
try:
|
|
|
|
| 9 |
model = CLIPModel.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
processor = CLIPProcessor.from_pretrained(model_name)
|
| 11 |
print("Model and processor loaded successfully.")
|
| 12 |
except Exception as e:
|
| 13 |
-
print(f"Error loading model or processor: {e}")
|
| 14 |
raise RuntimeError(f"Failed to load model: {e}")
|
| 15 |
|
| 16 |
# Step 2: Define the Inference Function
|
| 17 |
def classify_image(image):
|
| 18 |
"""
|
| 19 |
-
Classify an image as 'safe' or 'unsafe' and
|
| 20 |
|
| 21 |
Args:
|
| 22 |
-
image (PIL.Image.Image):
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
str: Predicted category ("safe" or "unsafe").
|
| 26 |
dict: Probabilities for "safe" and "unsafe".
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
if image is None:
|
| 31 |
raise ValueError("No image provided. Please upload a valid image.")
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# Define categories
|
| 34 |
categories = ["safe", "unsafe"]
|
|
|
|
| 35 |
|
| 36 |
# Process the image
|
|
|
|
| 37 |
inputs = processor(text=categories, images=image, return_tensors="pt", padding=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
outputs = model(**inputs)
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
logits_per_image = outputs.logits_per_image # Image-text similarity scores
|
| 42 |
probs = logits_per_image.softmax(dim=1) # Convert logits to probabilities
|
|
|
|
| 43 |
|
| 44 |
-
# Extract probabilities
|
| 45 |
safe_prob = probs[0][0].item() * 100 # Safe percentage
|
| 46 |
unsafe_prob = probs[0][1].item() * 100 # Unsafe percentage
|
| 47 |
|
| 48 |
# Determine the predicted category
|
| 49 |
predicted_category = "safe" if safe_prob > unsafe_prob else "unsafe"
|
|
|
|
| 50 |
|
| 51 |
# Return the predicted category and probabilities
|
| 52 |
return predicted_category, {"safe": f"{safe_prob:.2f}%", "unsafe": f"{unsafe_prob:.2f}%"}
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
print(f"Error during
|
| 56 |
return f"Error: {str(e)}", {}
|
| 57 |
|
| 58 |
# Step 3: Set Up Gradio Interface
|
|
|
|
| 4 |
# Step 1: Load Fine-Tuned Model from Hugging Face Model Hub
|
| 5 |
model_name = "quadranttechnologies/retail-content-safety-clip-finetuned"
|
| 6 |
|
| 7 |
+
print("Initializing the application...")
|
| 8 |
+
|
| 9 |
try:
|
| 10 |
+
print("Loading the model from Hugging Face Model Hub...")
|
| 11 |
model = CLIPModel.from_pretrained(model_name, trust_remote_code=True)
|
| 12 |
processor = CLIPProcessor.from_pretrained(model_name)
|
| 13 |
print("Model and processor loaded successfully.")
|
| 14 |
except Exception as e:
|
| 15 |
+
print(f"Error loading the model or processor: {e}")
|
| 16 |
raise RuntimeError(f"Failed to load model: {e}")
|
| 17 |
|
| 18 |
# Step 2: Define the Inference Function
|
| 19 |
def classify_image(image):
|
| 20 |
"""
|
| 21 |
+
Classify an image as 'safe' or 'unsafe' and return probabilities.
|
| 22 |
|
| 23 |
Args:
|
| 24 |
+
image (PIL.Image.Image): The uploaded image.
|
| 25 |
|
| 26 |
Returns:
|
| 27 |
str: Predicted category ("safe" or "unsafe").
|
| 28 |
dict: Probabilities for "safe" and "unsafe".
|
| 29 |
"""
|
| 30 |
try:
|
| 31 |
+
print("Starting image classification...")
|
| 32 |
+
|
| 33 |
+
# Check if the image is valid
|
| 34 |
if image is None:
|
| 35 |
raise ValueError("No image provided. Please upload a valid image.")
|
| 36 |
+
if not hasattr(image, "convert"):
|
| 37 |
+
raise ValueError("Uploaded file is not a valid image format.")
|
| 38 |
|
| 39 |
+
# Define main categories
|
| 40 |
categories = ["safe", "unsafe"]
|
| 41 |
+
print(f"Categories: {categories}")
|
| 42 |
|
| 43 |
# Process the image
|
| 44 |
+
print("Processing the image with the processor...")
|
| 45 |
inputs = processor(text=categories, images=image, return_tensors="pt", padding=True)
|
| 46 |
+
print(f"Processed inputs: {inputs}")
|
| 47 |
+
|
| 48 |
+
# Perform inference
|
| 49 |
+
print("Running model inference...")
|
| 50 |
outputs = model(**inputs)
|
| 51 |
+
print(f"Model outputs: {outputs}")
|
| 52 |
|
| 53 |
+
# Calculate probabilities
|
| 54 |
logits_per_image = outputs.logits_per_image # Image-text similarity scores
|
| 55 |
probs = logits_per_image.softmax(dim=1) # Convert logits to probabilities
|
| 56 |
+
print(f"Probabilities: {probs}")
|
| 57 |
|
| 58 |
+
# Extract probabilities for each category
|
| 59 |
safe_prob = probs[0][0].item() * 100 # Safe percentage
|
| 60 |
unsafe_prob = probs[0][1].item() * 100 # Unsafe percentage
|
| 61 |
|
| 62 |
# Determine the predicted category
|
| 63 |
predicted_category = "safe" if safe_prob > unsafe_prob else "unsafe"
|
| 64 |
+
print(f"Predicted category: {predicted_category}")
|
| 65 |
|
| 66 |
# Return the predicted category and probabilities
|
| 67 |
return predicted_category, {"safe": f"{safe_prob:.2f}%", "unsafe": f"{unsafe_prob:.2f}%"}
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
+
print(f"Error during classification: {e}")
|
| 71 |
return f"Error: {str(e)}", {}
|
| 72 |
|
| 73 |
# Step 3: Set Up Gradio Interface
|