Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load BLIP VQA model
|
| 6 |
+
model_name = "Salesforce/blip-vqa-base"
|
| 7 |
+
processor = BlipProcessor.from_pretrained(model_name)
|
| 8 |
+
model = BlipForQuestionAnswering.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def classify_material(image, question):
|
| 11 |
+
if image is None:
|
| 12 |
+
return "Please upload an image."
|
| 13 |
+
|
| 14 |
+
inputs = processor(image, question, return_tensors="pt")
|
| 15 |
+
out = model.generate(**inputs)
|
| 16 |
+
answer = processor.decode(out[0], skip_special_tokens=True)
|
| 17 |
+
return answer
|
| 18 |
+
|
| 19 |
+
title = "🧠 Smart Waste Material Identifier"
|
| 20 |
+
description = """
|
| 21 |
+
Upload an image of an object or trash item.
|
| 22 |
+
Type your question, e.g.:
|
| 23 |
+
- "Ignore the base, what is this made of?"
|
| 24 |
+
- "What type of waste is this?"
|
| 25 |
+
- "Is this plastic, paper, or metal?"
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=classify_material,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Image(type="pil", label="Upload Trash Image"),
|
| 32 |
+
gr.Textbox(label="Ask a Question", value="Ignore the base, what is this material?")
|
| 33 |
+
],
|
| 34 |
+
outputs=gr.Textbox(label="AI Answer"),
|
| 35 |
+
title=title,
|
| 36 |
+
description=description,
|
| 37 |
+
examples=[
|
| 38 |
+
["example_plastic.jpg", "Ignore background, what material is this?"],
|
| 39 |
+
["example_can.jpg", "Is this metal or plastic?"]
|
| 40 |
+
],
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
demo.launch()
|