Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,20 +49,22 @@ def classify_image(image):
|
|
| 49 |
# Run inference with the model
|
| 50 |
print("Running model inference...")
|
| 51 |
outputs = model(**inputs)
|
| 52 |
-
print(f"Model outputs: {outputs}")
|
| 53 |
-
|
| 54 |
-
# Extract logits and probabilities
|
| 55 |
logits_per_image = outputs.logits_per_image # Image-text similarity scores
|
|
|
|
|
|
|
|
|
|
| 56 |
probs = logits_per_image.softmax(dim=1) # Convert logits to probabilities
|
| 57 |
-
print(f"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
safe_prob = probs[0][0].item()
|
| 61 |
unsafe_prob = probs[0][1].item()
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Return results
|
| 68 |
return {
|
|
@@ -74,6 +76,7 @@ def classify_image(image):
|
|
| 74 |
print(f"Error during classification: {e}")
|
| 75 |
return {"Error": str(e)}
|
| 76 |
|
|
|
|
| 77 |
# Step 3: Set Up Gradio Interface
|
| 78 |
iface = gr.Interface(
|
| 79 |
fn=classify_image,
|
|
|
|
| 49 |
# Run inference with the model
|
| 50 |
print("Running model inference...")
|
| 51 |
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
| 52 |
logits_per_image = outputs.logits_per_image # Image-text similarity scores
|
| 53 |
+
print(f"Logits per image: {logits_per_image}")
|
| 54 |
+
|
| 55 |
+
# Apply softmax to convert logits to probabilities
|
| 56 |
probs = logits_per_image.softmax(dim=1) # Convert logits to probabilities
|
| 57 |
+
print(f"Softmax probabilities: {probs}")
|
| 58 |
|
| 59 |
+
# Extract probabilities for each category
|
| 60 |
safe_prob = probs[0][0].item()
|
| 61 |
unsafe_prob = probs[0][1].item()
|
| 62 |
|
| 63 |
+
# Normalize probabilities to ensure they sum to 100%
|
| 64 |
+
total_prob = safe_prob + unsafe_prob
|
| 65 |
+
safe_percentage = (safe_prob / total_prob) * 100
|
| 66 |
+
unsafe_percentage = (unsafe_prob / total_prob) * 100
|
| 67 |
+
print(f"Normalized percentages: safe={safe_percentage}, unsafe={unsafe_percentage}")
|
| 68 |
|
| 69 |
# Return results
|
| 70 |
return {
|
|
|
|
| 76 |
print(f"Error during classification: {e}")
|
| 77 |
return {"Error": str(e)}
|
| 78 |
|
| 79 |
+
|
| 80 |
# Step 3: Set Up Gradio Interface
|
| 81 |
iface = gr.Interface(
|
| 82 |
fn=classify_image,
|