Commit ·
72f3ab6
1
Parent(s): 11ef3eb
javascript_autotrigger
Browse files
app.py
CHANGED
|
@@ -67,9 +67,10 @@ def merge_overlapping_rectangles(rectangles):
|
|
| 67 |
merged_rects.append(merged_rect)
|
| 68 |
return merged_rects
|
| 69 |
|
| 70 |
-
def markup_color_regions(doc, color_comment_pairs, tolerance=30):
|
| 71 |
for page_num in range(len(doc)):
|
| 72 |
page = doc[page_num]
|
|
|
|
| 73 |
for color_comment_pair in color_comment_pairs:
|
| 74 |
target_color = color_comment_pair['color']
|
| 75 |
comment = color_comment_pair['comment']
|
|
@@ -85,7 +86,7 @@ def markup_color_regions(doc, color_comment_pairs, tolerance=30):
|
|
| 85 |
annot.set_info({"title": "Markup", "content": comment})
|
| 86 |
annot.update()
|
| 87 |
|
| 88 |
-
def process_pdf_files(input_pdfs, selected_color_comment_indices, tolerance, custom_color, custom_comment, custom_stroke_color):
|
| 89 |
color_comment_pairs = [
|
| 90 |
{
|
| 91 |
"color": (235, 128, 138),
|
|
@@ -116,10 +117,10 @@ def process_pdf_files(input_pdfs, selected_color_comment_indices, tolerance, cus
|
|
| 116 |
modified_pdfs = []
|
| 117 |
|
| 118 |
# Process each input PDF file
|
| 119 |
-
for pdf_file in input_pdfs:
|
| 120 |
with open(pdf_file.name, "rb") as file_stream:
|
| 121 |
doc = fitz.open(stream=file_stream.read(), filetype="pdf")
|
| 122 |
-
markup_color_regions(doc, selected_color_comment_pairs, tolerance)
|
| 123 |
|
| 124 |
# Save the modified PDF in memory
|
| 125 |
pdf_in_memory = io.BytesIO()
|
|
@@ -152,8 +153,18 @@ def process_pdf_files(input_pdfs, selected_color_comment_indices, tolerance, cus
|
|
| 152 |
with open(temp_filename, 'wb') as tmp_file:
|
| 153 |
tmp_file.write(zip_in_memory.read())
|
| 154 |
|
| 155 |
-
# Return the downloadable file path as a
|
| 156 |
-
return f'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
# Define the Gradio interface
|
| 159 |
interface = gr.Interface(
|
|
@@ -170,7 +181,7 @@ interface = gr.Interface(
|
|
| 170 |
gr.Textbox(label="Custom Comment", placeholder="Enter custom comment for this color"),
|
| 171 |
gr.Textbox(label="Custom Stroke Color (R,G,B)", placeholder="Enter stroke color in RGB format, e.g., 0,0,255")
|
| 172 |
],
|
| 173 |
-
outputs="html", # Return HTML content with
|
| 174 |
title="PDF Color Region Markup"
|
| 175 |
)
|
| 176 |
|
|
|
|
| 67 |
merged_rects.append(merged_rect)
|
| 68 |
return merged_rects
|
| 69 |
|
| 70 |
+
def markup_color_regions(doc, color_comment_pairs, tolerance=30, progress=None):
|
| 71 |
for page_num in range(len(doc)):
|
| 72 |
page = doc[page_num]
|
| 73 |
+
progress((page_num + 1) / len(doc)) # Update the progress as each page is processed
|
| 74 |
for color_comment_pair in color_comment_pairs:
|
| 75 |
target_color = color_comment_pair['color']
|
| 76 |
comment = color_comment_pair['comment']
|
|
|
|
| 86 |
annot.set_info({"title": "Markup", "content": comment})
|
| 87 |
annot.update()
|
| 88 |
|
| 89 |
+
def process_pdf_files(input_pdfs, selected_color_comment_indices, tolerance, custom_color, custom_comment, custom_stroke_color, progress=gr.Progress()):
|
| 90 |
color_comment_pairs = [
|
| 91 |
{
|
| 92 |
"color": (235, 128, 138),
|
|
|
|
| 117 |
modified_pdfs = []
|
| 118 |
|
| 119 |
# Process each input PDF file
|
| 120 |
+
for i, pdf_file in enumerate(input_pdfs):
|
| 121 |
with open(pdf_file.name, "rb") as file_stream:
|
| 122 |
doc = fitz.open(stream=file_stream.read(), filetype="pdf")
|
| 123 |
+
markup_color_regions(doc, selected_color_comment_pairs, tolerance, progress=lambda p: progress((i + p) / len(input_pdfs)))
|
| 124 |
|
| 125 |
# Save the modified PDF in memory
|
| 126 |
pdf_in_memory = io.BytesIO()
|
|
|
|
| 153 |
with open(temp_filename, 'wb') as tmp_file:
|
| 154 |
tmp_file.write(zip_in_memory.read())
|
| 155 |
|
| 156 |
+
# Return the downloadable file path as a JS script for auto-download
|
| 157 |
+
return f'''
|
| 158 |
+
<script>
|
| 159 |
+
var link = document.createElement('a');
|
| 160 |
+
link.href = '{temp_filename}';
|
| 161 |
+
link.download = '{zip_filename}';
|
| 162 |
+
document.body.appendChild(link);
|
| 163 |
+
link.click();
|
| 164 |
+
document.body.removeChild(link);
|
| 165 |
+
</script>
|
| 166 |
+
File is being downloaded automatically.
|
| 167 |
+
'''
|
| 168 |
|
| 169 |
# Define the Gradio interface
|
| 170 |
interface = gr.Interface(
|
|
|
|
| 181 |
gr.Textbox(label="Custom Comment", placeholder="Enter custom comment for this color"),
|
| 182 |
gr.Textbox(label="Custom Stroke Color (R,G,B)", placeholder="Enter stroke color in RGB format, e.g., 0,0,255")
|
| 183 |
],
|
| 184 |
+
outputs="html", # Return HTML content with JavaScript for download
|
| 185 |
title="PDF Color Region Markup"
|
| 186 |
)
|
| 187 |
|