Spaces:
Sleeping
Sleeping
Commit ·
73a4802
1
Parent(s): dbad9f3
prompt model change
Browse files
app.py
CHANGED
|
@@ -5,33 +5,25 @@ except RuntimeError:
|
|
| 5 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
-
import torch
|
| 9 |
from transformers import (
|
| 10 |
-
AutoFeatureExtractor,
|
| 11 |
-
AutoModelForImageClassification,
|
| 12 |
T5Tokenizer,
|
| 13 |
-
T5ForConditionalGeneration
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# ------------------ LOAD CLASSIFIER ------------------
|
| 17 |
-
cls_model_name = "
|
| 18 |
-
|
| 19 |
-
cls_model = AutoModelForImageClassification.from_pretrained(cls_model_name)
|
| 20 |
-
|
| 21 |
-
id2label = cls_model.config.id2label
|
| 22 |
-
|
| 23 |
|
| 24 |
def classify_image(image):
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# ------------------ LOAD FLAN-T5 ------------------
|
|
@@ -42,12 +34,11 @@ def explain_recycling(class_label):
|
|
| 42 |
prompt = f"""
|
| 43 |
You are a waste management expert.
|
| 44 |
|
| 45 |
-
The waste item is classified as: **{class_label}**
|
| 46 |
Provide a 2-paragraph explanation including:
|
| 47 |
|
| 48 |
-
1.
|
| 49 |
-
2.
|
| 50 |
-
|
| 51 |
"""
|
| 52 |
|
| 53 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
|
@@ -56,14 +47,16 @@ def explain_recycling(class_label):
|
|
| 56 |
inputs,
|
| 57 |
max_length=250,
|
| 58 |
do_sample=True,
|
| 59 |
-
top_p
|
|
|
|
| 60 |
|
| 61 |
return tokenizer.decode(outputs[0])
|
| 62 |
|
|
|
|
| 63 |
# ------------------ PIPELINE ------------------
|
| 64 |
def full_pipeline(image):
|
| 65 |
predictions = classify_image(image)
|
| 66 |
-
top_label = max(predictions, key=predictions.get)
|
| 67 |
explanation = explain_recycling(top_label)
|
| 68 |
return predictions, explanation
|
| 69 |
|
|
|
|
| 5 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
from transformers import (
|
|
|
|
|
|
|
| 9 |
T5Tokenizer,
|
| 10 |
+
T5ForConditionalGeneration,
|
| 11 |
+
pipeline
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# ------------------ LOAD NEW CLASSIFIER ------------------
|
| 15 |
+
cls_model_name = "yangy50/garbage-classification"
|
| 16 |
+
classifier = pipeline("image-classification", model=cls_model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def classify_image(image):
|
| 19 |
+
preds = classifier(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
results = {}
|
| 22 |
+
for item in preds[:3]:
|
| 23 |
+
label = item["label"]
|
| 24 |
+
score = float(item["score"])
|
| 25 |
+
results[label] = score
|
| 26 |
+
return results
|
| 27 |
|
| 28 |
|
| 29 |
# ------------------ LOAD FLAN-T5 ------------------
|
|
|
|
| 34 |
prompt = f"""
|
| 35 |
You are a waste management expert.
|
| 36 |
|
| 37 |
+
The waste item is classified as: **{class_label}**.
|
| 38 |
Provide a 2-paragraph explanation including:
|
| 39 |
|
| 40 |
+
1. How to dispose of {class_label} correctly.
|
| 41 |
+
2. Optional tips for reducing waste or reusing {class_label}.
|
|
|
|
| 42 |
"""
|
| 43 |
|
| 44 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
|
|
|
| 47 |
inputs,
|
| 48 |
max_length=250,
|
| 49 |
do_sample=True,
|
| 50 |
+
top_p=0.9,
|
| 51 |
+
)
|
| 52 |
|
| 53 |
return tokenizer.decode(outputs[0])
|
| 54 |
|
| 55 |
+
|
| 56 |
# ------------------ PIPELINE ------------------
|
| 57 |
def full_pipeline(image):
|
| 58 |
predictions = classify_image(image)
|
| 59 |
+
top_label = max(predictions, key=predictions.get) # top-1
|
| 60 |
explanation = explain_recycling(top_label)
|
| 61 |
return predictions, explanation
|
| 62 |
|