Spaces:
Sleeping
Sleeping
Commit ·
bbd1b5c
1
Parent(s): 3f1a615
prompt model change
Browse files
app.py
CHANGED
|
@@ -1,23 +1,16 @@
|
|
| 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 (
|
| 10 |
AutoFeatureExtractor,
|
| 11 |
AutoModelForImageClassification,
|
| 12 |
-
|
| 13 |
-
|
| 14 |
)
|
| 15 |
|
| 16 |
# ------------------ LOAD CLASSIFIER ------------------
|
| 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,63 +20,69 @@ def classify_image(image):
|
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = cls_model(**inputs)
|
| 29 |
|
| 30 |
-
|
| 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 |
|
| 37 |
-
# ------------------ LOAD
|
| 38 |
-
|
| 39 |
-
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
prompt = f"""
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
outputs = chat_model.generate(
|
| 47 |
-
inputs,
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
|
|
|
|
| 55 |
# ------------------ PIPELINE ------------------
|
| 56 |
def full_pipeline(image):
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
explanation = explain_recycling(
|
| 60 |
-
return
|
| 61 |
|
| 62 |
|
| 63 |
# ------------------ GRADIO UI ------------------
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.Markdown("
|
| 66 |
-
|
| 67 |
-
with gr.Row():
|
| 68 |
-
img_input = gr.Image(type="pil", label="Upload waste image")
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
label="Detailed Recycling & Disposal Advice",
|
| 74 |
-
elem_id="explainbox",
|
| 75 |
-
lines=4
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
full_pipeline,
|
| 82 |
-
inputs=img_input,
|
| 83 |
-
outputs=[cls_output, explain_output]
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
demo.launch(
|
| 87 |
-
theme=gr.themes.Soft(primary_hue="green"),
|
| 88 |
-
css="#explainbox {height: 330px; font-size: 15px;}"
|
| 89 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import (
|
| 4 |
AutoFeatureExtractor,
|
| 5 |
AutoModelForImageClassification,
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
AutoModelForCausalLM
|
| 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)
|
|
|
|
| 14 |
id2label = cls_model.config.id2label
|
| 15 |
|
| 16 |
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = cls_model(**inputs)
|
| 22 |
|
| 23 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
|
|
|
|
|
|
|
| 24 |
top3 = probs.topk(3).indices.tolist()
|
| 25 |
+
|
| 26 |
return {id2label[i]: float(probs[i]) for i in top3}
|
| 27 |
|
| 28 |
|
| 29 |
+
# ------------------ LOAD TinyLlama ------------------
|
| 30 |
+
tiny_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
|
|
| 31 |
|
| 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"""
|
| 41 |
+
You are an expert in waste sorting.
|
| 42 |
+
Return ONLY the following 2 bullet points:
|
| 43 |
+
|
| 44 |
+
• Recycling type: (one short category)
|
| 45 |
+
• Disposal: (1 clear and correct sentence)
|
| 46 |
|
| 47 |
+
Example:
|
| 48 |
+
Item: Glass bottle
|
| 49 |
+
• Recycling type: Glass recycling
|
| 50 |
+
• Disposal: Rinse and put in the glass-recycling bin.
|
| 51 |
+
|
| 52 |
+
Now answer for:
|
| 53 |
+
Item: {label}
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 57 |
outputs = chat_model.generate(
|
| 58 |
+
**inputs,
|
| 59 |
+
max_new_tokens=80,
|
| 60 |
+
temperature=0.3,
|
| 61 |
+
do_sample=True,
|
| 62 |
+
top_p=0.9
|
| 63 |
)
|
| 64 |
|
| 65 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 66 |
|
| 67 |
+
|
| 68 |
# ------------------ PIPELINE ------------------
|
| 69 |
def full_pipeline(image):
|
| 70 |
+
preds = classify_image(image)
|
| 71 |
+
label = max(preds, key=preds.get)
|
| 72 |
+
explanation = explain_recycling(label)
|
| 73 |
+
return preds, explanation
|
| 74 |
|
| 75 |
|
| 76 |
# ------------------ GRADIO UI ------------------
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("## ♻️ Waste Classifier + TinyLlama Advisor")
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
img = gr.Image(type="pil")
|
| 81 |
+
preds = gr.Label(num_top_classes=3, label="Classifier")
|
| 82 |
+
advice = gr.Textbox(lines=6, label="Recycling Advice")
|
| 83 |
|
| 84 |
+
btn = gr.Button("Analyze")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
btn.click(full_pipeline, inputs=img, outputs=[preds, advice])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
demo.launch()
|
|
|
|
|
|
|
|
|