Spaces:
Sleeping
Sleeping
Commit ·
e93c8ed
1
Parent(s): 85ad2da
prompt model change
Browse files
app.py
CHANGED
|
@@ -10,7 +10,8 @@ from transformers import (
|
|
| 10 |
AutoFeatureExtractor,
|
| 11 |
AutoModelForImageClassification,
|
| 12 |
AutoTokenizer,
|
| 13 |
-
AutoModelForCausalLM
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
# ------------------ LOAD CLASSIFIER ------------------
|
|
@@ -42,33 +43,50 @@ chat_model = AutoModelForCausalLM.from_pretrained(
|
|
| 42 |
tiny_model,
|
| 43 |
torch_dtype=torch.float32)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def explain_recycling(label):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
•
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
Item: {label}
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# ------------------ PIPELINE ------------------
|
| 74 |
def full_pipeline(image):
|
|
|
|
| 10 |
AutoFeatureExtractor,
|
| 11 |
AutoModelForImageClassification,
|
| 12 |
AutoTokenizer,
|
| 13 |
+
AutoModelForCausalLM,
|
| 14 |
+
pipeline # <-- added
|
| 15 |
)
|
| 16 |
|
| 17 |
# ------------------ LOAD CLASSIFIER ------------------
|
|
|
|
| 43 |
tiny_model,
|
| 44 |
torch_dtype=torch.float32)
|
| 45 |
|
| 46 |
+
# ✅ Create pipeline (this was missing)
|
| 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 |
+
|
| 56 |
def explain_recycling(label):
|
| 57 |
+
system_msg = {
|
| 58 |
+
"role": "system",
|
| 59 |
+
"content": (
|
| 60 |
+
"You are an expert in waste sorting. "
|
| 61 |
+
"You ALWAYS answer using exactly two bullet points:\n"
|
| 62 |
+
"• Recycling type: <one short category>\n"
|
| 63 |
+
"• Disposal: <one clear correct sentence>\n"
|
| 64 |
+
"No extra text, no introductions, no explanations."
|
| 65 |
+
)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
user_msg = {
|
| 69 |
+
"role": "user",
|
| 70 |
+
"content": f"Item: {label}\nReturn the two bullet points now."
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
messages = [system_msg, user_msg]
|
| 74 |
+
|
| 75 |
+
# Chat template
|
| 76 |
+
prompt = tokenizer.apply_chat_template(
|
| 77 |
+
messages,
|
| 78 |
+
tokenize=False,
|
| 79 |
+
add_generation_prompt=True
|
| 80 |
)
|
| 81 |
|
| 82 |
+
# Use the pipeline (fixed)
|
| 83 |
+
outputs = pipe(
|
| 84 |
+
prompt,
|
| 85 |
+
do_sample=False # deterministic to avoid repeating instructions
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return outputs[0]["generated_text"]
|
| 89 |
+
|
| 90 |
|
| 91 |
# ------------------ PIPELINE ------------------
|
| 92 |
def full_pipeline(image):
|