Spaces:
Sleeping
Sleeping
Commit ·
8415066
1
Parent(s): 9bd47cb
minor prompt change
Browse files
app.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoFeatureExtractor, AutoModelForImageClassification #for classifer model
|
| 3 |
-
|
| 4 |
import torch
|
| 5 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# ------------------
|
| 8 |
cls_model_name = "Aalaa/Fine_tuned_Vit_trash_classification"
|
| 9 |
feature_extractor = AutoFeatureExtractor.from_pretrained(cls_model_name)
|
| 10 |
cls_model = AutoModelForImageClassification.from_pretrained(cls_model_name)
|
|
@@ -13,7 +16,6 @@ id2label = cls_model.config.id2label
|
|
| 13 |
|
| 14 |
|
| 15 |
def classify_image(image):
|
| 16 |
-
"""Run ViT classifier and return top 3 predictions."""
|
| 17 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 18 |
|
| 19 |
with torch.no_grad():
|
|
@@ -22,28 +24,32 @@ def classify_image(image):
|
|
| 22 |
logits = outputs.logits
|
| 23 |
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
result = {id2label[i]: float(probs[i]) for i in top3_indices}
|
| 28 |
-
return result
|
| 29 |
|
| 30 |
|
| 31 |
-
#------------------
|
| 32 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 33 |
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
| 34 |
|
|
|
|
| 35 |
def explain_recycling(class_label):
|
| 36 |
prompt = f"""
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 49 |
|
|
@@ -57,45 +63,43 @@ def explain_recycling(class_label):
|
|
| 57 |
|
| 58 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 59 |
|
| 60 |
-
# ------------------ GRADIO APP ------------------
|
| 61 |
|
|
|
|
| 62 |
def full_pipeline(image):
|
| 63 |
-
"Classification → get top label → explanation"
|
| 64 |
predictions = classify_image(image)
|
| 65 |
-
|
| 66 |
-
top_label = max(predictions, key=predictions.get) # Get the top class name
|
| 67 |
explanation = explain_recycling(top_label)
|
| 68 |
return predictions, explanation
|
| 69 |
|
|
|
|
|
|
|
| 70 |
custom_theme = gr.themes.Base(
|
| 71 |
primary_hue="green",
|
| 72 |
secondary_hue="blue",
|
| 73 |
-
neutral_hue="slate"
|
| 74 |
).set(
|
| 75 |
body_background_fill="#F8FFF4",
|
| 76 |
block_background_fill="#FFFFFF",
|
| 77 |
-
button_primary_background_fill="#3BAF4A",
|
| 78 |
button_primary_background_fill_hover="#2F8C3A",
|
| 79 |
button_primary_text_color="white",
|
| 80 |
)
|
| 81 |
|
|
|
|
|
|
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
-
gr.Markdown(
|
| 84 |
-
"<h1 style='text-align:center;'>AI Waste Classifier</h1>"
|
| 85 |
-
)
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
img_input = gr.Image(type="pil", label="Upload waste image")
|
| 89 |
|
| 90 |
-
|
| 91 |
-
cls_output = gr.Label(num_top_classes=3, label="Classifier Prediction")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
|
| 100 |
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 101 |
|
|
@@ -105,4 +109,5 @@ with gr.Blocks() as demo:
|
|
| 105 |
outputs=[cls_output, explain_output]
|
| 106 |
)
|
| 107 |
|
|
|
|
| 108 |
demo.launch(theme=custom_theme, css="#explainbox {height: 350px;}")
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoFeatureExtractor,
|
| 5 |
+
AutoModelForImageClassification,
|
| 6 |
+
T5Tokenizer,
|
| 7 |
+
T5ForConditionalGeneration
|
| 8 |
+
)
|
| 9 |
|
| 10 |
+
# ------------------ LOAD CLASSIFIER ------------------
|
| 11 |
cls_model_name = "Aalaa/Fine_tuned_Vit_trash_classification"
|
| 12 |
feature_extractor = AutoFeatureExtractor.from_pretrained(cls_model_name)
|
| 13 |
cls_model = AutoModelForImageClassification.from_pretrained(cls_model_name)
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
def classify_image(image):
|
|
|
|
| 19 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 20 |
|
| 21 |
with torch.no_grad():
|
|
|
|
| 24 |
logits = outputs.logits
|
| 25 |
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 26 |
|
| 27 |
+
top3 = probs.topk(3).indices.tolist()
|
| 28 |
+
return {id2label[i]: float(probs[i]) for i in top3}
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
+
# ------------------ LOAD FLAN-T5 ------------------
|
| 32 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 33 |
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
| 34 |
|
| 35 |
+
|
| 36 |
def explain_recycling(class_label):
|
| 37 |
prompt = f"""
|
| 38 |
+
You are a waste management expert.
|
| 39 |
+
|
| 40 |
+
The waste item is classified as: {class_label}
|
| 41 |
+
|
| 42 |
+
Provide a detailed explanation including:
|
| 43 |
|
| 44 |
+
1. Recycling type
|
| 45 |
+
2. Why it belongs to this category
|
| 46 |
+
3. Correct disposal method
|
| 47 |
+
4. How it's recycled or processed
|
| 48 |
+
5. Environmental impact if handled incorrectly
|
| 49 |
+
6. Tips for reducing or reusing this item
|
| 50 |
|
| 51 |
+
Write 3–6 paragraphs, friendly and clear.
|
| 52 |
+
"""
|
| 53 |
|
| 54 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 55 |
|
|
|
|
| 63 |
|
| 64 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 65 |
|
|
|
|
| 66 |
|
| 67 |
+
# ------------------ PIPELINE ------------------
|
| 68 |
def full_pipeline(image):
|
|
|
|
| 69 |
predictions = classify_image(image)
|
| 70 |
+
top_label = max(predictions, key=predictions.get)
|
|
|
|
| 71 |
explanation = explain_recycling(top_label)
|
| 72 |
return predictions, explanation
|
| 73 |
|
| 74 |
+
|
| 75 |
+
# ------------------ CUSTOM THEME ------------------
|
| 76 |
custom_theme = gr.themes.Base(
|
| 77 |
primary_hue="green",
|
| 78 |
secondary_hue="blue",
|
| 79 |
+
neutral_hue="slate"
|
| 80 |
).set(
|
| 81 |
body_background_fill="#F8FFF4",
|
| 82 |
block_background_fill="#FFFFFF",
|
| 83 |
+
button_primary_background_fill="#3BAF4A",
|
| 84 |
button_primary_background_fill_hover="#2F8C3A",
|
| 85 |
button_primary_text_color="white",
|
| 86 |
)
|
| 87 |
|
| 88 |
+
|
| 89 |
+
# ------------------ GRADIO UI ------------------
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("<h1 style='text-align:center;'>♻️ AI Waste Classifier + Eco Advisor</h1>")
|
|
|
|
|
|
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
img_input = gr.Image(type="pil", label="Upload waste image")
|
| 95 |
|
| 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 |
+
)
|
|
|
|
| 103 |
|
| 104 |
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 105 |
|
|
|
|
| 109 |
outputs=[cls_output, explain_output]
|
| 110 |
)
|
| 111 |
|
| 112 |
+
|
| 113 |
demo.launch(theme=custom_theme, css="#explainbox {height: 350px;}")
|