File size: 1,912 Bytes
e7c6d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00aa2d8
e7c6d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8004d01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)