Spaces:
Sleeping
Sleeping
Commit ·
e689198
1
Parent(s): f402398
UI change
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ except RuntimeError:
|
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import (
|
|
|
|
| 9 |
T5Tokenizer,
|
| 10 |
T5ForConditionalGeneration,
|
| 11 |
pipeline
|
|
@@ -26,26 +27,43 @@ def classify_image(image):
|
|
| 26 |
return results
|
| 27 |
|
| 28 |
|
| 29 |
-
# ------------------ LOAD
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
def explain_recycling(class_label):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
outputs = chat_model.generate(
|
| 42 |
-
|
| 43 |
-
max_length=250,
|
| 44 |
do_sample=True,
|
| 45 |
-
|
|
|
|
| 46 |
)
|
|
|
|
| 47 |
|
| 48 |
-
return
|
| 49 |
|
| 50 |
|
| 51 |
# ------------------ PIPELINE ------------------
|
|
|
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import (
|
| 9 |
+
AutoTokenizer, AutoModelForCausalLM,
|
| 10 |
T5Tokenizer,
|
| 11 |
T5ForConditionalGeneration,
|
| 12 |
pipeline
|
|
|
|
| 27 |
return results
|
| 28 |
|
| 29 |
|
| 30 |
+
# ------------------ LOAD CHAT MODEL ------------------
|
| 31 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 33 |
+
chat_model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 34 |
|
| 35 |
def explain_recycling(class_label):
|
| 36 |
+
system_msg = {
|
| 37 |
+
"role": "system",
|
| 38 |
+
"content": (
|
| 39 |
+
"You are an expert in waste sorting. "
|
| 40 |
+
"You ALWAYS answer using exactly two bullet points:\n"
|
| 41 |
+
"• Recycling type: <one short category>\n"
|
| 42 |
+
"• Disposal: <clear correct sentence>\n"
|
| 43 |
+
"No extra text, no introductions, no explanations."
|
| 44 |
+
)
|
| 45 |
+
}
|
| 46 |
+
user_msg = {
|
| 47 |
+
"role": "user",
|
| 48 |
+
"content": f"Item: {class_label}\nReturn the two bullet points now."
|
| 49 |
+
}
|
| 50 |
+
messages = [system_msg, user_msg]
|
| 51 |
+
|
| 52 |
+
prompt = tokenizer.apply_chat_template(
|
| 53 |
+
messages,
|
| 54 |
+
tokenize=False,
|
| 55 |
+
add_generation_prompt=True
|
| 56 |
+
)
|
| 57 |
|
| 58 |
outputs = chat_model.generate(
|
| 59 |
+
prompt,
|
|
|
|
| 60 |
do_sample=True,
|
| 61 |
+
temperature=0.3,
|
| 62 |
+
top_p=0.9
|
| 63 |
)
|
| 64 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 65 |
|
| 66 |
+
return outputs[0]["generated_text"]
|
| 67 |
|
| 68 |
|
| 69 |
# ------------------ PIPELINE ------------------
|