Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import JSONResponse, StreamingResponse
|
| 3 |
from annotations import analyze_pdf
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from annotations import analyze_pdf
|
| 3 |
+
import base64
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
def process_pdf(file):
|
| 7 |
+
if file is None:
|
| 8 |
+
return {"error": "No file uploaded."}, None
|
| 9 |
+
|
| 10 |
+
# Analyze the PDF
|
| 11 |
+
issues, annotated_pdf = analyze_pdf(file)
|
| 12 |
+
|
| 13 |
+
if "error" in issues:
|
| 14 |
+
return issues, None
|
| 15 |
+
|
| 16 |
+
# Prepare issues for display
|
| 17 |
+
issues_display = f"Total Issues Found: {issues['total_issues']}\n\n"
|
| 18 |
+
for idx, issue in enumerate(issues['issues'], start=1):
|
| 19 |
+
issues_display += f"Issue {idx}:\n"
|
| 20 |
+
issues_display += f"Message: {issue['message']}\n"
|
| 21 |
+
issues_display += f"Context: {issue['context']}\n"
|
| 22 |
+
issues_display += f"Suggestions: {', '.join(issue['suggestions']) if issue['suggestions'] else 'None'}\n"
|
| 23 |
+
issues_display += f"Category: {issue['category']}\n"
|
| 24 |
+
issues_display += f"Rule ID: {issue['rule_id']}\n"
|
| 25 |
+
issues_display += f"Offset: {issue['offset']}\n"
|
| 26 |
+
issues_display += f"Length: {issue['length']}\n\n"
|
| 27 |
+
|
| 28 |
+
# Prepare annotated PDF for download
|
| 29 |
+
if annotated_pdf:
|
| 30 |
+
annotated_pdf_base64 = base64.b64encode(annotated_pdf).decode('utf-8')
|
| 31 |
+
annotated_pdf_link = f"data:application/pdf;base64,{annotated_pdf_base64}"
|
| 32 |
+
else:
|
| 33 |
+
annotated_pdf_link = None
|
| 34 |
+
|
| 35 |
+
return issues_display, annotated_pdf_link
|
| 36 |
+
|
| 37 |
+
def download_annotated_pdf(annotated_pdf_link):
|
| 38 |
+
return annotated_pdf_link
|
| 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="file")
|
| 47 |
+
analyze_button = gr.Button("Analyze PDF")
|
| 48 |
+
with gr.Column():
|
| 49 |
+
issues_output = gr.Textbox(label="Language Issues", lines=20)
|
| 50 |
+
annotated_pdf_output = gr.File(label="Download Annotated PDF")
|
| 51 |
+
|
| 52 |
+
analyze_button.click(
|
| 53 |
+
fn=process_pdf,
|
| 54 |
+
inputs=pdf_input,
|
| 55 |
+
outputs=[issues_output, annotated_pdf_output]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|