Spaces:
Sleeping
Sleeping
Commit ·
c530017
1
Parent(s): e82dffa
Add chat model
Browse files
app.py
CHANGED
|
@@ -5,31 +5,29 @@ import torch
|
|
| 5 |
from transformers import T5Tokenizer, T5ForConditionalGeneration #for chat model
|
| 6 |
|
| 7 |
# ------------------ Load classifier model + extractor ------------------
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 12 |
-
# Label mapping
|
| 13 |
-
id2label = model.config.id2label
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 18 |
|
| 19 |
with torch.no_grad():
|
| 20 |
-
outputs =
|
| 21 |
|
| 22 |
logits = outputs.logits
|
| 23 |
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
result = {
|
| 27 |
-
id2label[i]: float(probs[i])
|
| 28 |
-
for i in probs.topk(3).indices.tolist()
|
| 29 |
-
}
|
| 30 |
|
|
|
|
| 31 |
return result
|
| 32 |
|
|
|
|
| 33 |
#------------------ Load flan-t5 chat model------------------
|
| 34 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 35 |
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
|
@@ -60,11 +58,33 @@ def explain_recycling(class_label):
|
|
| 60 |
|
| 61 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
demo.launch()
|
|
|
|
| 5 |
from transformers import T5Tokenizer, T5ForConditionalGeneration #for chat model
|
| 6 |
|
| 7 |
# ------------------ Load classifier model + extractor ------------------
|
| 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)
|
| 11 |
|
| 12 |
+
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():
|
| 20 |
+
outputs = cls_model(**inputs)
|
| 21 |
|
| 22 |
logits = outputs.logits
|
| 23 |
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 24 |
|
| 25 |
+
top3_indices = probs.topk(3).indices.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
result = {id2label[i]: float(probs[i]) for i in top3_indices}
|
| 28 |
return result
|
| 29 |
|
| 30 |
+
|
| 31 |
#------------------ Load flan-t5 chat model------------------
|
| 32 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 33 |
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
|
|
|
| 58 |
|
| 59 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
|
| 61 |
+
# ------------------ GRADIO APP ------------------
|
| 62 |
+
|
| 63 |
+
def full_pipeline(image):
|
| 64 |
+
"""Classifier → get top label → Flan-T5 explanation."""
|
| 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 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("# ♻️ AI Waste Classifier + Disposal Advisor")
|
| 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 |
+
run_btn = gr.Button("Analyze")
|
| 86 |
+
|
| 87 |
+
run_btn.click(full_pipeline, inputs=img_input, outputs=[cls_output, explain_output])
|
| 88 |
+
|
| 89 |
|
| 90 |
demo.launch()
|