Update app.py
Browse files
app.py
CHANGED
|
@@ -67,10 +67,24 @@ def extract_website_content(url, max_images=2):
|
|
| 67 |
headers = {
|
| 68 |
'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'
|
| 69 |
}
|
| 70 |
-
response = requests.get(url, headers=headers, timeout=10)
|
| 71 |
-
response.raise_for_status()
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
for script in soup(["script", "style", "nav", "footer", "header", "noscript"]):
|
| 76 |
script.extract()
|
|
@@ -863,7 +877,7 @@ def create_demo():
|
|
| 863 |
|
| 864 |
with gr.Row():
|
| 865 |
with gr.Column(scale=1):
|
| 866 |
-
url_input = gr.Textbox(label="Enter a Website URL π", placeholder="e.g. https://
|
| 867 |
pdf_input = gr.File(label="Or Upload Book PDF π", file_types=[".pdf"], value=example_pdf)
|
| 868 |
|
| 869 |
gr.Markdown("### βοΈ Customization Settings")
|
|
|
|
| 67 |
headers = {
|
| 68 |
'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'
|
| 69 |
}
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
html_content = ""
|
| 72 |
+
try:
|
| 73 |
+
from playwright.sync_api import sync_playwright
|
| 74 |
+
with sync_playwright() as p:
|
| 75 |
+
browser = p.chromium.launch(headless=True)
|
| 76 |
+
page = browser.new_page(user_agent=headers['User-Agent'])
|
| 77 |
+
# Wait until there are no network connections for at least 500 ms (so JS can finish)
|
| 78 |
+
page.goto(url, timeout=30000, wait_until="networkidle")
|
| 79 |
+
html_content = page.content()
|
| 80 |
+
browser.close()
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"Playwright headless fetch failed: {e}. Falling back to requests...")
|
| 83 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 84 |
+
response.raise_for_status()
|
| 85 |
+
html_content = response.content
|
| 86 |
+
|
| 87 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 88 |
|
| 89 |
for script in soup(["script", "style", "nav", "footer", "header", "noscript"]):
|
| 90 |
script.extract()
|
|
|
|
| 877 |
|
| 878 |
with gr.Row():
|
| 879 |
with gr.Column(scale=1):
|
| 880 |
+
url_input = gr.Textbox(label="Enter a Website URL π", placeholder=r"e.g. https://storykorean.com/stories?level=beginner&story=tiger", value=r"https://storykorean.com/stories?level=beginner&story=tiger")
|
| 881 |
pdf_input = gr.File(label="Or Upload Book PDF π", file_types=[".pdf"], value=example_pdf)
|
| 882 |
|
| 883 |
gr.Markdown("### βοΈ Customization Settings")
|