Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
from requests.exceptions import RequestException
|
4 |
|
5 |
def fetch_content(url):
|
6 |
"""
|
7 |
-
|
8 |
-
|
|
|
9 |
"""
|
10 |
-
if not url.startswith(('http://', 'https://')):
|
11 |
-
url = 'https://' + url
|
12 |
-
|
13 |
try:
|
14 |
-
|
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
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
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 |
-
|
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(mcp_server=True)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
def fetch_content(url):
|
5 |
"""
|
6 |
+
This function takes a URL as input, fetches its HTML content,
|
7 |
+
and returns it as a string. It includes error handling for common
|
8 |
+
request issues.
|
9 |
"""
|
|
|
|
|
|
|
10 |
try:
|
11 |
+
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
|
|
|
|
|
|
|
12 |
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
|
13 |
return response.text
|
14 |
+
except requests.exceptions.RequestException as e:
|
15 |
return f"An error occurred: {e}"
|
|
|
|
|
16 |
|
17 |
+
# Define the Gradio interface
|
18 |
+
demo = gr.Interface(
|
19 |
+
fn=fetch_content,
|
20 |
+
inputs=gr.Textbox(label="URL", placeholder="https://www.google.com"),
|
21 |
+
outputs=gr.Textbox(label="Page Content"),
|
22 |
+
title="Fetch",
|
23 |
+
description="Enter a URL to fetch the full HTML content of the web page.",
|
24 |
+
allow_flagging="never"
|
25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
if __name__ == "__main__":
|
28 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|