Spaces:
Sleeping
Sleeping
Upload get_public_ip_address.py
Browse files
tools/get_public_ip_address.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
import requests
|
4 |
+
|
5 |
+
class GetPublicIPAddressTool(Tool):
|
6 |
+
name = "get_public_ip_address"
|
7 |
+
description = "Fetches the public IP address of the machine."
|
8 |
+
inputs = {}
|
9 |
+
output_type = "string"
|
10 |
+
|
11 |
+
def forward(self) -> str:
|
12 |
+
try:
|
13 |
+
response = requests.get("https://api.ipify.org?format=json", timeout=5)
|
14 |
+
response.raise_for_status()
|
15 |
+
ip = response.json()["ip"]
|
16 |
+
return f"The public IP address is: {ip}"
|
17 |
+
except requests.exceptions.RequestException as e:
|
18 |
+
return f"Network error: {e}"
|
19 |
+
except KeyError:
|
20 |
+
return "Error: Invalid API response"
|
21 |
+
except Exception as e:
|
22 |
+
return f"Unexpected error: {e}"
|
23 |
+
|
24 |
+
def __init__(self, *args, **kwargs):
|
25 |
+
self.is_initialized = False
|