Spaces:
Sleeping
Sleeping
Commit ·
a25a02d
1
Parent(s): cb5c4fe
prompt model change
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import asyncio
|
| 2 |
-
# Prevent shutdown-time event loop errors
|
| 3 |
try:
|
| 4 |
asyncio.get_event_loop()
|
| 5 |
except RuntimeError:
|
|
@@ -39,37 +38,30 @@ def classify_image(image):
|
|
| 39 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 40 |
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
| 41 |
|
| 42 |
-
|
| 43 |
def explain_recycling(class_label):
|
| 44 |
prompt = f"""
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
6. Advice on reducing, reusing, or sustainable alternatives
|
| 56 |
-
|
| 57 |
-
Write in clear, friendly paragraphs (4–7 paragraphs).
|
| 58 |
"""
|
| 59 |
|
| 60 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 61 |
|
| 62 |
outputs = chat_model.generate(
|
| 63 |
inputs,
|
| 64 |
-
max_length=
|
| 65 |
-
do_sample=
|
| 66 |
-
temperature=0.7,
|
| 67 |
-
top_p=0.9
|
| 68 |
)
|
| 69 |
|
| 70 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 71 |
|
| 72 |
-
|
| 73 |
# ------------------ PIPELINE ------------------
|
| 74 |
def full_pipeline(image):
|
| 75 |
predictions = classify_image(image)
|
|
@@ -101,7 +93,6 @@ with gr.Blocks() as demo:
|
|
| 101 |
outputs=[cls_output, explain_output]
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# Theme and CSS moved to launch()
|
| 105 |
demo.launch(
|
| 106 |
theme=gr.themes.Soft(primary_hue="green"),
|
| 107 |
css="#explainbox {height: 330px; font-size: 15px;}"
|
|
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
try:
|
| 3 |
asyncio.get_event_loop()
|
| 4 |
except RuntimeError:
|
|
|
|
| 38 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 39 |
chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
| 40 |
|
|
|
|
| 41 |
def explain_recycling(class_label):
|
| 42 |
prompt = f"""
|
| 43 |
+
The waste item is: {class_label}.
|
| 44 |
+
|
| 45 |
+
Provide the answer in EXACTLY this format:
|
| 46 |
+
|
| 47 |
+
• Recycling type: <short answer>\n
|
| 48 |
+
• Disposal method: <short answer>\n
|
| 49 |
+
• Processing: <short answer>\n
|
| 50 |
+
• Tip: <short answer>\n
|
| 51 |
+
|
| 52 |
+
Use real line breaks. Keep each answer short.
|
|
|
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
|
| 55 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 56 |
|
| 57 |
outputs = chat_model.generate(
|
| 58 |
inputs,
|
| 59 |
+
max_length=180,
|
| 60 |
+
do_sample=False
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 64 |
|
|
|
|
| 65 |
# ------------------ PIPELINE ------------------
|
| 66 |
def full_pipeline(image):
|
| 67 |
predictions = classify_image(image)
|
|
|
|
| 93 |
outputs=[cls_output, explain_output]
|
| 94 |
)
|
| 95 |
|
|
|
|
| 96 |
demo.launch(
|
| 97 |
theme=gr.themes.Soft(primary_hue="green"),
|
| 98 |
css="#explainbox {height: 330px; font-size: 15px;}"
|