SivaMallikarjun commited on
Commit
7ad7433
·
verified ·
1 Parent(s): f743124

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchvision.transforms as transforms
4
+ from torchvision import models
5
+ from PIL import Image
6
+ import requests
7
+ import json
8
+
9
+ # Load pre-trained model (ResNet50 fine-tuned for birds)
10
+ model = models.resnet50(pretrained=True)
11
+ model.eval()
12
+
13
+ # Define transformations
14
+ transform = transforms.Compose([
15
+ transforms.Resize((224, 224)),
16
+ transforms.ToTensor(),
17
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
18
+ ])
19
+
20
+ # Load bird species labels
21
+ url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
22
+ labels = requests.get(url).json()
23
+
24
+ def predict_bird(image):
25
+ img = Image.open(image).convert("RGB")
26
+ img = transform(img).unsqueeze(0)
27
+ with torch.no_grad():
28
+ outputs = model(img)
29
+ _, predicted = outputs.max(1)
30
+ bird_name = labels[predicted.item()]
31
+
32
+ # Fetch Wikipedia summary (encyclopedic feature)
33
+ wiki_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{bird_name.replace(' ', '_')}"
34
+ response = requests.get(wiki_url)
35
+ if response.status_code == 200:
36
+ summary = response.json().get("extract", "No additional info found.")
37
+ else:
38
+ summary = "No additional info found."
39
+
40
+ return bird_name, summary
41
+
42
+ # Gradio Interface
43
+ iface = gr.Interface(
44
+ fn=predict_bird,
45
+ inputs=gr.Image(type="filepath"),
46
+ outputs=["text", "text"],
47
+ title="Bird Identifier App",
48
+ description="Upload an image of a bird, and this app will identify its species along with some information from Wikipedia."
49
+ )
50
+
51
+ iface.launch()