Devpal18's picture
Update app.py
5b44256 verified
Raw
History Blame Contribute Delete
3.15 kB
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("""
<div style='text-align:center'>
<h1 style='color:white'>๐ŸŒ Website Weight Inspector</h1>
<p style='color:#cbd5e1'>
Bulk analyze website HTML size, status code, and load time
</p>
</div>
""")
urls_input = gr.Textbox(
label="Enter URLs (One per line)",
lines=8,
placeholder="https://example.com\nhttps://google.com"
)
analyze_button = gr.Button("Analyze Bulk")
output_table = gr.Dataframe(
headers=["URL", "Status Code", "HTML Size (KB)", "Load Time (s)"],
datatype=["str", "str", "str", "str"],
interactive=False
)
download_file = gr.File(label="Download CSV Report")
analyze_button.click(
fn=analyze_bulk,
inputs=urls_input,
outputs=[output_table, download_file]
)
demo.launch()