Spaces:
Running
on
Zero
Running
on
Zero
Commit
Β·
ab3dbbe
1
Parent(s):
3f388f9
add huggingface space support
Browse files
app.py
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
from classifier import GarbageClassifier
|
| 5 |
from config import Config
|
| 6 |
|
|
|
|
| 7 |
# Initialize classifier
|
| 8 |
config = Config()
|
| 9 |
classifier = GarbageClassifier(config)
|
|
@@ -14,9 +25,9 @@ classifier.load_model()
|
|
| 14 |
print("Model loaded successfully!")
|
| 15 |
|
| 16 |
|
| 17 |
-
def
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
if image is None:
|
| 22 |
return "Please upload an image", "No image provided"
|
|
@@ -28,6 +39,15 @@ def classify_garbage(image):
|
|
| 28 |
return "Error", f"Classification failed: {str(e)}"
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def get_example_images():
|
| 32 |
"""Get example images if they exist"""
|
| 33 |
example_dir = "test_images"
|
|
@@ -66,11 +86,14 @@ with gr.Blocks(title="Garbage Classification System") as demo:
|
|
| 66 |
|
| 67 |
# Category information
|
| 68 |
with gr.Accordion("π Garbage Categories Information", open=False):
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
examples = get_example_images()
|
| 75 |
if examples:
|
| 76 |
gr.Examples(examples=examples, inputs=image_input, label="Example Images")
|
|
|
|
| 1 |
+
# Check if running in Hugging Face Spaces environment
|
| 2 |
+
try:
|
| 3 |
+
import spaces
|
| 4 |
+
|
| 5 |
+
HF_SPACES = True
|
| 6 |
+
print("Running in Hugging Face Spaces environment")
|
| 7 |
+
except ImportError:
|
| 8 |
+
HF_SPACES = False
|
| 9 |
+
print("Running in local environment")
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
from PIL import Image
|
| 13 |
import os
|
| 14 |
from classifier import GarbageClassifier
|
| 15 |
from config import Config
|
| 16 |
|
| 17 |
+
|
| 18 |
# Initialize classifier
|
| 19 |
config = Config()
|
| 20 |
classifier = GarbageClassifier(config)
|
|
|
|
| 25 |
print("Model loaded successfully!")
|
| 26 |
|
| 27 |
|
| 28 |
+
def classify_garbage_impl(image):
|
| 29 |
"""
|
| 30 |
+
Actual classification implementation
|
| 31 |
"""
|
| 32 |
if image is None:
|
| 33 |
return "Please upload an image", "No image provided"
|
|
|
|
| 39 |
return "Error", f"Classification failed: {str(e)}"
|
| 40 |
|
| 41 |
|
| 42 |
+
# Apply GPU decorator based on environment
|
| 43 |
+
if HF_SPACES:
|
| 44 |
+
classify_garbage = spaces.GPU(classify_garbage_impl)
|
| 45 |
+
print("GPU decorator applied for Hugging Face Spaces")
|
| 46 |
+
else:
|
| 47 |
+
classify_garbage = classify_garbage_impl
|
| 48 |
+
print("Running without GPU decorator")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
def get_example_images():
|
| 52 |
"""Get example images if they exist"""
|
| 53 |
example_dir = "test_images"
|
|
|
|
| 86 |
|
| 87 |
# Category information
|
| 88 |
with gr.Accordion("π Garbage Categories Information", open=False):
|
| 89 |
+
try:
|
| 90 |
+
category_info = classifier.get_categories_info()
|
| 91 |
+
for category, description in category_info.items():
|
| 92 |
+
gr.Markdown(f"**{category}**: {description}")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
gr.Markdown(f"Categories information not available: {str(e)}")
|
| 95 |
+
|
| 96 |
+
# Examples section
|
| 97 |
examples = get_example_images()
|
| 98 |
if examples:
|
| 99 |
gr.Examples(examples=examples, inputs=image_input, label="Example Images")
|