Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import AutoModelForImageClassification, AutoProcessor
|
| 6 |
|
| 7 |
+
# Load model
|
| 8 |
+
model_name = "ttheland/insect-identification"
|
| 9 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 11 |
|
| 12 |
+
# Define inference function
|
| 13 |
+
def classify_insect(image):
|
| 14 |
+
image = Image.fromarray(image)
|
| 15 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
|
| 20 |
+
predicted_class = outputs.logits.argmax().item()
|
| 21 |
+
|
| 22 |
+
# Suggested commercial products for elimination
|
| 23 |
+
insect_products = {
|
| 24 |
+
"mosquito": "Talstar P Professional Insecticide",
|
| 25 |
+
"ant": "Advion Ant Gel Bait",
|
| 26 |
+
"roach": "Combat Max Roach Killing Gel",
|
| 27 |
+
"termite": "Termidor SC Termiticide",
|
| 28 |
+
"fly": "Noble Chemical Eliminate Flying Insect Killer"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
product = insect_products.get(predicted_class, "No specific product found.")
|
| 32 |
+
|
| 33 |
+
return f"Detected Insect Species: {predicted_class}\nRecommended Product: {product}"
|
| 34 |
|
| 35 |
+
# Gradio interface
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=classify_insect,
|
| 38 |
+
inputs="image",
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="Insect Identification",
|
| 41 |
+
description="Upload an insect image to identify its species and get elimination strategies."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
iface.launch()
|