Spaces:
Sleeping
Sleeping
Commit ·
f8a801c
1
Parent(s): 8156c42
prompt model change
Browse files
app.py
CHANGED
|
@@ -9,9 +9,8 @@ import torch
|
|
| 9 |
from transformers import (
|
| 10 |
AutoFeatureExtractor,
|
| 11 |
AutoModelForImageClassification,
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
pipeline # <-- added
|
| 15 |
)
|
| 16 |
|
| 17 |
# ------------------ LOAD CLASSIFIER ------------------
|
|
@@ -35,71 +34,24 @@ def classify_image(image):
|
|
| 35 |
return {id2label[i]: float(probs[i]) for i in top3}
|
| 36 |
|
| 37 |
|
| 38 |
-
# ------------------ LOAD
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
pipe = pipeline(
|
| 48 |
-
"text-generation",
|
| 49 |
-
model=chat_model,
|
| 50 |
-
tokenizer=tokenizer,
|
| 51 |
-
device_map="auto",
|
| 52 |
-
max_new_tokens=200
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
def clean_chat_output(full_text):
|
| 56 |
-
# Split at the last assistant tag (TinyLlama uses <|assistant|>)
|
| 57 |
-
if "<|assistant|>" in full_text:
|
| 58 |
-
full_text = full_text.split("<|assistant|>")[-1]
|
| 59 |
-
|
| 60 |
-
# Remove any leftover "Item:" echoes
|
| 61 |
-
lines = full_text.strip().split("\n")
|
| 62 |
-
if lines[0].lower().startswith("item:"):
|
| 63 |
-
lines = lines[1:]
|
| 64 |
-
|
| 65 |
-
return "\n".join(lines).strip()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def explain_recycling(label):
|
| 69 |
-
system_msg = {
|
| 70 |
-
"role": "system",
|
| 71 |
-
"content": (
|
| 72 |
-
"You are an expert in waste sorting. "
|
| 73 |
-
"You ALWAYS answer using exactly two bullet points:\n"
|
| 74 |
-
"• Recycling type: <one short category>\n"
|
| 75 |
-
"• Disposal: <one clear correct sentence>\n"
|
| 76 |
-
"No extra text, no introductions, no explanations."
|
| 77 |
-
)
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
user_msg = {
|
| 81 |
-
"role": "user",
|
| 82 |
-
"content": f"Item: {label}\nReturn the two bullet points now."
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
messages = [system_msg, user_msg]
|
| 86 |
-
|
| 87 |
-
# Chat template
|
| 88 |
-
prompt = tokenizer.apply_chat_template(
|
| 89 |
-
messages,
|
| 90 |
-
tokenize=False,
|
| 91 |
-
add_generation_prompt=True
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
do_sample=False
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
return clean_chat_output(raw)
|
| 102 |
-
|
| 103 |
|
| 104 |
# ------------------ PIPELINE ------------------
|
| 105 |
def full_pipeline(image):
|
|
@@ -121,7 +73,7 @@ with gr.Blocks() as demo:
|
|
| 121 |
explain_output = gr.Textbox(
|
| 122 |
label="Detailed Recycling & Disposal Advice",
|
| 123 |
elem_id="explainbox",
|
| 124 |
-
lines=
|
| 125 |
)
|
| 126 |
|
| 127 |
analyze_btn = gr.Button("Analyze", variant="primary")
|
|
|
|
| 9 |
from transformers import (
|
| 10 |
AutoFeatureExtractor,
|
| 11 |
AutoModelForImageClassification,
|
| 12 |
+
T5Tokenizer,
|
| 13 |
+
T5ForConditionalGeneration
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
# ------------------ LOAD CLASSIFIER ------------------
|
|
|
|
| 34 |
return {id2label[i]: float(probs[i]) for i in top3}
|
| 35 |
|
| 36 |
|
| 37 |
+
# ------------------ LOAD FLAN-T5 ------------------
|
| 38 |
+
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
|
| 39 |
+
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
|
| 40 |
|
| 41 |
+
def explain_recycling(class_label):
|
| 42 |
+
prompt = f"""
|
| 43 |
+
Give recycling instructions for item {class_label}.
|
| 44 |
+
"""
|
| 45 |
|
| 46 |
+
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
outputs = chat_model.generate(
|
| 49 |
+
inputs,
|
| 50 |
+
max_length=180,
|
| 51 |
+
do_sample=False
|
| 52 |
)
|
| 53 |
|
| 54 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# ------------------ PIPELINE ------------------
|
| 57 |
def full_pipeline(image):
|
|
|
|
| 73 |
explain_output = gr.Textbox(
|
| 74 |
label="Detailed Recycling & Disposal Advice",
|
| 75 |
elem_id="explainbox",
|
| 76 |
+
lines=18
|
| 77 |
)
|
| 78 |
|
| 79 |
analyze_btn = gr.Button("Analyze", variant="primary")
|