tbelvin5969 commited on
Commit
33392fa
·
verified ·
1 Parent(s): 932866d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -7
app.py CHANGED
@@ -1,11 +1,45 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
3
 
4
- model_name = "Genius-Society/insecta"
5
- detection_pipeline = pipeline("object-detection", model=model_name)
 
 
6
 
7
- def detect_objects(image):
8
- return detection_pipeline(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- iface = gr.Interface(fn=detect_objects, inputs="image", outputs="json")
11
- iface.launch()
 
 
 
 
 
 
 
 
 
 
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()