Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import requests
|
| 4 |
from jinja2 import Template
|
| 5 |
-
from weasyprint import HTML
|
| 6 |
from urllib.parse import urljoin
|
| 7 |
import os
|
| 8 |
import warnings
|
|
@@ -132,12 +131,6 @@ def generate_html(chapter_num, conceptual_input, problem_input, path):
|
|
| 132 |
with open(path, 'w', encoding='utf-8') as f:
|
| 133 |
f.write(rendered_html)
|
| 134 |
|
| 135 |
-
def generate_pdf(input_path, output_path):
|
| 136 |
-
try:
|
| 137 |
-
HTML(input_path).write_pdf(output_path)
|
| 138 |
-
except Exception as e:
|
| 139 |
-
logging.error(f"An error occurred when generating PDF: {e}")
|
| 140 |
-
|
| 141 |
def main_function(unit_num, conceptual_input, problem_input):
|
| 142 |
try:
|
| 143 |
if not conceptual_input and not problem_input:
|
|
@@ -148,20 +141,18 @@ def main_function(unit_num, conceptual_input, problem_input):
|
|
| 148 |
if not os.path.exists(abs_path):
|
| 149 |
os.makedirs(abs_path)
|
| 150 |
|
| 151 |
-
# Delete all existing
|
| 152 |
for file_name in os.listdir(abs_path):
|
| 153 |
-
if file_name.endswith(".
|
| 154 |
os.remove(os.path.join(abs_path, file_name))
|
| 155 |
|
| 156 |
file_name = "questions_" + str(int(unit_num))
|
| 157 |
html_path = os.path.join(abs_path, file_name + ".html")
|
| 158 |
-
pdf_path = os.path.join(abs_path, file_name + ".pdf")
|
| 159 |
|
| 160 |
generate_html(unit_num, conceptual_input, problem_input, html_path)
|
| 161 |
-
generate_pdf(html_path, pdf_path)
|
| 162 |
|
| 163 |
-
logging.info("
|
| 164 |
-
return
|
| 165 |
except Exception as e:
|
| 166 |
logging.error(f"An error occurred in main_function: {e}")
|
| 167 |
return "An error occurred. Please check the logs for more details."
|
|
@@ -173,7 +164,7 @@ iface = gr.Interface(
|
|
| 173 |
gr.Textbox(label="Conceptual Problems List (comma-separated)"),
|
| 174 |
gr.Textbox(label="Problems & Exercises List (comma-separated)")
|
| 175 |
],
|
| 176 |
-
outputs=gr.File(label="Generated
|
| 177 |
live=False
|
| 178 |
)
|
| 179 |
|
|
|
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import requests
|
| 4 |
from jinja2 import Template
|
|
|
|
| 5 |
from urllib.parse import urljoin
|
| 6 |
import os
|
| 7 |
import warnings
|
|
|
|
| 131 |
with open(path, 'w', encoding='utf-8') as f:
|
| 132 |
f.write(rendered_html)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
def main_function(unit_num, conceptual_input, problem_input):
|
| 135 |
try:
|
| 136 |
if not conceptual_input and not problem_input:
|
|
|
|
| 141 |
if not os.path.exists(abs_path):
|
| 142 |
os.makedirs(abs_path)
|
| 143 |
|
| 144 |
+
# Delete all existing HTML files in the directory
|
| 145 |
for file_name in os.listdir(abs_path):
|
| 146 |
+
if file_name.endswith(".html"):
|
| 147 |
os.remove(os.path.join(abs_path, file_name))
|
| 148 |
|
| 149 |
file_name = "questions_" + str(int(unit_num))
|
| 150 |
html_path = os.path.join(abs_path, file_name + ".html")
|
|
|
|
| 151 |
|
| 152 |
generate_html(unit_num, conceptual_input, problem_input, html_path)
|
|
|
|
| 153 |
|
| 154 |
+
logging.info("HTML generated successfully")
|
| 155 |
+
return html_path
|
| 156 |
except Exception as e:
|
| 157 |
logging.error(f"An error occurred in main_function: {e}")
|
| 158 |
return "An error occurred. Please check the logs for more details."
|
|
|
|
| 164 |
gr.Textbox(label="Conceptual Problems List (comma-separated)"),
|
| 165 |
gr.Textbox(label="Problems & Exercises List (comma-separated)")
|
| 166 |
],
|
| 167 |
+
outputs=gr.File(label="Generated HTML"),
|
| 168 |
live=False
|
| 169 |
)
|
| 170 |
|