Fetch / app.py
Nymbo's picture
Update app.py
00aa2d8 verified
raw
history blame
1.91 kB
import gradio as gr
import requests
from requests.exceptions import RequestException
def fetch_content(url):
"""
Fetches the full HTML content of a given URL.
Includes basic error handling for common request issues.
"""
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers, timeout=10, allow_redirects=True)
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
return response.text
except RequestException as e:
return f"An error occurred: {e}"
except Exception as e:
return f"An unexpected error occurred: {e}"
# Define the Gradio Interface
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
gr.Markdown(
"""
# Fetch: Web Page Content Viewer
Enter a URL into the text box below and click "Fetch Content" to see the full HTML content of the page.
"""
)
with gr.Row():
url_input = gr.Textbox(
label="Enter URL",
placeholder="e.g., https://www.google.com",
scale=4
)
submit_button = gr.Button("Fetch Content", variant="primary", scale=1)
output_content = gr.Code(label="Page Content", language="html", interactive=False)
# Define the button's click behavior
submit_button.click(
fn=fetch_content,
inputs=url_input,
outputs=output_content
)
# Add examples
gr.Examples(
examples=[
"https://gradio.app",
"https://www.huggingface.co",
"https://www.wikipedia.org"
],
inputs=url_input
)
# Launch the app
demo.launch(mcp_server=True)