Spaces:
Sleeping
Sleeping
Commit ·
87f424d
1
Parent(s): 64ce9e1
nothing
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from pydantic import BaseModel
|
|
| 7 |
|
| 8 |
import random
|
| 9 |
import torch
|
| 10 |
-
from transformers import (
|
| 11 |
AutoTokenizer, AutoModelForCausalLM,
|
| 12 |
pipeline
|
| 13 |
)
|
|
@@ -40,72 +40,43 @@ def classify_image(image):
|
|
| 40 |
return results
|
| 41 |
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
# =========================
|
| 46 |
-
tiny_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 47 |
|
| 48 |
-
tokenizer =
|
| 49 |
-
chat_model =
|
| 50 |
tiny_model,
|
| 51 |
-
|
| 52 |
-
device_map="auto",
|
| 53 |
-
low_cpu_mem_usage=True
|
| 54 |
)
|
| 55 |
|
| 56 |
pipe = pipeline(
|
| 57 |
-
"
|
| 58 |
model=chat_model,
|
| 59 |
tokenizer=tokenizer,
|
| 60 |
-
device_map="auto",
|
| 61 |
max_new_tokens=80
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
def clean_chat_output(full_text):
|
| 66 |
-
if "<|assistant|>" in full_text:
|
| 67 |
-
full_text = full_text.split("<|assistant|>")[-1]
|
| 68 |
-
|
| 69 |
-
lines = full_text.strip().split("\n")
|
| 70 |
-
if lines and lines[0].lower().startswith("item:"):
|
| 71 |
-
lines = lines[1:]
|
| 72 |
-
|
| 73 |
-
return "\n".join(lines).strip()
|
| 74 |
-
|
| 75 |
-
|
| 76 |
def explain_recycling(class_label):
|
| 77 |
-
|
| 78 |
-
"
|
| 79 |
-
"
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"No extra text, no introductions, no explanations."
|
| 85 |
-
)
|
| 86 |
-
}
|
| 87 |
-
user_msg = {
|
| 88 |
-
"role": "user",
|
| 89 |
-
"content": f"Item: {class_label}\nReturn the two bullet points now."
|
| 90 |
-
}
|
| 91 |
-
messages = [system_msg, user_msg]
|
| 92 |
-
|
| 93 |
-
prompt = tokenizer.apply_chat_template(
|
| 94 |
-
messages,
|
| 95 |
-
tokenize=False,
|
| 96 |
-
add_generation_prompt=True
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
raw = outputs[0]["generated_text"]
|
| 108 |
-
return clean_chat_output(raw)
|
| 109 |
|
| 110 |
|
| 111 |
# =========================
|
|
|
|
| 7 |
|
| 8 |
import random
|
| 9 |
import torch
|
| 10 |
+
from transformers import ( T5Tokenizer, T5ForConditionalGeneration,
|
| 11 |
AutoTokenizer, AutoModelForCausalLM,
|
| 12 |
pipeline
|
| 13 |
)
|
|
|
|
| 40 |
return results
|
| 41 |
|
| 42 |
|
| 43 |
+
# ------------------ LOAD CHAT MODEL
|
| 44 |
+
tiny_model = "google/flan-t5-small"
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
tokenizer = T5Tokenizer.from_pretrained(tiny_model)
|
| 47 |
+
chat_model = T5ForConditionalGeneration.from_pretrained(
|
| 48 |
tiny_model,
|
| 49 |
+
device_map="cpu"
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
pipe = pipeline(
|
| 53 |
+
"text2text-generation",
|
| 54 |
model=chat_model,
|
| 55 |
tokenizer=tokenizer,
|
|
|
|
| 56 |
max_new_tokens=80
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def explain_recycling(class_label):
|
| 60 |
+
prompt = (
|
| 61 |
+
"You are an expert in waste sorting. "
|
| 62 |
+
"You ALWAYS answer using exactly two bullet points:\n"
|
| 63 |
+
"• Recycling type: <Item category>\n"
|
| 64 |
+
"• Disposal: <clear, detailed correct sentence>\n"
|
| 65 |
+
f"Item: {class_label}\n"
|
| 66 |
+
"Return the two bullet points now."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
+
output = pipe(prompt)[0]["generated_text"]
|
| 70 |
+
return output.strip()
|
| 71 |
+
|
| 72 |
+
pipe = pipeline(
|
| 73 |
+
"text-generation",
|
| 74 |
+
model=chat_model,
|
| 75 |
+
tokenizer=tokenizer,
|
| 76 |
+
device_map="auto",
|
| 77 |
+
max_new_tokens=80
|
| 78 |
+
)
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
# =========================
|