Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from requests.exceptions import RequestException
|
4 |
+
|
5 |
+
def fetch_content(url):
|
6 |
+
"""
|
7 |
+
Fetches the full HTML content of a given URL.
|
8 |
+
Includes basic error handling for common request issues.
|
9 |
+
"""
|
10 |
+
if not url.startswith(('http://', 'https://')):
|
11 |
+
url = 'https://' + url
|
12 |
+
|
13 |
+
try:
|
14 |
+
headers = {
|
15 |
+
'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'
|
16 |
+
}
|
17 |
+
response = requests.get(url, headers=headers, timeout=10, allow_redirects=True)
|
18 |
+
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
|
19 |
+
return response.text
|
20 |
+
except RequestException as e:
|
21 |
+
return f"An error occurred: {e}"
|
22 |
+
except Exception as e:
|
23 |
+
return f"An unexpected error occurred: {e}"
|
24 |
+
|
25 |
+
# Define the Gradio Interface
|
26 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
27 |
+
gr.Markdown(
|
28 |
+
"""
|
29 |
+
# Fetch: Web Page Content Viewer
|
30 |
+
Enter a URL into the text box below and click "Fetch Content" to see the full HTML content of the page.
|
31 |
+
"""
|
32 |
+
)
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
url_input = gr.Textbox(
|
36 |
+
label="Enter URL",
|
37 |
+
placeholder="e.g., https://www.google.com",
|
38 |
+
scale=4
|
39 |
+
)
|
40 |
+
submit_button = gr.Button("Fetch Content", variant="primary", scale=1)
|
41 |
+
|
42 |
+
output_content = gr.Code(label="Page Content", language="html", interactive=False)
|
43 |
+
|
44 |
+
# Define the button's click behavior
|
45 |
+
submit_button.click(
|
46 |
+
fn=fetch_content,
|
47 |
+
inputs=url_input,
|
48 |
+
outputs=output_content
|
49 |
+
)
|
50 |
+
|
51 |
+
# Add examples
|
52 |
+
gr.Examples(
|
53 |
+
examples=[
|
54 |
+
"https://gradio.app",
|
55 |
+
"https://www.huggingface.co",
|
56 |
+
"https://www.wikipedia.org"
|
57 |
+
],
|
58 |
+
inputs=url_input
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
+
demo.launch()
|