samyak152002 commited on
Commit
e8260de
β€’
1 Parent(s): 10622c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -36
app.py CHANGED
@@ -5,49 +5,54 @@ 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,
@@ -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()