Spaces:
Build error
Build error
Upload tools/infopokemon.py with huggingface_hub
Browse files- tools/infopokemon.py +61 -0
tools/infopokemon.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
|
| 9 |
+
|
| 10 |
+
def infopokemon(nom: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Récupère et renvoie des informations détaillées sur un Pokémon donné via l'API Pokémon.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
nom (str): Le nom du Pokémon (en minuscules, sans accents ni espaces).
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
str: Une chaîne formatée contenant le numéro national, les types, la taille, le poids,
|
| 19 |
+
les statistiques de base et une description courte du Pokémon.
|
| 20 |
+
"""
|
| 21 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{nom.lower()}"
|
| 22 |
+
resp = requests.get(url, timeout=10)
|
| 23 |
+
if resp.status_code != 200:
|
| 24 |
+
return f"Pokémon '{nom}' introuvable."
|
| 25 |
+
data: Dict[str, Any] = resp.json()
|
| 26 |
+
|
| 27 |
+
# Infos générales
|
| 28 |
+
num = data["id"]
|
| 29 |
+
types = ", ".join(t["type"]["name"] for t in data["types"])
|
| 30 |
+
height = data["height"] / 10 # dm -> m
|
| 31 |
+
weight = data["weight"] / 10 # hg -> kg
|
| 32 |
+
|
| 33 |
+
# Statistiques
|
| 34 |
+
stats = {s["stat"]["name"]: s["base_stat"] for s in data["stats"]}
|
| 35 |
+
stats_str = ", ".join(f"{k}: {v}" for k, v in stats.items())
|
| 36 |
+
|
| 37 |
+
# Description (depuis l'espèce)
|
| 38 |
+
species_url = data["species"]["url"]
|
| 39 |
+
species_resp = requests.get(species_url, timeout=10).json()
|
| 40 |
+
flavor = next(
|
| 41 |
+
(f["flavor_text"] for f in species_resp["flavor_text_entries"] if f["language"]["name"] == "fr"),
|
| 42 |
+
"Aucune description disponible."
|
| 43 |
+
).replace("\n", " ")
|
| 44 |
+
|
| 45 |
+
return (
|
| 46 |
+
f"#{num} – {nom.capitalize()}\n"
|
| 47 |
+
f"Types : {types}\n"
|
| 48 |
+
f"Taille : {height:.1f} m – Poids : {weight:.1f} kg\n"
|
| 49 |
+
f"Statistiques : {stats_str}\n"
|
| 50 |
+
f"Description : {flavor}"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# --- Interface Factory ---
|
| 54 |
+
def create_interface():
|
| 55 |
+
return gr.Interface(
|
| 56 |
+
fn=infopokemon,
|
| 57 |
+
inputs=[gr.Textbox(label=k) for k in ['nom']],
|
| 58 |
+
outputs=gr.Textbox(label="Informations détaillées sur le Pokémon"),
|
| 59 |
+
title="infopokemon",
|
| 60 |
+
description="Auto-generated tool: infopokemon"
|
| 61 |
+
)
|