Upload tools/pokemon_info.py with huggingface_hub
Browse files- tools/pokemon_info.py +73 -0
tools/pokemon_info.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# --- User Defined Logic ---
|
| 7 |
+
import requests
|
| 8 |
+
from typing import Dict, Any
|
| 9 |
+
|
| 10 |
+
def pokemon_info(name: str) -> Dict[str, Any]:
|
| 11 |
+
"""Fetch detailed information about a Pokémon from the PokeAPI.
|
| 12 |
+
|
| 13 |
+
Given a Pokémon name, returns comprehensive data including types, abilities,
|
| 14 |
+
base stats, height, weight, and sprite images.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
name: The name of the Pokémon to look up (case-insensitive).
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
A dictionary containing:
|
| 21 |
+
- name: The Pokémon's name
|
| 22 |
+
- id: National Pokédex number
|
| 23 |
+
- types: List of type names
|
| 24 |
+
- abilities: List of ability names
|
| 25 |
+
- stats: Dictionary mapping stat names to base values
|
| 26 |
+
- height: Height in decimetres
|
| 27 |
+
- weight: Weight in hectograms
|
| 28 |
+
- sprites: Dictionary of sprite image URLs
|
| 29 |
+
- base_experience: Base experience yield
|
| 30 |
+
- order: Order for sorting
|
| 31 |
+
|
| 32 |
+
Raises:
|
| 33 |
+
ValueError: If the Pokémon is not found or the API request fails.
|
| 34 |
+
"""
|
| 35 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name.lower()}"
|
| 36 |
+
try:
|
| 37 |
+
response = requests.get(url, timeout=10)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
data = response.json()
|
| 40 |
+
except requests.exceptions.HTTPError as e:
|
| 41 |
+
if e.response.status_code == 404:
|
| 42 |
+
raise ValueError(f"Pokémon '{name}' not found.")
|
| 43 |
+
raise ValueError(f"API request failed: {e}")
|
| 44 |
+
except requests.exceptions.RequestException as e:
|
| 45 |
+
raise ValueError(f"Network error: {e}")
|
| 46 |
+
|
| 47 |
+
types = [t["type"]["name"] for t in data["types"]]
|
| 48 |
+
abilities = [a["ability"]["name"] for a in data["abilities"]]
|
| 49 |
+
stats = {s["stat"]["name"]: s["base_stat"] for s in data["stats"]}
|
| 50 |
+
sprites = {k: v for k, v in data["sprites"].items() if isinstance(v, str)}
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"name": data["name"],
|
| 54 |
+
"id": data["id"],
|
| 55 |
+
"types": types,
|
| 56 |
+
"abilities": abilities,
|
| 57 |
+
"stats": stats,
|
| 58 |
+
"height": data["height"],
|
| 59 |
+
"weight": data["weight"],
|
| 60 |
+
"sprites": sprites,
|
| 61 |
+
"base_experience": data.get("base_experience"),
|
| 62 |
+
"order": data["order"]
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# --- Interface Factory ---
|
| 66 |
+
def create_interface():
|
| 67 |
+
return gr.Interface(
|
| 68 |
+
fn=pokemon_info,
|
| 69 |
+
inputs=[gr.Textbox(label=k) for k in ['name']],
|
| 70 |
+
outputs=gr.JSON(label="Detailed Pokémon information including types, abilities, stats, height, weight, and sprite URLs"),
|
| 71 |
+
title="pokemon-info",
|
| 72 |
+
description="Auto-generated tool: pokemon-info"
|
| 73 |
+
)
|