test_vercel / app.py
John6666's picture
Upload 3 files
2d08364 verified
import gradio as gr
import requests
import socket
from urllib.parse import urlparse
import traceback
DEFAULT_TEST_URL = "https://realfavicongenerator.net/api/v2"
DEFAULT_RFG_ANALYSIS_ENDPOINT = "https://realfavicongenerator.net/api/v2/favicons/analysis"
def normalize_url(url: str) -> str:
url = (url or "").strip()
if not url:
return ""
if not url.startswith(("http://", "https://")):
url = "https://" + url
return url
def ping_url(url: str, method: str = "GET", timeout_sec: float = 10.0) -> str:
url = normalize_url(url)
if not url:
return "ERROR: URL is empty"
try:
resp = requests.request(method=method, url=url, timeout=timeout_sec)
lines = [
f"Request: {method} {url}",
f"Status code: {resp.status_code}",
f"Elapsed: {resp.elapsed.total_seconds():.3f} seconds",
"",
"=== Response headers ===",
]
for k, v in resp.headers.items():
lines.append(f"{k}: {v}")
lines.extend(
[
"",
"=== First 1000 characters of body ===",
resp.text[:1000],
]
)
return "\n".join(lines)
except Exception as e:
return "ERROR while calling URL: " + repr(e) + "\n\n" + traceback.format_exc()
def resolve_domain(target: str) -> str:
target = (target or "").strip()
if not target:
return "ERROR: Empty input"
if "://" in target:
hostname = urlparse(target).hostname or target
else:
hostname = target
try:
infos = socket.getaddrinfo(hostname, None)
ips = sorted({item[4][0] for item in infos})
lines = [
f"Input: {target}",
f"Hostname: {hostname}",
"",
"Resolved IP addresses:",
]
if ips:
lines.extend(ips)
else:
lines.append("(no IPs returned)")
return "\n".join(lines)
except Exception as e:
return "ERROR while resolving domain: " + repr(e) + "\n\n" + traceback.format_exc()
def test_rfg_analysis(site_url: str, timeout_sec: float = 25.0) -> str:
site_url = normalize_url(site_url)
if not site_url:
return "ERROR: Website URL is empty"
api = DEFAULT_RFG_ANALYSIS_ENDPOINT
try:
resp = requests.get(api, params={"url": site_url}, timeout=timeout_sec)
lines = [
f"Request: GET {api}",
f"Query param url={site_url}",
f"Status code: {resp.status_code}",
f"Elapsed: {resp.elapsed.total_seconds():.3f} seconds",
"",
"=== First 2000 characters of JSON/text body ===",
resp.text[:2000],
]
return "\n".join(lines)
except Exception as e:
return "ERROR while calling RealFaviconGenerator analysis API: " + repr(e) + "\n\n" + traceback.format_exc()
with gr.Blocks() as demo:
gr.Markdown(
"""
# Minimal network test Space
This Space is designed to test outbound connectivity from Hugging Face
Spaces to arbitrary HTTPS endpoints, including the RealFaviconGenerator
API hosted on Vercel.
Use the tabs below to:
- Ping any HTTPS URL (GET/HEAD) and inspect status, latency and headers.
- Resolve a domain to its IP addresses.
- Call the RealFaviconGenerator analysis API directly.
"""
)
with gr.Tab("Ping website"):
url_in = gr.Textbox(
label="URL",
value="https://realfavicongenerator.net/api/v2",
placeholder="https://example.com",
)
method = gr.Dropdown(
["GET", "HEAD"],
value="GET",
label="HTTP method",
)
timeout = gr.Slider(
minimum=1,
maximum=60,
value=10,
step=1,
label="Timeout (seconds)",
)
ping_out = gr.Textbox(
label="Result",
lines=22,
show_copy_button=True,
)
ping_btn = gr.Button("Ping URL")
ping_btn.click(ping_url, inputs=[url_in, method, timeout], outputs=ping_out)
with gr.Tab("Resolve domain"):
domain_in = gr.Textbox(
label="Domain or URL",
value="realfavicongenerator.net",
placeholder="example.com or https://example.com",
)
resolve_out = gr.Textbox(
label="Resolution result",
lines=14,
show_copy_button=True,
)
resolve_btn = gr.Button("Resolve")
resolve_btn.click(resolve_domain, inputs=domain_in, outputs=resolve_out)
with gr.Tab("RealFaviconGenerator analysis"):
site_url_in = gr.Textbox(
label="Website URL to analyze",
value="https://example.com",
placeholder="https://example.com",
)
rfg_timeout = gr.Slider(
minimum=5,
maximum=60,
value=25,
step=1,
label="Timeout (seconds)",
)
rfg_out = gr.Textbox(
label="RFG API response / error",
lines=24,
show_copy_button=True,
)
rfg_btn = gr.Button("Call RFG analysis API")
rfg_btn.click(
test_rfg_analysis,
inputs=[site_url_in, rfg_timeout],
outputs=rfg_out,
)
if __name__ == "__main__":
demo.launch()