alihmaou commited on
Commit
3019e55
·
verified ·
1 Parent(s): 340d5bb

Deploy tools/meteo.py via Meta-MCP

Browse files
Files changed (1) hide show
  1. tools/meteo.py +67 -0
tools/meteo.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 meteo(ville: str) -> str:
11
+ """
12
+ Récupère la température actuelle (en °C) pour une ville donnée en utilisant l'API gratuite Open-Meteo.
13
+
14
+ Args:
15
+ ville (str): Nom de la ville (ex: "Paris", "Lyon", "Marseille").
16
+
17
+ Returns:
18
+ str: Chaîne décrivant la température actuelle dans la ville spécifiée.
19
+ En cas d'erreur, retourne un message explicite.
20
+ """
21
+ # Géocodage simple : on utilise une API Nominatim (OpenStreetMap) sans clé
22
+ geo_url = "https://nominatim.openstreetmap.org/search"
23
+ geo_params = {
24
+ "q": ville,
25
+ "format": "json",
26
+ "limit": 1
27
+ }
28
+ headers = {"User-Agent": "gradio-mcp-demo/1.0"}
29
+
30
+ try:
31
+ geo_resp = requests.get(geo_url, params=geo_params, headers=headers, timeout=10)
32
+ geo_resp.raise_for_status()
33
+ geo_data = geo_resp.json()
34
+ if not geo_data:
35
+ return f"Ville '{ville}' introuvable."
36
+ lat = float(geo_data[0]["lat"])
37
+ lon = float(geo_data[0]["lon"])
38
+ except Exception as e:
39
+ return f"Erreur géocodage : {e}"
40
+
41
+ # Appel météo via Open-Meteo (pas de clé requise)
42
+ meteo_url = "https://api.open-meteo.com/v1/forecast"
43
+ meteo_params = {
44
+ "latitude": lat,
45
+ "longitude": lon,
46
+ "current_weather": True,
47
+ "temperature_unit": "celsius"
48
+ }
49
+
50
+ try:
51
+ meteo_resp = requests.get(meteo_url, params=meteo_params, timeout=10)
52
+ meteo_resp.raise_for_status()
53
+ data = meteo_resp.json()
54
+ temp = data["current_weather"]["temperature"]
55
+ return f"Température actuelle à {ville} : {temp} °C"
56
+ except Exception as e:
57
+ return f"Erreur météo : {e}"
58
+
59
+ # --- Interface Factory ---
60
+ def create_interface():
61
+ return gr.Interface(
62
+ fn=meteo,
63
+ inputs=[gr.Textbox(label=k) for k in ['ville']],
64
+ outputs=gr.Textbox(label="Température actuelle dans la ville"),
65
+ title="meteo",
66
+ description="Auto-generated tool: meteo"
67
+ )