Spaces:
Sleeping
Sleeping
Commit ·
85ad2da
1
Parent(s): bbd1b5c
prompt model change
Browse files
app.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import (
|
|
@@ -11,6 +17,7 @@ from transformers import (
|
|
| 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)
|
|
|
|
| 14 |
id2label = cls_model.config.id2label
|
| 15 |
|
| 16 |
|
|
@@ -20,9 +27,10 @@ def classify_image(image):
|
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = cls_model(**inputs)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
|
|
|
| 26 |
return {id2label[i]: float(probs[i]) for i in top3}
|
| 27 |
|
| 28 |
|
|
@@ -32,9 +40,7 @@ tiny_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
| 32 |
tokenizer = AutoTokenizer.from_pretrained(tiny_model)
|
| 33 |
chat_model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
tiny_model,
|
| 35 |
-
torch_dtype=torch.float32
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
|
| 39 |
def explain_recycling(label):
|
| 40 |
prompt = f"""
|
|
@@ -64,25 +70,38 @@ Item: {label}
|
|
| 64 |
|
| 65 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 66 |
|
| 67 |
-
|
| 68 |
# ------------------ PIPELINE ------------------
|
| 69 |
def full_pipeline(image):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
explanation = explain_recycling(
|
| 73 |
-
return
|
| 74 |
|
| 75 |
|
| 76 |
# ------------------ GRADIO UI ------------------
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
try:
|
| 3 |
+
asyncio.get_event_loop()
|
| 4 |
+
except RuntimeError:
|
| 5 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 6 |
+
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
| 9 |
from transformers import (
|
|
|
|
| 17 |
cls_model_name = "Aalaa/Fine_tuned_Vit_trash_classification"
|
| 18 |
feature_extractor = AutoFeatureExtractor.from_pretrained(cls_model_name)
|
| 19 |
cls_model = AutoModelForImageClassification.from_pretrained(cls_model_name)
|
| 20 |
+
|
| 21 |
id2label = cls_model.config.id2label
|
| 22 |
|
| 23 |
|
|
|
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = cls_model(**inputs)
|
| 29 |
|
| 30 |
+
logits = outputs.logits
|
| 31 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 32 |
|
| 33 |
+
top3 = probs.topk(3).indices.tolist()
|
| 34 |
return {id2label[i]: float(probs[i]) for i in top3}
|
| 35 |
|
| 36 |
|
|
|
|
| 40 |
tokenizer = AutoTokenizer.from_pretrained(tiny_model)
|
| 41 |
chat_model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
tiny_model,
|
| 43 |
+
torch_dtype=torch.float32)
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def explain_recycling(label):
|
| 46 |
prompt = f"""
|
|
|
|
| 70 |
|
| 71 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 72 |
|
|
|
|
| 73 |
# ------------------ PIPELINE ------------------
|
| 74 |
def full_pipeline(image):
|
| 75 |
+
predictions = classify_image(image)
|
| 76 |
+
top_label = max(predictions, key=predictions.get)
|
| 77 |
+
explanation = explain_recycling(top_label)
|
| 78 |
+
return predictions, explanation
|
| 79 |
|
| 80 |
|
| 81 |
# ------------------ GRADIO UI ------------------
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("<h1 style='text-align:center;'>♻️ AI Waste Classifier + Eco Advisor</h1>")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
img_input = gr.Image(type="pil", label="Upload waste image")
|
| 87 |
+
|
| 88 |
+
cls_output = gr.Label(num_top_classes=3, label="Classifier Prediction")
|
| 89 |
|
| 90 |
+
explain_output = gr.Textbox(
|
| 91 |
+
label="Detailed Recycling & Disposal Advice",
|
| 92 |
+
elem_id="explainbox",
|
| 93 |
+
lines=4
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 97 |
|
| 98 |
+
analyze_btn.click(
|
| 99 |
+
full_pipeline,
|
| 100 |
+
inputs=img_input,
|
| 101 |
+
outputs=[cls_output, explain_output]
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
demo.launch(
|
| 105 |
+
theme=gr.themes.Soft(primary_hue="green"),
|
| 106 |
+
css="#explainbox {height: 330px; font-size: 15px;}"
|
| 107 |
+
)
|