from PIL import Image from transformers import ViTFeatureExtractor, ViTForImageClassification import warnings import requests import gradio as gr warnings.filterwarnings('ignore') # Load the pre-trained Vision Transformer model and feature extractor model_name = "google/vit-base-patch16-224" feature_extractor = ViTFeatureExtractor.from_pretrained(model_name) model = ViTForImageClassification.from_pretrained(model_name) # API key for the animal information api_key = '+Rf6S+6HhBwWSlq+rC5wzw==uNM884hcXekpp4tk' # Replace with your actual API key def identify_image(image_path): """Identify the animal in the image.""" image = Image.open(image_path) inputs = feature_extractor(images=image, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits predicted_class_idx = logits.argmax(-1).item() predicted_label = model.config.id2label[predicted_class_idx] animal_name = predicted_label.split(',')[0] return animal_name def get_animal_info(animal_name): """Get the animal information from the API.""" api_url = f'https://api.api-ninjas.com/v1/animals?name={animal_name}' response = requests.get(api_url, headers={'X-Api-Key': api_key}) if response.status_code == requests.codes.ok: animal_info = response.json() else: animal_info = {"Error": response.status_code, "Message": response.text} return animal_info def format_animal_info(animal_info): """Format the animal information into an HTML table.""" if "Error" in animal_info: return f"Error: {animal_info['Error']} - {animal_info['Message']}" if len(animal_info) == 0: return "No animal information found." animal_data = animal_info[0] taxonomy = animal_data.get('taxonomy', {}) locations = ', '.join(animal_data.get('locations', [])) characteristics = animal_data.get('characteristics', {}) table = f"""
Animal Information
Animal Name: {animal_data['name']}
Taxonomy
Kingdom{taxonomy.get('kingdom', 'N/A')} Phylum{taxonomy.get('phylum', 'N/A')}
Class{taxonomy.get('class', 'N/A')} Order{taxonomy.get('order', 'N/A')}
Family{taxonomy.get('family', 'N/A')} Genus{taxonomy.get('genus', 'N/A')}
Locations
{locations}
Characteristics
Main Prey{characteristics.get('main_prey', 'N/A')} Distinctive Feature{characteristics.get('distinctive_feature', 'N/A')}
Wingspan{characteristics.get('wingspan', 'N/A')} Incubation Period{characteristics.get('incubation_period', 'N/A')}
Habitat{characteristics.get('habitat', 'N/A')} Predators{characteristics.get('predators', 'N/A')}
Diet{characteristics.get('diet', 'N/A')} Lifestyle{characteristics.get('lifestyle', 'N/A')}
Favorite Food{characteristics.get('favorite_food', 'N/A')} Type{characteristics.get('type', 'N/A')}
Average Clutch Size{characteristics.get('average_clutch_size', 'N/A')} Slogan{characteristics.get('slogan', 'N/A')}
Nesting Location{characteristics.get('nesting_location', 'N/A')} Age of Molting{characteristics.get('age_of_molting', 'N/A')}
Color{characteristics.get('color', 'N/A')} Skin Type{characteristics.get('skin_type', 'N/A')}
Top Speed{characteristics.get('top_speed', 'N/A')} Lifespan{characteristics.get('lifespan', 'N/A')}
Weight{characteristics.get('weight', 'N/A')} Length{characteristics.get('length', 'N/A')}
""" return table def main_process(image_path): """Identify the animal and fetch its information.""" animal_name = identify_image(image_path) animal_info = get_animal_info(animal_name) formatted_animal_info = format_animal_info(animal_info) return formatted_animal_info # Define the Gradio interface def gradio_interface(image): formatted_animal_info = main_process(image) return formatted_animal_info # Create the Gradio UI iface = gr.Interface( fn=gradio_interface, inputs=gr.Image(type="filepath"), outputs="html", title="Animal Identification and Information", description="Upload an image of an animal to get detailed information.", allow_flagging="never" # Disable flagging ) # Launch the Gradio app iface.launch()