Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,30 +34,18 @@ def classify_image(image):
|
|
34 |
# Run inference
|
35 |
outputs = model(**inputs)
|
36 |
|
37 |
-
# Extract logits
|
38 |
logits_per_image = outputs.logits_per_image # Shape: [1, 2]
|
39 |
-
|
40 |
-
|
41 |
-
# Apply softmax to logits to get probabilities
|
42 |
-
probs = logits_per_image.softmax(dim=1) # Shape: [1, 2]
|
43 |
-
print(f"Softmax probabilities: {probs}")
|
44 |
|
45 |
# Extract probabilities for each category
|
46 |
-
safe_prob = probs[0][0]
|
47 |
-
unsafe_prob = probs[0][1]
|
48 |
-
|
49 |
-
|
50 |
-
# Normalize probabilities to ensure they sum to 100%
|
51 |
-
total_prob = safe_prob + unsafe_prob
|
52 |
-
print(f"Total probability before normalization: {total_prob}")
|
53 |
-
safe_percentage = (safe_prob / total_prob) * 100
|
54 |
-
unsafe_percentage = (unsafe_prob / total_prob) * 100
|
55 |
-
|
56 |
-
# Ensure the sum is exactly 100%
|
57 |
-
print(f"Normalized percentages: Safe={safe_percentage}%, Unsafe={unsafe_percentage}%")
|
58 |
return {
|
59 |
-
"safe":
|
60 |
-
"unsafe":
|
61 |
}
|
62 |
|
63 |
except Exception as e:
|
@@ -66,6 +54,7 @@ def classify_image(image):
|
|
66 |
|
67 |
|
68 |
|
|
|
69 |
# Step 3: Set Up Gradio Interface
|
70 |
iface = gr.Interface(
|
71 |
fn=classify_image,
|
|
|
34 |
# Run inference
|
35 |
outputs = model(**inputs)
|
36 |
|
37 |
+
# Extract logits and apply softmax
|
38 |
logits_per_image = outputs.logits_per_image # Shape: [1, 2]
|
39 |
+
probs = logits_per_image.softmax(dim=1).detach().numpy() # Convert logits to probabilities
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# Extract probabilities for each category
|
42 |
+
safe_prob = probs[0][0] # Safe probability
|
43 |
+
unsafe_prob = probs[0][1] # Unsafe probability
|
44 |
+
|
45 |
+
# Return raw probabilities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
return {
|
47 |
+
"safe": safe_prob, # Leave as a fraction (e.g., 0.92)
|
48 |
+
"unsafe": unsafe_prob # Leave as a fraction (e.g., 0.08)
|
49 |
}
|
50 |
|
51 |
except Exception as e:
|
|
|
54 |
|
55 |
|
56 |
|
57 |
+
|
58 |
# Step 3: Set Up Gradio Interface
|
59 |
iface = gr.Interface(
|
60 |
fn=classify_image,
|