Spaces:
Sleeping
Sleeping
Prathamesh1420
commited on
Commit
•
6bd0074
1
Parent(s):
06e63c5
Upload 2 files
Browse files- app.py +140 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
3 |
+
import warnings
|
4 |
+
import requests
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
warnings.filterwarnings('ignore')
|
8 |
+
|
9 |
+
# Load the pre-trained Vision Transformer model and feature extractor
|
10 |
+
model_name = "google/vit-base-patch16-224"
|
11 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
12 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
13 |
+
|
14 |
+
# API key for the animal information
|
15 |
+
api_key = '+Rf6S+6HhBwWSlq+rC5wzw==uNM884hcXekpp4tk' # Replace with your actual API key
|
16 |
+
|
17 |
+
def identify_image(image_path):
|
18 |
+
"""Identify the animal in the image."""
|
19 |
+
image = Image.open(image_path)
|
20 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
21 |
+
outputs = model(**inputs)
|
22 |
+
logits = outputs.logits
|
23 |
+
predicted_class_idx = logits.argmax(-1).item()
|
24 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
25 |
+
animal_name = predicted_label.split(',')[0]
|
26 |
+
return animal_name
|
27 |
+
|
28 |
+
def get_animal_info(animal_name):
|
29 |
+
"""Get the animal information from the API."""
|
30 |
+
api_url = f'https://api.api-ninjas.com/v1/animals?name={animal_name}'
|
31 |
+
response = requests.get(api_url, headers={'X-Api-Key': api_key})
|
32 |
+
if response.status_code == requests.codes.ok:
|
33 |
+
animal_info = response.json()
|
34 |
+
else:
|
35 |
+
animal_info = {"Error": response.status_code, "Message": response.text}
|
36 |
+
return animal_info
|
37 |
+
|
38 |
+
def format_animal_info(animal_info):
|
39 |
+
"""Format the animal information into an HTML table."""
|
40 |
+
if "Error" in animal_info:
|
41 |
+
return f"Error: {animal_info['Error']} - {animal_info['Message']}"
|
42 |
+
|
43 |
+
if len(animal_info) == 0:
|
44 |
+
return "No animal information found."
|
45 |
+
|
46 |
+
animal_data = animal_info[0]
|
47 |
+
taxonomy = animal_data.get('taxonomy', {})
|
48 |
+
locations = ', '.join(animal_data.get('locations', []))
|
49 |
+
characteristics = animal_data.get('characteristics', {})
|
50 |
+
|
51 |
+
table = f"""
|
52 |
+
<table border="1" style="width: 100%; border-collapse: collapse;">
|
53 |
+
<tr><th colspan="4" style="text-align: center;"><b>Animal Information</b></th></tr>
|
54 |
+
<tr><td colspan="4" style="text-align: center;"><b>Animal Name: {animal_data['name']}</b></td></tr>
|
55 |
+
<tr><th colspan="4" style="text-align: center;"><b>Taxonomy</b></th></tr>
|
56 |
+
<tr>
|
57 |
+
<td style="text-align: left;"><b>Kingdom</b></td><td style="text-align: right;">{taxonomy.get('kingdom', 'N/A')}</td>
|
58 |
+
<td style="text-align: left;"><b>Phylum</b></td><td style="text-align: right;">{taxonomy.get('phylum', 'N/A')}</td>
|
59 |
+
</tr>
|
60 |
+
<tr>
|
61 |
+
<td style="text-align: left;"><b>Class</b></td><td style="text-align: right;">{taxonomy.get('class', 'N/A')}</td>
|
62 |
+
<td style="text-align: left;"><b>Order</b></td><td style="text-align: right;">{taxonomy.get('order', 'N/A')}</td>
|
63 |
+
</tr>
|
64 |
+
<tr>
|
65 |
+
<td style="text-align: left;"><b>Family</b></td><td style="text-align: right;">{taxonomy.get('family', 'N/A')}</td>
|
66 |
+
<td style="text-align: left;"><b>Genus</b></td><td style="text-align: right;">{taxonomy.get('genus', 'N/A')}</td>
|
67 |
+
</tr>
|
68 |
+
<tr><th colspan="4" style="text-align: center;"><b>Locations</b></th></tr>
|
69 |
+
<tr>
|
70 |
+
<td colspan="4" style="text-align: center;">{locations}</td>
|
71 |
+
</tr>
|
72 |
+
<tr><th colspan="4" style="text-align: center;"><b>Characteristics</b></th></tr>
|
73 |
+
<tr>
|
74 |
+
<td style="text-align: left;"><b>Main Prey</b></td><td style="text-align: right;">{characteristics.get('main_prey', 'N/A')}</td>
|
75 |
+
<td style="text-align: left;"><b>Distinctive Feature</b></td><td style="text-align: right;">{characteristics.get('distinctive_feature', 'N/A')}</td>
|
76 |
+
</tr>
|
77 |
+
<tr>
|
78 |
+
<td style="text-align: left;"><b>Wingspan</b></td><td style="text-align: right;">{characteristics.get('wingspan', 'N/A')}</td>
|
79 |
+
<td style="text-align: left;"><b>Incubation Period</b></td><td style="text-align: right;">{characteristics.get('incubation_period', 'N/A')}</td>
|
80 |
+
</tr>
|
81 |
+
<tr>
|
82 |
+
<td style="text-align: left;"><b>Habitat</b></td><td style="text-align: right;">{characteristics.get('habitat', 'N/A')}</td>
|
83 |
+
<td style="text-align: left;"><b>Predators</b></td><td style="text-align: right;">{characteristics.get('predators', 'N/A')}</td>
|
84 |
+
</tr>
|
85 |
+
<tr>
|
86 |
+
<td style="text-align: left;"><b>Diet</b></td><td style="text-align: right;">{characteristics.get('diet', 'N/A')}</td>
|
87 |
+
<td style="text-align: left;"><b>Lifestyle</b></td><td style="text-align: right;">{characteristics.get('lifestyle', 'N/A')}</td>
|
88 |
+
</tr>
|
89 |
+
<tr>
|
90 |
+
<td style="text-align: left;"><b>Favorite Food</b></td><td style="text-align: right;">{characteristics.get('favorite_food', 'N/A')}</td>
|
91 |
+
<td style="text-align: left;"><b>Type</b></td><td style="text-align: right;">{characteristics.get('type', 'N/A')}</td>
|
92 |
+
</tr>
|
93 |
+
<tr>
|
94 |
+
<td style="text-align: left;"><b>Average Clutch Size</b></td><td style="text-align: right;">{characteristics.get('average_clutch_size', 'N/A')}</td>
|
95 |
+
<td style="text-align: left;"><b>Slogan</b></td><td style="text-align: right;">{characteristics.get('slogan', 'N/A')}</td>
|
96 |
+
</tr>
|
97 |
+
<tr>
|
98 |
+
<td style="text-align: left;"><b>Nesting Location</b></td><td style="text-align: right;">{characteristics.get('nesting_location', 'N/A')}</td>
|
99 |
+
<td style="text-align: left;"><b>Age of Molting</b></td><td style="text-align: right;">{characteristics.get('age_of_molting', 'N/A')}</td>
|
100 |
+
</tr>
|
101 |
+
<tr>
|
102 |
+
<td style="text-align: left;"><b>Color</b></td><td style="text-align: right;">{characteristics.get('color', 'N/A')}</td>
|
103 |
+
<td style="text-align: left;"><b>Skin Type</b></td><td style="text-align: right;">{characteristics.get('skin_type', 'N/A')}</td>
|
104 |
+
</tr>
|
105 |
+
<tr>
|
106 |
+
<td style="text-align: left;"><b>Top Speed</b></td><td style="text-align: right;">{characteristics.get('top_speed', 'N/A')}</td>
|
107 |
+
<td style="text-align: left;"><b>Lifespan</b></td><td style="text-align: right;">{characteristics.get('lifespan', 'N/A')}</td>
|
108 |
+
</tr>
|
109 |
+
<tr>
|
110 |
+
<td style="text-align: left;"><b>Weight</b></td><td style="text-align: right;">{characteristics.get('weight', 'N/A')}</td>
|
111 |
+
<td style="text-align: left;"><b>Length</b></td><td style="text-align: right;">{characteristics.get('length', 'N/A')}</td>
|
112 |
+
</tr>
|
113 |
+
</table>
|
114 |
+
"""
|
115 |
+
return table
|
116 |
+
|
117 |
+
def main_process(image_path):
|
118 |
+
"""Identify the animal and fetch its information."""
|
119 |
+
animal_name = identify_image(image_path)
|
120 |
+
animal_info = get_animal_info(animal_name)
|
121 |
+
formatted_animal_info = format_animal_info(animal_info)
|
122 |
+
return formatted_animal_info
|
123 |
+
|
124 |
+
# Define the Gradio interface
|
125 |
+
def gradio_interface(image):
|
126 |
+
formatted_animal_info = main_process(image)
|
127 |
+
return formatted_animal_info
|
128 |
+
|
129 |
+
# Create the Gradio UI
|
130 |
+
iface = gr.Interface(
|
131 |
+
fn=gradio_interface,
|
132 |
+
inputs=gr.Image(type="filepath"),
|
133 |
+
outputs="html",
|
134 |
+
title="Animal Identification and Information",
|
135 |
+
description="Upload an image of an animal to get detailed information.",
|
136 |
+
allow_flagging="never" # Disable flagging
|
137 |
+
)
|
138 |
+
|
139 |
+
# Launch the Gradio app
|
140 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torchvision
|
4 |
+
requests
|