pdf-annotator / app.py
samyak152002's picture
Update app.py
e8260de verified
raw
history blame
2.58 kB
import gradio as gr
from annotations import analyze_pdf
import base64
import io
def process_pdf(file):
if file is None:
return "❌ No file uploaded.", None
try:
# Wrap the binary data in BytesIO to make it file-like
file_like = io.BytesIO(file)
# Analyze the PDF
issues, annotated_pdf = analyze_pdf(file_like)
if "error" in issues:
return f"❌ Error: {issues['error']}", None
# Prepare issues for display
issues_display = f"**Total Issues Found:** {issues['total_issues']}\n\n"
for idx, issue in enumerate(issues['issues'], start=1):
issues_display += f"**Issue {idx}:**\n"
issues_display += f"- **Message:** {issue['message']}\n"
issues_display += f"- **Context:** {issue['context']}\n"
issues_display += f"- **Suggestions:** {', '.join(issue['suggestions']) if issue['suggestions'] else 'None'}\n"
issues_display += f"- **Category:** {issue['category']}\n"
issues_display += f"- **Rule ID:** {issue['rule_id']}\n"
issues_display += f"- **Offset:** {issue['offset']}\n"
issues_display += f"- **Length:** {issue['length']}\n\n"
# Prepare annotated PDF for download
if annotated_pdf:
# Save the annotated PDF to a BytesIO object
annotated_pdf_io = io.BytesIO(annotated_pdf)
annotated_pdf_io.name = "annotated_document.pdf" # Set a default filename
annotated_pdf_io.seek(0)
return issues_display, annotated_pdf_io
else:
return issues_display, None
except Exception as e:
return f"❌ An unexpected error occurred: {str(e)}", None
with gr.Blocks() as demo:
gr.Markdown("# πŸ“„ PDF Language Issue Analyzer")
gr.Markdown("Upload a PDF to analyze language issues and receive an annotated PDF.")
with gr.Row():
with gr.Column():
pdf_input = gr.File(label="πŸ“‚ Upload PDF", type="binary")
analyze_button = gr.Button("πŸ” Analyze PDF")
with gr.Column():
issues_output = gr.Markdown(label="πŸ“ Language Issues")
annotated_pdf_output = gr.File(label="πŸ’Ύ Download Annotated PDF")
analyze_button.click(
fn=process_pdf,
inputs=pdf_input,
outputs=[issues_output, annotated_pdf_output]
)
gr.Markdown("""
---
**Note:** The annotated PDF highlights the detected language issues. Click the download link to view the annotated document.
""")
demo.launch()