Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,49 +5,54 @@ import io
|
|
| 5 |
|
| 6 |
def process_pdf(file):
|
| 7 |
if file is None:
|
| 8 |
-
return
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
issues_display
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
-
gr.Markdown("# PDF Language Issue Analyzer")
|
| 42 |
gr.Markdown("Upload a PDF to analyze language issues and receive an annotated PDF.")
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
with gr.Column():
|
| 46 |
-
pdf_input = gr.File(label="Upload PDF", type="
|
| 47 |
-
analyze_button = gr.Button("Analyze PDF")
|
| 48 |
with gr.Column():
|
| 49 |
-
issues_output = gr.
|
| 50 |
-
annotated_pdf_output = gr.File(label="Download Annotated PDF")
|
| 51 |
|
| 52 |
analyze_button.click(
|
| 53 |
fn=process_pdf,
|
|
@@ -55,4 +60,9 @@ with gr.Blocks() as demo:
|
|
| 55 |
outputs=[issues_output, annotated_pdf_output]
|
| 56 |
)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
def process_pdf(file):
|
| 7 |
if file is None:
|
| 8 |
+
return "β No file uploaded.", None
|
| 9 |
+
|
| 10 |
+
try:
|
| 11 |
+
# Wrap the binary data in BytesIO to make it file-like
|
| 12 |
+
file_like = io.BytesIO(file)
|
| 13 |
+
|
| 14 |
+
# Analyze the PDF
|
| 15 |
+
issues, annotated_pdf = analyze_pdf(file_like)
|
| 16 |
+
|
| 17 |
+
if "error" in issues:
|
| 18 |
+
return f"β Error: {issues['error']}", None
|
| 19 |
+
|
| 20 |
+
# Prepare issues for display
|
| 21 |
+
issues_display = f"**Total Issues Found:** {issues['total_issues']}\n\n"
|
| 22 |
+
for idx, issue in enumerate(issues['issues'], start=1):
|
| 23 |
+
issues_display += f"**Issue {idx}:**\n"
|
| 24 |
+
issues_display += f"- **Message:** {issue['message']}\n"
|
| 25 |
+
issues_display += f"- **Context:** {issue['context']}\n"
|
| 26 |
+
issues_display += f"- **Suggestions:** {', '.join(issue['suggestions']) if issue['suggestions'] else 'None'}\n"
|
| 27 |
+
issues_display += f"- **Category:** {issue['category']}\n"
|
| 28 |
+
issues_display += f"- **Rule ID:** {issue['rule_id']}\n"
|
| 29 |
+
issues_display += f"- **Offset:** {issue['offset']}\n"
|
| 30 |
+
issues_display += f"- **Length:** {issue['length']}\n\n"
|
| 31 |
+
|
| 32 |
+
# Prepare annotated PDF for download
|
| 33 |
+
if annotated_pdf:
|
| 34 |
+
# Save the annotated PDF to a BytesIO object
|
| 35 |
+
annotated_pdf_io = io.BytesIO(annotated_pdf)
|
| 36 |
+
annotated_pdf_io.name = "annotated_document.pdf" # Set a default filename
|
| 37 |
+
annotated_pdf_io.seek(0)
|
| 38 |
+
return issues_display, annotated_pdf_io
|
| 39 |
+
else:
|
| 40 |
+
return issues_display, None
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"β An unexpected error occurred: {str(e)}", None
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("# π PDF Language Issue Analyzer")
|
| 47 |
gr.Markdown("Upload a PDF to analyze language issues and receive an annotated PDF.")
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
+
pdf_input = gr.File(label="π Upload PDF", type="binary")
|
| 52 |
+
analyze_button = gr.Button("π Analyze PDF")
|
| 53 |
with gr.Column():
|
| 54 |
+
issues_output = gr.Markdown(label="π Language Issues")
|
| 55 |
+
annotated_pdf_output = gr.File(label="πΎ Download Annotated PDF")
|
| 56 |
|
| 57 |
analyze_button.click(
|
| 58 |
fn=process_pdf,
|
|
|
|
| 60 |
outputs=[issues_output, annotated_pdf_output]
|
| 61 |
)
|
| 62 |
|
| 63 |
+
gr.Markdown("""
|
| 64 |
+
---
|
| 65 |
+
**Note:** The annotated PDF highlights the detected language issues. Click the download link to view the annotated document.
|
| 66 |
+
""")
|
| 67 |
+
|
| 68 |
demo.launch()
|