import gradio as gr import requests import pandas as pd import tempfile import time from urllib.parse import urlparse # ---------------------------- # Helper: URL validation # ---------------------------- def is_valid_url(url): try: parsed = urlparse(url) return parsed.scheme in ("http", "https") and parsed.netloc != "" except: return False # ---------------------------- # Bulk Analyzer Function # ---------------------------- def analyze_bulk(urls_text): if not urls_text.strip(): return pd.DataFrame(), None urls = [u.strip() for u in urls_text.split("\n") if u.strip()] results = [] for url in urls: if not is_valid_url(url): results.append({ "URL": url, "Status Code": "Invalid URL", "HTML Size (KB)": "-", "Load Time (s)": "-" }) continue try: start_time = time.time() response = requests.get(url, timeout=15) load_time = round(time.time() - start_time, 2) size_kb = round(len(response.content) / 1024, 2) results.append({ "URL": url, "Status Code": response.status_code, "HTML Size (KB)": size_kb, "Load Time (s)": load_time }) except Exception as e: results.append({ "URL": url, "Status Code": "Error", "HTML Size (KB)": "-", "Load Time (s)": "-" }) df = pd.DataFrame(results) # Create downloadable CSV temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv") df.to_csv(temp_file.name, index=False) return df, temp_file.name # ---------------------------- # Custom UI Styling # ---------------------------- custom_css = """ body { background: linear-gradient(135deg, #0f172a, #1e293b); } .gradio-container { max-width: 1000px !important; margin: auto; } button { background: linear-gradient(90deg, #3b82f6, #6366f1) !important; color: white !important; border-radius: 12px !important; } """ # ---------------------------- # UI Layout # ---------------------------- with gr.Blocks(css=custom_css) as demo: gr.Markdown("""
Bulk analyze website HTML size, status code, and load time