Spaces:
Sleeping
Sleeping
Commit ·
8f79ebc
1
Parent(s): 860ffa6
change theme
Browse files
app.py
CHANGED
|
@@ -38,21 +38,25 @@ def explain_recycling(class_label):
|
|
| 38 |
|
| 39 |
Explain the followings:
|
| 40 |
1. What recycling type this is (e.g., plastic, organic, glass, paper, metal).
|
| 41 |
-
2. How to properly dispose of or recycle it.
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
| 43 |
Use this format:
|
| 44 |
|
| 45 |
Recycling type: <type>
|
| 46 |
-
Recommendation: <
|
| 47 |
"""
|
| 48 |
|
| 49 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 50 |
|
| 51 |
outputs = chat_model.generate(
|
| 52 |
inputs,
|
| 53 |
-
max_length=
|
| 54 |
do_sample=True,
|
| 55 |
-
temperature=0.
|
| 56 |
top_p=0.9
|
| 57 |
)
|
| 58 |
|
|
@@ -61,30 +65,50 @@ def explain_recycling(class_label):
|
|
| 61 |
# ------------------ GRADIO APP ------------------
|
| 62 |
|
| 63 |
def full_pipeline(image):
|
| 64 |
-
"
|
| 65 |
predictions = classify_image(image)
|
| 66 |
|
| 67 |
-
# Get the top class name
|
| 68 |
-
top_label = max(predictions, key=predictions.get)
|
| 69 |
-
|
| 70 |
explanation = explain_recycling(top_label)
|
| 71 |
-
|
| 72 |
return predictions, explanation
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
img_input = gr.Image(type="pil", label="Upload waste image")
|
| 80 |
|
| 81 |
with gr.Column():
|
| 82 |
cls_output = gr.Label(num_top_classes=3, label="Classifier Prediction")
|
| 83 |
-
explain_output = gr.Textbox(label="Recycling Advice", lines=6)
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
demo.launch()
|
|
|
|
| 38 |
|
| 39 |
Explain the followings:
|
| 40 |
1. What recycling type this is (e.g., plastic, organic, glass, paper, metal).
|
| 41 |
+
2. How to properly dispose of or recycle it correctly.
|
| 42 |
+
3. Environmental impact if disposed improperly
|
| 43 |
+
4. Optional tips for reducing waste or reusing the item
|
| 44 |
|
| 45 |
+
Write the response in friendly, clear, detailed paragraphs.
|
| 46 |
+
|
| 47 |
Use this format:
|
| 48 |
|
| 49 |
Recycling type: <type>
|
| 50 |
+
Recommendation: <Detailed instructions>
|
| 51 |
"""
|
| 52 |
|
| 53 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 54 |
|
| 55 |
outputs = chat_model.generate(
|
| 56 |
inputs,
|
| 57 |
+
max_length=400,
|
| 58 |
do_sample=True,
|
| 59 |
+
temperature=0.75,
|
| 60 |
top_p=0.9
|
| 61 |
)
|
| 62 |
|
|
|
|
| 65 |
# ------------------ GRADIO APP ------------------
|
| 66 |
|
| 67 |
def full_pipeline(image):
|
| 68 |
+
"Classification → get top label → explanation"
|
| 69 |
predictions = classify_image(image)
|
| 70 |
|
| 71 |
+
top_label = max(predictions, key=predictions.get) # Get the top class name
|
|
|
|
|
|
|
| 72 |
explanation = explain_recycling(top_label)
|
|
|
|
| 73 |
return predictions, explanation
|
| 74 |
|
| 75 |
+
custom_theme = gr.themes.Base(
|
| 76 |
+
primary_hue="green",
|
| 77 |
+
secondary_hue="blue",
|
| 78 |
+
neutral_hue="slate",
|
| 79 |
+
).set(
|
| 80 |
+
body_background_fill="#F8FFF4",
|
| 81 |
+
block_background_fill="#FFFFFF",
|
| 82 |
+
button_primary_background_fill="#3BAF4A", # Green Analyze button
|
| 83 |
+
button_primary_background_fill_hover="#2F8C3A",
|
| 84 |
+
button_primary_text_color="white",
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(theme=custom_theme, css="#explainbox {height: 350px;}") as demo:
|
| 88 |
+
gr.Markdown(
|
| 89 |
+
"<h1 style='text-align:center;'>♻️ AI Waste Classifier + Eco Advisor</h1>"
|
| 90 |
+
)
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
img_input = gr.Image(type="pil", label="Upload waste image")
|
| 94 |
|
| 95 |
with gr.Column():
|
| 96 |
cls_output = gr.Label(num_top_classes=3, label="Classifier Prediction")
|
|
|
|
| 97 |
|
| 98 |
+
explain_output = gr.Textbox(
|
| 99 |
+
label="Detailed Recycling & Disposal Advice",
|
| 100 |
+
elem_id="explainbox",
|
| 101 |
+
lines=16,
|
| 102 |
+
show_copy_button=True,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 106 |
|
| 107 |
+
analyze_btn.click(
|
| 108 |
+
full_pipeline,
|
| 109 |
+
inputs=img_input,
|
| 110 |
+
outputs=[cls_output, explain_output]
|
| 111 |
+
)
|
| 112 |
|
| 113 |
|
| 114 |
demo.launch()
|