Spaces:
Sleeping
Sleeping
Commit ·
cb5c4fe
1
Parent(s): 7b6423d
sync change
Browse files
app.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import (
|
|
@@ -35,25 +42,28 @@ chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
|
| 35 |
|
| 36 |
def explain_recycling(class_label):
|
| 37 |
prompt = f"""
|
| 38 |
-
You are a waste
|
| 39 |
-
The
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
| 51 |
|
| 52 |
outputs = chat_model.generate(
|
| 53 |
inputs,
|
| 54 |
-
max_length=
|
| 55 |
do_sample=True,
|
| 56 |
-
temperature=0.
|
| 57 |
top_p=0.9
|
| 58 |
)
|
| 59 |
|
|
@@ -68,20 +78,6 @@ def full_pipeline(image):
|
|
| 68 |
return predictions, explanation
|
| 69 |
|
| 70 |
|
| 71 |
-
# ------------------ CUSTOM THEME ------------------
|
| 72 |
-
custom_theme = gr.themes.Base(
|
| 73 |
-
primary_hue="green",
|
| 74 |
-
secondary_hue="blue",
|
| 75 |
-
neutral_hue="slate"
|
| 76 |
-
).set(
|
| 77 |
-
body_background_fill="#F8FFF4",
|
| 78 |
-
block_background_fill="#FFFFFF",
|
| 79 |
-
button_primary_background_fill="#3BAF4A",
|
| 80 |
-
button_primary_background_fill_hover="#2F8C3A",
|
| 81 |
-
button_primary_text_color="white",
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
# ------------------ GRADIO UI ------------------
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("<h1 style='text-align:center;'>♻️ AI Waste Classifier + Eco Advisor</h1>")
|
|
@@ -94,7 +90,7 @@ with gr.Blocks() as demo:
|
|
| 94 |
explain_output = gr.Textbox(
|
| 95 |
label="Detailed Recycling & Disposal Advice",
|
| 96 |
elem_id="explainbox",
|
| 97 |
-
lines=
|
| 98 |
)
|
| 99 |
|
| 100 |
analyze_btn = gr.Button("Analyze", variant="primary")
|
|
@@ -105,5 +101,8 @@ with gr.Blocks() as demo:
|
|
| 105 |
outputs=[cls_output, explain_output]
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
# Prevent shutdown-time event loop errors
|
| 3 |
+
try:
|
| 4 |
+
asyncio.get_event_loop()
|
| 5 |
+
except RuntimeError:
|
| 6 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 7 |
+
|
| 8 |
import gradio as gr
|
| 9 |
import torch
|
| 10 |
from transformers import (
|
|
|
|
| 42 |
|
| 43 |
def explain_recycling(class_label):
|
| 44 |
prompt = f"""
|
| 45 |
+
You are a waste-management specialist.
|
| 46 |
+
The item was classified as: **{class_label}**.
|
| 47 |
+
|
| 48 |
+
Write a detailed explanation with the following sections:
|
| 49 |
+
|
| 50 |
+
1. Recycling type (organic, inorganic, recyclable, hazardous, compostable, etc.)
|
| 51 |
+
2. Why this item belongs to this category
|
| 52 |
+
3. Correct and safe disposal method
|
| 53 |
+
4. How it should be processed or recycled at facilities
|
| 54 |
+
5. Environmental impact if handled incorrectly
|
| 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=420,
|
| 65 |
do_sample=True,
|
| 66 |
+
temperature=0.7,
|
| 67 |
top_p=0.9
|
| 68 |
)
|
| 69 |
|
|
|
|
| 78 |
return predictions, explanation
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# ------------------ GRADIO UI ------------------
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
gr.Markdown("<h1 style='text-align:center;'>♻️ AI Waste Classifier + Eco Advisor</h1>")
|
|
|
|
| 90 |
explain_output = gr.Textbox(
|
| 91 |
label="Detailed Recycling & Disposal Advice",
|
| 92 |
elem_id="explainbox",
|
| 93 |
+
lines=18
|
| 94 |
)
|
| 95 |
|
| 96 |
analyze_btn = gr.Button("Analyze", variant="primary")
|
|
|
|
| 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;}"
|
| 108 |
+
)
|