Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,15 @@ import os
|
|
| 4 |
from classifier import GarbageClassifier
|
| 5 |
from config import Config
|
| 6 |
|
| 7 |
-
#
|
| 8 |
try:
|
| 9 |
import spaces
|
|
|
|
| 10 |
HF_SPACES = True
|
|
|
|
| 11 |
except ImportError:
|
| 12 |
HF_SPACES = False
|
|
|
|
| 13 |
|
| 14 |
# Initialize classifier
|
| 15 |
config = Config()
|
|
@@ -21,9 +24,9 @@ classifier.load_model()
|
|
| 21 |
print("Model loaded successfully!")
|
| 22 |
|
| 23 |
|
| 24 |
-
def
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
"""
|
| 28 |
if image is None:
|
| 29 |
return "Please upload an image", "No image provided"
|
|
@@ -35,6 +38,15 @@ def classify_garbage(image):
|
|
| 35 |
return "Error", f"Classification failed: {str(e)}"
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def get_example_images():
|
| 39 |
"""Get example images if they exist"""
|
| 40 |
example_dir = "test_images"
|
|
@@ -73,11 +85,14 @@ with gr.Blocks(title="Garbage Classification System") as demo:
|
|
| 73 |
|
| 74 |
# Category information
|
| 75 |
with gr.Accordion("π Garbage Categories Information", open=False):
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
examples = get_example_images()
|
| 82 |
if examples:
|
| 83 |
gr.Examples(examples=examples, inputs=image_input, label="Example Images")
|
|
@@ -97,4 +112,8 @@ with gr.Blocks(title="Garbage Classification System") as demo:
|
|
| 97 |
)
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from classifier import GarbageClassifier
|
| 5 |
from config import Config
|
| 6 |
|
| 7 |
+
# Check if running in Hugging Face Spaces environment
|
| 8 |
try:
|
| 9 |
import spaces
|
| 10 |
+
|
| 11 |
HF_SPACES = True
|
| 12 |
+
print("Running in Hugging Face Spaces environment")
|
| 13 |
except ImportError:
|
| 14 |
HF_SPACES = False
|
| 15 |
+
print("Running in local environment")
|
| 16 |
|
| 17 |
# Initialize classifier
|
| 18 |
config = Config()
|
|
|
|
| 24 |
print("Model loaded successfully!")
|
| 25 |
|
| 26 |
|
| 27 |
+
def classify_garbage_impl(image):
|
| 28 |
"""
|
| 29 |
+
Actual classification implementation
|
| 30 |
"""
|
| 31 |
if image is None:
|
| 32 |
return "Please upload an image", "No image provided"
|
|
|
|
| 38 |
return "Error", f"Classification failed: {str(e)}"
|
| 39 |
|
| 40 |
|
| 41 |
+
# Apply GPU decorator based on environment
|
| 42 |
+
if HF_SPACES:
|
| 43 |
+
classify_garbage = spaces.GPU(classify_garbage_impl)
|
| 44 |
+
print("GPU decorator applied for Hugging Face Spaces")
|
| 45 |
+
else:
|
| 46 |
+
classify_garbage = classify_garbage_impl
|
| 47 |
+
print("Running without GPU decorator")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
def get_example_images():
|
| 51 |
"""Get example images if they exist"""
|
| 52 |
example_dir = "test_images"
|
|
|
|
| 85 |
|
| 86 |
# Category information
|
| 87 |
with gr.Accordion("π Garbage Categories Information", open=False):
|
| 88 |
+
try:
|
| 89 |
+
category_info = classifier.get_categories_info()
|
| 90 |
+
for category, description in category_info.items():
|
| 91 |
+
gr.Markdown(f"**{category}**: {description}")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
gr.Markdown(f"Categories information not available: {str(e)}")
|
| 94 |
+
|
| 95 |
+
# Examples section
|
| 96 |
examples = get_example_images()
|
| 97 |
if examples:
|
| 98 |
gr.Examples(examples=examples, inputs=image_input, label="Example Images")
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
# Set appropriate launch parameters based on environment
|
| 116 |
+
if HF_SPACES:
|
| 117 |
+
demo.launch()
|
| 118 |
+
else:
|
| 119 |
+
demo.launch(share=True)
|