Spaces:
Build error
Build error
Upload tools/pokemonfinder.py with huggingface_hub
Browse files- tools/pokemonfinder.py +61 -0
tools/pokemonfinder.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, Optional
|
| 9 |
+
|
| 10 |
+
def pokemonfinder(name: str) -> Optional[Dict[str, Any]]:
|
| 11 |
+
"""
|
| 12 |
+
Recherche et retourne les informations détaillées d'un Pokémon depuis l'API Pokémon.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
name: Le nom (ou nom approximatif) du Pokémon à rechercher.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
Un dictionnaire contenant les informations du Pokémon trouvé (nom, identifiant, types, statistiques, sprites, etc.)
|
| 19 |
+
ou None si aucun Pokémon n'est trouvé.
|
| 20 |
+
"""
|
| 21 |
+
name = name.lower().strip()
|
| 22 |
+
|
| 23 |
+
# Recherche exacte d'abord
|
| 24 |
+
r = requests.get(f"https://pokeapi.co/api/v2/pokemon/{name}")
|
| 25 |
+
if r.status_code == 200:
|
| 26 |
+
return r.json()
|
| 27 |
+
|
| 28 |
+
# Si la recherche exacte échoue, on récupère la liste complète des Pokémon
|
| 29 |
+
r = requests.get("https://pokeapi.co/api/v2/pokemon?limit=10000")
|
| 30 |
+
if r.status_code != 200:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
results = r.json().get("results", [])
|
| 34 |
+
|
| 35 |
+
# Recherche floue (fuzzy) sur les noms
|
| 36 |
+
candidates = []
|
| 37 |
+
for p in results:
|
| 38 |
+
p_name = p["name"]
|
| 39 |
+
if name in p_name:
|
| 40 |
+
candidates.append(p)
|
| 41 |
+
|
| 42 |
+
if not candidates:
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
# On prend le premier candidat (le plus proche)
|
| 46 |
+
url = candidates[0]["url"]
|
| 47 |
+
r = requests.get(url)
|
| 48 |
+
if r.status_code == 200:
|
| 49 |
+
return r.json()
|
| 50 |
+
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
# --- Interface Factory ---
|
| 54 |
+
def create_interface():
|
| 55 |
+
return gr.Interface(
|
| 56 |
+
fn=pokemonfinder,
|
| 57 |
+
inputs=[gr.Textbox(label=k) for k in ['name']],
|
| 58 |
+
outputs=gr.JSON(label="Informations détaillées du Pokémon trouvé (nom, ID, types, statistiques, sprites, etc.)"),
|
| 59 |
+
title="pokemonfinder",
|
| 60 |
+
description="Auto-generated tool: pokemonfinder"
|
| 61 |
+
)
|