Update app.py
Browse files
app.py
CHANGED
@@ -226,10 +226,9 @@ def check_reference_order(full_text: str) -> Dict[str, Any]:
|
|
226 |
"missing_references": missing_refs,
|
227 |
"is_ordered": len(out_of_order) == 0 and len(missing_refs) == 0
|
228 |
}
|
229 |
-
|
230 |
def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> bytes:
|
231 |
"""
|
232 |
-
Highlights language issues in the PDF, adds a comment box with text on the side of the page,
|
233 |
and draws arrows pointing from the highlighted text to the comment box.
|
234 |
Returns the annotated PDF as bytes.
|
235 |
"""
|
@@ -299,18 +298,29 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
299 |
highlight.set_colors(stroke=(1, 1, 0)) # Yellow color
|
300 |
highlight.update()
|
301 |
|
302 |
-
#
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
comment_y = initial_y # Position the comment box near the highlighted text
|
305 |
-
comment_rect = fitz.Rect(comment_x, comment_y, comment_x +
|
306 |
page.add_freetext_annot(comment_rect, error_text)
|
307 |
|
308 |
# Draw an arrow from the highlighted word to the comment box
|
309 |
-
|
310 |
-
|
|
|
|
|
311 |
|
312 |
# Draw the arrow
|
313 |
-
page.add_arrow(
|
314 |
|
315 |
# Save annotated PDF to bytes
|
316 |
byte_stream = io.BytesIO()
|
@@ -331,6 +341,7 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
331 |
|
332 |
|
333 |
|
|
|
334 |
# ------------------------------
|
335 |
# Main Analysis Function
|
336 |
# ------------------------------
|
|
|
226 |
"missing_references": missing_refs,
|
227 |
"is_ordered": len(out_of_order) == 0 and len(missing_refs) == 0
|
228 |
}
|
|
|
229 |
def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> bytes:
|
230 |
"""
|
231 |
+
Highlights language issues in the PDF, adds a dynamic comment box with text on the side of the page,
|
232 |
and draws arrows pointing from the highlighted text to the comment box.
|
233 |
Returns the annotated PDF as bytes.
|
234 |
"""
|
|
|
298 |
highlight.set_colors(stroke=(1, 1, 0)) # Yellow color
|
299 |
highlight.update()
|
300 |
|
301 |
+
# Dynamically calculate the position of the comment box
|
302 |
+
page_width, page_height = page.rect.width, page.rect.height
|
303 |
+
comment_box_width = min(140, page_width / 3) # Ensure the comment box width is a reasonable fraction of the page width
|
304 |
+
comment_box_height = 100 # Set a reasonable height for the comment box
|
305 |
+
|
306 |
+
# Position the comment box dynamically
|
307 |
+
if initial_x < page_width / 2: # If the highlighted text is on the left half of the page
|
308 |
+
comment_x = page_width - comment_box_width - 10 # Position it on the right side
|
309 |
+
else: # If the highlighted text is on the right half of the page
|
310 |
+
comment_x = 10 # Position it on the left side
|
311 |
+
|
312 |
comment_y = initial_y # Position the comment box near the highlighted text
|
313 |
+
comment_rect = fitz.Rect(comment_x, comment_y, comment_x + comment_box_width, comment_y + comment_box_height)
|
314 |
page.add_freetext_annot(comment_rect, error_text)
|
315 |
|
316 |
# Draw an arrow from the highlighted word to the comment box
|
317 |
+
arrow_start_x = (initial_x + final_x) / 2 # Center X of the highlighted word
|
318 |
+
arrow_start_y = (initial_y + final_y) / 2 # Center Y of the highlighted word
|
319 |
+
arrow_end_x = (comment_rect.x0 + comment_rect.x1) / 2 # Center X of the comment box
|
320 |
+
arrow_end_y = (comment_rect.y0 + comment_rect.y1) / 2 # Center Y of the comment box
|
321 |
|
322 |
# Draw the arrow
|
323 |
+
page.add_arrow((arrow_start_x, arrow_start_y), (arrow_end_x, arrow_end_y), color=(0, 0, 0), width=2)
|
324 |
|
325 |
# Save annotated PDF to bytes
|
326 |
byte_stream = io.BytesIO()
|
|
|
341 |
|
342 |
|
343 |
|
344 |
+
|
345 |
# ------------------------------
|
346 |
# Main Analysis Function
|
347 |
# ------------------------------
|