import gradio as gr from transformers import AutoImageProcessor from transformers import SiglipForImageClassification from PIL import Image import torch # Load model and processor with the new bird classifier name model_name = "prithivMLmods/Bird-Species-Classifier-526" model = SiglipForImageClassification.from_pretrained(model_name) processor = AutoImageProcessor.from_pretrained(model_name) def bird_classification(image): """Predicts bird species classification for an image.""" image = Image.fromarray(image).convert("RGB") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist() labels = { "0": "ABBOTTS BABBLER", "1": "ABBOTTS BOOBY", "2": "ABYSSINIAN GROUND HORNBILL", "3": "AFRICAN CROWNED CRANE", "4": "AFRICAN EMERALD CUCKOO", "5": "AFRICAN FIREFINCH", "6": "AFRICAN OYSTER CATCHER", "7": "AFRICAN PIED HORNBILL", "8": "AFRICAN PYGMY GOOSE", "9": "ALBATROSS", "10": "ALBERTS TOWHEE", "11": "ALEXANDRINE PARAKEET", "12": "ALPINE CHOUGH", "13": "ALTAMIRA YELLOWTHROAT", "14": "AMERICAN AVOCET", "15": "AMERICAN BITTERN", "16": "AMERICAN COOT", "17": "AMERICAN DIPPER", "18": "AMERICAN FLAMINGO", "19": "AMERICAN GOLDFINCH", "20": "AMERICAN KESTREL", "21": "AMERICAN PIPIT", "22": "AMERICAN REDSTART", "23": "AMERICAN ROBIN", "24": "AMERICAN WIGEON", "25": "AMETHYST WOODSTAR", "26": "ANDEAN GOOSE", "27": "ANDEAN LAPWING", "28": "ANDEAN SISKIN", "29": "ANHINGA", "30": "ANIANIAU", "31": "ANNAS HUMMINGBIRD", "32": "ANTBIRD", "33": "ANTILLEAN EUPHONIA", "34": "APAPANE", "35": "APOSTLEBIRD", "36": "ARARIPE MANAKIN", "37": "ASHY STORM PETREL", "38": "ASHY THRUSHBIRD", "39": "ASIAN CRESTED IBIS", "40": "ASIAN DOLLARD BIRD", "41": "ASIAN GREEN BEE EATER", "42": "ASIAN OPENBILL STORK", "43": "AUCKLAND SHAQ", "44": "AUSTRAL CANASTERO", "45": "AUSTRALASIAN FIGBIRD", "46": "AVADAVAT", "47": "AZARAS SPINETAIL", "48": "AZURE BREASTED PITTA", "49": "AZURE JAY", "50": "AZURE TANAGER", "51": "AZURE TIT", "52": "BAIKAL TEAL", "53": "BALD EAGLE", "54": "BALD IBIS", "55": "BALI STARLING", "56": "BALTIMORE ORIOLE", "57": "BANANAQUIT", "58": "BAND TAILED GUAN", "59": "BANDED BROADBILL", "60": "BANDED PITA", "61": "BANDED STILT", "62": "BAR-TAILED GODWIT", "63": "BARN OWL", "64": "BARN SWALLOW", "65": "BARRED PUFFBIRD", "66": "BARROWS GOLDENEYE", "67": "BAY-BREASTED WARBLER", "68": "BEARDED BARBET", "69": "BEARDED BELLBIRD", "70": "BEARDED REEDLING", "71": "BELTED KINGFISHER", "72": "BIRD OF PARADISE", "73": "BLACK AND YELLOW BROADBILL", "74": "BLACK BAZA", "75": "BLACK BREASTED PUFFBIRD", "76": "BLACK COCKATO", "77": "BLACK FACED SPOONBILL", "78": "BLACK FRANCOLIN", "79": "BLACK HEADED CAIQUE", "80": "BLACK NECKED STILT", "81": "BLACK SKIMMER", "82": "BLACK SWAN", "83": "BLACK TAIL CRAKE", "84": "BLACK THROATED BUSHTIT", "85": "BLACK THROATED HUET", "86": "BLACK THROATED WARBLER", "87": "BLACK VENTED SHEARWATER", "88": "BLACK VULTURE", "89": "BLACK-CAPPED CHICKADEE", "90": "BLACK-NECKED GREBE", "91": "BLACK-THROATED SPARROW", "92": "BLACKBURNIAM WARBLER", "93": "BLONDE CRESTED WOODPECKER", "94": "BLOOD PHEASANT", "95": "BLUE COAU", "96": "BLUE DACNIS", "97": "BLUE GRAY GNATCATCHER", "98": "BLUE GROSBEAK", "99": "BLUE GROUSE", "100": "BLUE HERON", "101": "BLUE MALKOHA", "102": "BLUE THROATED PIPING GUAN", "103": "BLUE THROATED TOUCANET", "104": "BOBOLINK", "105": "BORNEAN BRISTLEHEAD", "106": "BORNEAN LEAFBIRD", "107": "BORNEAN PHEASANT", "108": "BRANDT CORMARANT", "109": "BREWERS BLACKBIRD", "110": "BROWN CREPPER", "111": "BROWN HEADED COWBIRD", "112": "BROWN NOODY", "113": "BROWN THRASHER", "114": "BUFFLEHEAD", "115": "BULWERS PHEASANT", "116": "BURCHELLS COURSER", "117": "BUSH TURKEY", "118": "CAATINGA CACHOLOTE", "119": "CABOTS TRAGOPAN", "120": "CACTUS WREN", "121": "CALIFORNIA CONDOR", "122": "CALIFORNIA GULL", "123": "CALIFORNIA QUAIL", "124": "CAMPO FLICKER", "125": "CANARY", "126": "CANVASBACK", "127": "CAPE GLOSSY STARLING", "128": "CAPE LONGCLAW", "129": "CAPE MAY WARBLER", "130": "CAPE ROCK THRUSH", "131": "CAPPED HERON", "132": "CAPUCHINBIRD", "133": "CARMINE BEE-EATER", "134": "CASPIAN TERN", "135": "CASSOWARY", "136": "CEDAR WAXWING", "137": "CERULEAN WARBLER", "138": "CHARA DE COLLAR", "139": "CHATTERING LORY", "140": "CHESTNET BELLIED EUPHONIA", "141": "CHESTNUT WINGED CUCKOO", "142": "CHINESE BAMBOO PARTRIDGE", "143": "CHINESE POND HERON", "144": "CHIPPING SPARROW", "145": "CHUCAO TAPACULO", "146": "CHUKAR PARTRIDGE", "147": "CINNAMON ATTILA", "148": "CINNAMON FLYCATCHER", "149": "CINNAMON TEAL", "150": "CLARKS GREBE", "151": "CLARKS NUTCRACKER", "152": "COCK OF THE ROCK", "153": "COCKATOO", "154": "COLLARED ARACARI", "155": "COLLARED CRESCENTCHEST", "156": "COMMON FIRECREST", "157": "COMMON GRACKLE", "158": "COMMON HOUSE MARTIN", "159": "COMMON IORA", "160": "COMMON LOON", "161": "COMMON POORWILL", "162": "COMMON STARLING", "163": "COPPERSMITH BARBET", "164": "COPPERY TAILED COUCAL", "165": "CRAB PLOVER", "166": "CRANE HAWK", "167": "CREAM COLORED WOODPECKER", "168": "CRESTED AUKLET", "169": "CRESTED CARACARA", "170": "CRESTED COUA", "171": "CRESTED FIREBACK", "172": "CRESTED KINGFISHER", "173": "CRESTED NUTHATCH", "174": "CRESTED OROPENDOLA", "175": "CRESTED SERPENT EAGLE", "176": "CRESTED SHRIKETIT", "177": "CRESTED WOOD PARTRIDGE", "178": "CRIMSON CHAT", "179": "CRIMSON SUNBIRD", "180": "CROW", "181": "CUBAN TODY", "182": "CUBAN TROGON", "183": "CURL CRESTED ARACURI", "184": "D-ARNAUDS BARBET", "185": "DALMATIAN PELICAN", "186": "DARJEELING WOODPECKER", "187": "DARK EYED JUNCO", "188": "DAURIAN REDSTART", "189": "DEMOISELLE CRANE", "190": "DOUBLE BARRED FINCH", "191": "DOUBLE BRESTED CORMARANT", "192": "DOUBLE EYED FIG PARROT", "193": "DOWNY WOODPECKER", "194": "DUNLIN", "195": "DUSKY LORY", "196": "DUSKY ROBIN", "197": "EARED PITA", "198": "EASTERN BLUEBIRD", "199": "EASTERN BLUEBONNET", "200": "EASTERN GOLDEN WEAVER", "201": "EASTERN MEADOWLARK", "202": "EASTERN ROSELLA", "203": "EASTERN TOWEE", "204": "EASTERN WIP POOR WILL", "205": "EASTERN YELLOW ROBIN", "206": "ECUADORIAN HILLSTAR", "207": "EGYPTIAN GOOSE", "208": "ELEGANT TROGON", "209": "ELLIOTS PHEASANT", "210": "EMERALD TANAGER", "211": "EMPEROR PENGUIN", "212": "EMU", "213": "ENGGANO MYNA", "214": "EURASIAN BULLFINCH", "215": "EURASIAN GOLDEN ORIOLE", "216": "EURASIAN MAGPIE", "217": "EUROPEAN GOLDFINCH", "218": "EUROPEAN TURTLE DOVE", "219": "EVENING GROSBEAK", "220": "FAIRY BLUEBIRD", "221": "FAIRY PENGUIN", "222": "FAIRY TERN", "223": "FAN TAILED WIDOW", "224": "FASCIATED WREN", "225": "FIERY MINIVET", "226": "FIORDLAND PENGUIN", "227": "FIRE TAILLED MYZORNIS", "228": "FLAME BOWERBIRD", "229": "FLAME TANAGER", "230": "FOREST WAGTAIL", "231": "FRIGATE", "232": "FRILL BACK PIGEON", "233": "GAMBELS QUAIL", "234": "GANG GANG COCKATOO", "235": "GILA WOODPECKER", "236": "GILDED FLICKER", "237": "GLOSSY IBIS", "238": "GO AWAY BIRD", "239": "GOLD WING WARBLER", "240": "GOLDEN BOWER BIRD", "241": "GOLDEN CHEEKED WARBLER", "242": "GOLDEN CHLOROPHONIA", "243": "GOLDEN EAGLE", "244": "GOLDEN PARAKEET", "245": "GOLDEN PHEASANT", "246": "GOLDEN PIPIT", "247": "GOULDIAN FINCH", "248": "GRANDALA", "249": "GRAY CATBIRD", "250": "GRAY KINGBIRD", "251": "GRAY PARTRIDGE", "252": "GREAT ARGUS", "253": "GREAT GRAY OWL", "254": "GREAT JACAMAR", "255": "GREAT KISKADEE", "256": "GREAT POTOO", "257": "GREAT TINAMOU", "258": "GREAT XENOPS", "259": "GREATER PEWEE", "260": "GREATER PRAIRIE CHICKEN", "261": "GREATOR SAGE GROUSE", "262": "GREEN BROADBILL", "263": "GREEN JAY", "264": "GREEN MAGPIE", "265": "GREEN WINGED DOVE", "266": "GREY CUCKOOSHRIKE", "267": "GREY HEADED CHACHALACA", "268": "GREY HEADED FISH EAGLE", "269": "GREY PLOVER", "270": "GROVED BILLED ANI", "271": "GUINEA TURACO", "272": "GUINEAFOWL", "273": "GURNEYS PITTA", "274": "GYRFALCON", "275": "HAMERKOP", "276": "HARLEQUIN DUCK", "277": "HARLEQUIN QUAIL", "278": "HARPY EAGLE", "279": "HAWAIIAN GOOSE", "280": "HAWFINCH", "281": "HELMET VANGA", "282": "HEPATIC TANAGER", "283": "HIMALAYAN BLUETAIL", "284": "HIMALAYAN MONAL", "285": "HOATZIN", "286": "HOODED MERGANSER", "287": "HOOPOES", "288": "HORNED GUAN", "289": "HORNED LARK", "290": "HORNED SUNGEM", "291": "HOUSE FINCH", "292": "HOUSE SPARROW", "293": "HYACINTH MACAW", "294": "IBERIAN MAGPIE", "295": "IBISBILL", "296": "IMPERIAL SHAQ", "297": "INCA TERN", "298": "INDIAN BUSTARD", "299": "INDIAN PITTA", "300": "INDIAN ROLLER", "301": "INDIAN VULTURE", "302": "INDIGO BUNTING", "303": "INDIGO FLYCATCHER", "304": "INLAND DOTTEREL", "305": "IVORY BILLED ARACARI", "306": "IVORY GULL", "307": "IWI", "308": "JABIRU", "309": "JACK SNIPE", "310": "JACOBIN PIGEON", "311": "JANDAYA PARAKEET", "312": "JAPANESE ROBIN", "313": "JAVA SPARROW", "314": "JOCOTOCO ANTPITTA", "315": "KAGU", "316": "KAKAPO", "317": "KILLDEAR", "318": "KING EIDER", "319": "KING VULTURE", "320": "KIWI", "321": "KNOB BILLED DUCK", "322": "KOOKABURRA", "323": "LARK BUNTING", "324": "LAUGHING GULL", "325": "LAZULI BUNTING", "326": "LESSER ADJUTANT", "327": "LILAC ROLLER", "328": "LIMPKIN", "329": "LITTLE AUK", "330": "LOGGERHEAD SHRIKE", "331": "LONG-EARED OWL", "332": "LOONEY BIRDS", "333": "LUCIFER HUMMINGBIRD", "334": "MAGPIE GOOSE", "335": "MALABAR HORNBILL", "336": "MALACHITE KINGFISHER", "337": "MALAGASY WHITE EYE", "338": "MALEO", "339": "MALLARD DUCK", "340": "MANDRIN DUCK", "341": "MANGROVE CUCKOO", "342": "MARABOU STORK", "343": "MASKED BOBWHITE", "344": "MASKED BOOBY", "345": "MASKED LAPWING", "346": "MCKAYS BUNTING", "347": "MERLIN", "348": "MIKADO PHEASANT", "349": "MILITARY MACAW", "350": "MOURNING DOVE", "351": "MYNA", "352": "NICOBAR PIGEON", "353": "NOISY FRIARBIRD", "354": "NORTHERN BEARDLESS TYRANNULET", "355": "NORTHERN CARDINAL", "356": "NORTHERN FLICKER", "357": "NORTHERN FULMAR", "358": "NORTHERN GANNET", "359": "NORTHERN GOSHAWK", "360": "NORTHERN JACANA", "361": "NORTHERN MOCKINGBIRD", "362": "NORTHERN PARULA", "363": "NORTHERN RED BISHOP", "364": "NORTHERN SHOVELER", "365": "OCELLATED TURKEY", "366": "OILBIRD", "367": "OKINAWA RAIL", "368": "ORANGE BREASTED TROGON", "369": "ORANGE BRESTED BUNTING", "370": "ORIENTAL BAY OWL", "371": "ORNATE HAWK EAGLE", "372": "OSPREY", "373": "OSTRICH", "374": "OVENBIRD", "375": "OYSTER CATCHER", "376": "PAINTED BUNTING", "377": "PALILA", "378": "PALM NUT VULTURE", "379": "PARADISE TANAGER", "380": "PARAKETT AUKLET", "381": "PARAKETT AUKLET", "382": "PARUS MAJOR", "383": "PATAGONIAN SIERRA FINCH", "384": "PEACOCK", "385": "PEREGRINE FALCON", "386": "PHAINOPEPLA", "387": "PHILIPPINE EAGLE", "388": "PINK ROBIN", "389": "PLUSH CRESTED JAY", "390": "POMARINE JAEGER", "391": "PUFFIN", "392": "PUNA TEAL", "393": "PURPLE FINCH", "394": "PURPLE GALLINULE", "395": "PURPLE MARTIN", "396": "PURPLE SWAMPHEN", "397": "PYGMY KINGFISHER", "398": "PYRRHULOXIA", "399": "QUETZAL", "400": "RAINBOW LORIKEET", "401": "RAZORBILL", "402": "RED BEARDED BEE EATER", "403": "RED BELLIED PITTA", "404": "RED BILLED TROPICBIRD", "405": "RED BROWED FINCH", "406": "RED CROSSBILL", "407": "RED FACED CORMORANT", "408": "RED FACED WARBLER", "409": "RED FODY", "410": "RED HEADED DUCK", "411": "RED HEADED WOODPECKER", "412": "RED KNOT", "413": "RED LEGGED HONEYCREEPER", "414": "RED NAPED TROGON", "415": "RED SHOULDERED HAWK", "416": "RED TAILED HAWK", "417": "RED TAILED THRUSH", "418": "RED WINGED BLACKBIRD", "419": "RED WISKERED BULBUL", "420": "REGENT BOWERBIRD", "421": "RING-NECKED PHEASANT", "422": "ROADRUNNER", "423": "ROCK DOVE", "424": "ROSE BREASTED COCKATOO", "425": "ROSE BREASTED GROSBEAK", "426": "ROSEATE SPOONBILL", "427": "ROSY FACED LOVEBIRD", "428": "ROUGH LEG BUZZARD", "429": "ROYAL FLYCATCHER", "430": "RUBY CROWNED KINGLET", "431": "RUBY THROATED HUMMINGBIRD", "432": "RUDDY SHELDUCK", "433": "RUDY KINGFISHER", "434": "RUFOUS KINGFISHER", "435": "RUFOUS TREPE", "436": "RUFUOS MOTMOT", "437": "SAMATRAN THRUSH", "438": "SAND MARTIN", "439": "SANDHILL CRANE", "440": "SATYR TRAGOPAN", "441": "SAYS PHOEBE", "442": "SCARLET CROWNED FRUIT DOVE", "443": "SCARLET FACED LIOCICHLA", "444": "SCARLET IBIS", "445": "SCARLET MACAW", "446": "SCARLET TANAGER", "447": "SHOEBILL", "448": "SHORT BILLED DOWITCHER", "449": "SMITHS LONGSPUR", "450": "SNOW GOOSE", "451": "SNOW PARTRIDGE", "452": "SNOWY EGRET", "453": "SNOWY OWL", "454": "SNOWY PLOVER", "455": "SNOWY SHEATHBILL", "456": "SORA", "457": "SPANGLED COTINGA", "458": "SPLENDID WREN", "459": "SPOON BILED SANDPIPER", "460": "SPOTTED CATBIRD", "461": "SPOTTED WHISTLING DUCK", "462": "SQUACCO HERON", "463": "SRI LANKA BLUE MAGPIE", "464": "STEAMER DUCK", "465": "STORK BILLED KINGFISHER", "466": "STRIATED CARACARA", "467": "STRIPED OWL", "468": "STRIPPED MANAKIN", "469": "STRIPPED SWALLOW", "470": "SUNBITTERN", "471": "SUPERB STARLING", "472": "SURF SCOTER", "473": "SWINHOES PHEASANT", "474": "TAILORBIRD", "475": "TAIWAN MAGPIE", "476": "TAKAHE", "477": "TASMANIAN HEN", "478": "TAWNY FROGMOUTH", "479": "TEAL DUCK", "480": "TIT MOUSE", "481": "TOUCHAN", "482": "TOWNSENDS WARBLER", "483": "TREE SWALLOW", "484": "TRICOLORED BLACKBIRD", "485": "TROPICAL KINGBIRD", "486": "TRUMPTER SWAN", "487": "TURKEY VULTURE", "488": "TURQUOISE MOTMOT", "489": "UMBRELLA BIRD", "490": "VARIED THRUSH", "491": "VEERY", "492": "VENEZUELIAN TROUPIAL", "493": "VERDIN", "494": "VERMILION FLYCATHER", "495": "VICTORIA CROWNED PIGEON", "496": "VIOLET BACKED STARLING", "497": "VIOLET CUCKOO", "498": "VIOLET GREEN SWALLOW", "499": "VIOLET TURACO", "500": "VISAYAN HORNBILL", "501": "VULTURINE GUINEAFOWL", "502": "WALL CREAPER", "503": "WATTLED CURASSOW", "504": "WATTLED LAPWING", "505": "WHIMBREL", "506": "WHITE BREASTED WATERHEN", "507": "WHITE BROWED CRAKE", "508": "WHITE CHEEKED TURACO", "509": "WHITE CRESTED HORNBILL", "510": "WHITE EARED HUMMINGBIRD", "511": "WHITE NECKED RAVEN", "512": "WHITE TAILED TROPIC", "513": "WHITE THROATED BEE EATER", "514": "WILD TURKEY", "515": "WILLOW PTARMIGAN", "516": "WILSONS BIRD OF PARADISE", "517": "WOOD DUCK", "518": "WOOD THRUSH", "519": "WOODLAND KINGFISHER", "520": "WRENTIT", "521": "YELLOW BELLIED FLOWERPECKER", "522": "YELLOW BREASTED CHAT", "523": "YELLOW CACIQUE", "524": "YELLOW HEADED BLACKBIRD", "525": "ZEBRA DOVE" } predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))} return predictions # Create Gradio interface for the bird species classifier iface = gr.Interface( fn=bird_classification, inputs=gr.Image(type="numpy"), outputs=gr.Label(label="Prediction Scores"), title="Bird Species Classifier", description="Upload an image to classify the bird species." ) if __name__ == "__main__": iface.launch()