samyak152002 commited on
Commit
a04c0d5
β€’
1 Parent(s): 5bb5a7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -1,11 +1,13 @@
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 "❌ No file uploaded.", None
 
 
9
 
10
  try:
11
  # Wrap the binary data in BytesIO to make it file-like
@@ -18,27 +20,33 @@ def process_pdf(file):
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
 
 
1
  import gradio as gr
2
  from annotations import analyze_pdf
 
3
  import io
4
 
5
  def process_pdf(file):
6
+ MAX_SIZE = 10 * 1024 * 1024 # 10MB
7
  if file is None:
8
  return "❌ No file uploaded.", None
9
+ if len(file) > MAX_SIZE:
10
+ return "❌ File size exceeds the 10MB limit.", None
11
 
12
  try:
13
  # Wrap the binary data in BytesIO to make it file-like
 
20
  return f"❌ Error: {issues['error']}", None
21
 
22
  # Prepare issues for display
23
+ if issues['total_issues'] == 0:
24
+ issues_display = "βœ… No language issues found. Great job!"
25
+ else:
26
+ issues_display = f"**Total Issues Found:** {issues['total_issues']}\n\n"
27
+ for idx, issue in enumerate(issues['issues'], start=1):
28
+ issues_display += f"**Issue {idx}:**\n"
29
+ issues_display += f"- **Message:** {issue['message']}\n"
30
+ issues_display += f"- **Context:** {issue['context']}\n"
31
+ issues_display += f"- **Suggestions:** {', '.join(issue['suggestions']) if issue['suggestions'] else 'None'}\n"
32
+ issues_display += f"- **Category:** {issue['category']}\n"
33
+ issues_display += f"- **Rule ID:** {issue['rule_id']}\n"
34
+ issues_display += f"- **Offset:** {issue['offset']}\n"
35
+ issues_display += f"- **Length:** {issue['length']}\n\n"
36
 
37
  # Prepare annotated PDF for download
38
  if annotated_pdf:
39
+ # Return a dictionary with 'name' and 'data' keys
40
+ annotated_pdf_dict = {
41
+ "name": "annotated_document.pdf",
42
+ "data": annotated_pdf
43
+ }
44
+ return issues_display, annotated_pdf_dict
45
  else:
46
  return issues_display, None
47
 
48
+ except language_tool_python.LanguageToolError as e:
49
+ return f"❌ LanguageTool Error: {str(e)}", None
50
  except Exception as e:
51
  return f"❌ An unexpected error occurred: {str(e)}", None
52