Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -36,11 +36,13 @@ def process_pdf_to_epub(pdf_file, title, author):
|
|
36 |
|
37 |
for i in range(num_pages):
|
38 |
page_num = i + 1
|
|
|
39 |
|
40 |
try:
|
41 |
# Render page to base64 image
|
42 |
image_base64 = render_pdf_to_base64png(pdf_path, page_num, target_longest_image_dim=1024)
|
43 |
anchor_text = get_anchor_text(pdf_path, page_num, pdf_engine="pdfreport", target_length=4000)
|
|
|
44 |
prompt = build_finetuning_prompt(anchor_text)
|
45 |
|
46 |
messages = [
|
@@ -77,11 +79,17 @@ def process_pdf_to_epub(pdf_file, title, author):
|
|
77 |
decoded = f"[Processing error on page {page_num}: {str(processing_error)}]"
|
78 |
else:
|
79 |
try:
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
except Exception as decode_error:
|
83 |
decoded = f"[Decoding error on page {page_num}: {str(decode_error)}]"
|
84 |
|
|
|
|
|
85 |
# Create chapter
|
86 |
chapter = epub.EpubHtml(title=f"Page {page_num}", file_name=f"page_{page_num}.xhtml", lang="en")
|
87 |
chapter.content = f"<h1>Page {page_num}</h1><p>{decoded}</p>"
|
@@ -127,4 +135,3 @@ if __name__ == "__main__":
|
|
127 |
debug=True, # Optional: helpful if you're troubleshooting
|
128 |
allowed_paths=["/tmp"] # Optional: makes it explicit that Gradio can write here
|
129 |
)
|
130 |
-
|
|
|
36 |
|
37 |
for i in range(num_pages):
|
38 |
page_num = i + 1
|
39 |
+
print(f"Processing page {page_num}...") # Debugging line
|
40 |
|
41 |
try:
|
42 |
# Render page to base64 image
|
43 |
image_base64 = render_pdf_to_base64png(pdf_path, page_num, target_longest_image_dim=1024)
|
44 |
anchor_text = get_anchor_text(pdf_path, page_num, pdf_engine="pdfreport", target_length=4000)
|
45 |
+
print(f"Anchor text for page {page_num}: {anchor_text}") # Debugging line
|
46 |
prompt = build_finetuning_prompt(anchor_text)
|
47 |
|
48 |
messages = [
|
|
|
79 |
decoded = f"[Processing error on page {page_num}: {str(processing_error)}]"
|
80 |
else:
|
81 |
try:
|
82 |
+
# Check if the tokens are empty
|
83 |
+
if not new_tokens:
|
84 |
+
decoded = f"[No tokens generated for page {page_num}]"
|
85 |
+
else:
|
86 |
+
decoded_list = processor.tokenizer.batch_decode(new_tokens, skip_special_tokens=True)
|
87 |
+
decoded = decoded_list[0].strip() if decoded_list else "[No output generated]"
|
88 |
except Exception as decode_error:
|
89 |
decoded = f"[Decoding error on page {page_num}: {str(decode_error)}]"
|
90 |
|
91 |
+
print(f"Decoded content for page {page_num}: {decoded}") # Debugging line
|
92 |
+
|
93 |
# Create chapter
|
94 |
chapter = epub.EpubHtml(title=f"Page {page_num}", file_name=f"page_{page_num}.xhtml", lang="en")
|
95 |
chapter.content = f"<h1>Page {page_num}</h1><p>{decoded}</p>"
|
|
|
135 |
debug=True, # Optional: helpful if you're troubleshooting
|
136 |
allowed_paths=["/tmp"] # Optional: makes it explicit that Gradio can write here
|
137 |
)
|
|