Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -67,6 +67,7 @@ def classify_image(image):
|
|
67 |
}
|
68 |
|
69 |
except Exception as e:
|
|
|
70 |
print(f"Error during classification: {e}")
|
71 |
return {"Error": str(e)}
|
72 |
|
@@ -74,13 +75,30 @@ def classify_image(image):
|
|
74 |
iface = gr.Interface(
|
75 |
fn=classify_image,
|
76 |
inputs=gr.Image(type="pil"),
|
77 |
-
outputs=gr.Label(label="Output"), # Display probabilities as
|
78 |
title="Content Safety Classification",
|
79 |
description="Upload an image to classify it as 'safe' or 'unsafe' with corresponding probabilities.",
|
80 |
)
|
81 |
|
82 |
-
# Step 4:
|
83 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
print("Launching the Gradio interface...")
|
85 |
iface.launch()
|
86 |
|
@@ -109,5 +127,6 @@ if __name__ == "__main__":
|
|
109 |
|
110 |
|
111 |
|
|
|
112 |
|
113 |
|
|
|
67 |
}
|
68 |
|
69 |
except Exception as e:
|
70 |
+
# Log and return detailed error messages
|
71 |
print(f"Error during classification: {e}")
|
72 |
return {"Error": str(e)}
|
73 |
|
|
|
75 |
iface = gr.Interface(
|
76 |
fn=classify_image,
|
77 |
inputs=gr.Image(type="pil"),
|
78 |
+
outputs=gr.Label(label="Output"), # Display probabilities as progress bars
|
79 |
title="Content Safety Classification",
|
80 |
description="Upload an image to classify it as 'safe' or 'unsafe' with corresponding probabilities.",
|
81 |
)
|
82 |
|
83 |
+
# Step 4: Add Local Test Before Launching
|
84 |
if __name__ == "__main__":
|
85 |
+
print("Testing model locally with a sample image...")
|
86 |
+
from PIL import Image
|
87 |
+
import requests
|
88 |
+
|
89 |
+
# Use a sample image
|
90 |
+
url = "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png"
|
91 |
+
try:
|
92 |
+
test_image = Image.open(requests.get(url, stream=True).raw)
|
93 |
+
|
94 |
+
# Test the classification function
|
95 |
+
print("Running local test...")
|
96 |
+
test_result = classify_image(test_image)
|
97 |
+
print(f"Local Test Result: {test_result}")
|
98 |
+
except Exception as e:
|
99 |
+
print(f"Error during local test: {e}")
|
100 |
+
|
101 |
+
# Launch Gradio Interface
|
102 |
print("Launching the Gradio interface...")
|
103 |
iface.launch()
|
104 |
|
|
|
127 |
|
128 |
|
129 |
|
130 |
+
|
131 |
|
132 |
|