Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,3 @@
|
|
| 1 |
-
!wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
| 2 |
-
!sudo apt install ./google-chrome-stable_current_amd64.deb
|
| 3 |
-
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
import requests
|
|
@@ -12,6 +8,8 @@ from selenium.webdriver.chrome.options import Options
|
|
| 12 |
from selenium.webdriver.chrome.service import Service
|
| 13 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 14 |
|
|
|
|
|
|
|
| 15 |
import base64
|
| 16 |
from urllib.parse import urljoin
|
| 17 |
import time
|
|
@@ -162,34 +160,7 @@ def generate_html(chapter_num, conceptual_input, problem_input,path):
|
|
| 162 |
|
| 163 |
|
| 164 |
def generate_pdf(input_path, output_path):
|
| 165 |
-
|
| 166 |
-
chrome_options.add_argument('--headless')
|
| 167 |
-
chrome_options.add_argument('--disable-gpu')
|
| 168 |
-
chrome_options.add_argument('--no-sandbox')
|
| 169 |
-
|
| 170 |
-
driver = None
|
| 171 |
-
try:
|
| 172 |
-
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
|
| 173 |
-
html_path = 'file:///' + input_path
|
| 174 |
-
driver.get(html_path)
|
| 175 |
-
|
| 176 |
-
# Add a delay to allow all elements to load (tune this time as needed)
|
| 177 |
-
time.sleep(2)
|
| 178 |
-
|
| 179 |
-
pdf_data = driver.execute_cdp_cmd('Page.printToPDF', {'printBackground': True})
|
| 180 |
-
|
| 181 |
-
if 'data' in pdf_data:
|
| 182 |
-
pdf_content = pdf_data['data']
|
| 183 |
-
pdf_bytes = base64.b64decode(pdf_content)
|
| 184 |
-
with open(output_path, 'wb') as f:
|
| 185 |
-
f.write(pdf_bytes)
|
| 186 |
-
else:
|
| 187 |
-
print("PDF could not be generated.")
|
| 188 |
-
except Exception as e:
|
| 189 |
-
print(f"An error occurred: {e}")
|
| 190 |
-
finally:
|
| 191 |
-
if driver:
|
| 192 |
-
driver.quit()
|
| 193 |
|
| 194 |
def main_function(unit_num, conceptual_input, problem_input):
|
| 195 |
if not conceptual_input and not problem_input:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import requests
|
|
|
|
| 8 |
from selenium.webdriver.chrome.service import Service
|
| 9 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 10 |
|
| 11 |
+
from weasyprint import HTML
|
| 12 |
+
|
| 13 |
import base64
|
| 14 |
from urllib.parse import urljoin
|
| 15 |
import time
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
def generate_pdf(input_path, output_path):
|
| 163 |
+
HTML(input_path).write_pdf(output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
def main_function(unit_num, conceptual_input, problem_input):
|
| 166 |
if not conceptual_input and not problem_input:
|