sblumenf commited on
Commit
6a30f2e
·
verified ·
1 Parent(s): 7cb3598

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -8,6 +8,7 @@ from PIL import Image
8
  import pandas as pd
9
  import pdfplumber
10
  import tempfile
 
11
 
12
  def parse_pdf(pdf_file, output_format, progress=gr.Progress()):
13
  """
@@ -65,14 +66,14 @@ def parse_pdf(pdf_file, output_format, progress=gr.Progress()):
65
  df = pd.DataFrame(table[1:], columns=table[0] if table[0] else None)
66
  tables.append(df)
67
 
68
- with tempfile.NamedTemporaryFile(mode="w+b", delete=False, suffix="." + output_format.lower()) as tmp:
69
  if output_format == "JSON":
70
  json_data = {
71
  "text": text,
72
  "tables": [table.to_dict(orient='records') for table in tables if not table.columns.duplicated().any()],
73
  "images": images
74
  }
75
- json.dump(json_data, tmp, indent=4)
76
  elif output_format == "Markdown":
77
  markdown_text = f"# Extracted Text\n\n{text}\n\n# Tables\n"
78
  for i, table in enumerate(tables):
@@ -83,7 +84,7 @@ def parse_pdf(pdf_file, output_format, progress=gr.Progress()):
83
  for image in images:
84
  image_path = os.path.join(os.getcwd(), image["filename"])
85
  markdown_text += f'![Image]({image_path})\n'
86
- tmp.write(markdown_text.encode('utf-8'))
87
  elif output_format == "HTML":
88
  html_text = f"<p>{text}</p>\n\n<h2>Tables</h2>\n"
89
  for i, table in enumerate(tables):
@@ -94,11 +95,13 @@ def parse_pdf(pdf_file, output_format, progress=gr.Progress()):
94
  for image in images:
95
  image_path = os.path.join(os.getcwd(), image["filename"])
96
  html_text += f'<img src="{image_path}" alt="Image"><br>\n'
97
- tmp.write(html_text.encode('utf-8'))
98
  download_path = tmp.name
 
99
  return text, download_path
100
 
101
  except Exception as main_e:
 
102
  print(f"A main error occurred: {main_e}")
103
  return "", None
104
 
 
8
  import pandas as pd
9
  import pdfplumber
10
  import tempfile
11
+ import traceback
12
 
13
  def parse_pdf(pdf_file, output_format, progress=gr.Progress()):
14
  """
 
66
  df = pd.DataFrame(table[1:], columns=table[0] if table[0] else None)
67
  tables.append(df)
68
 
69
+ with tempfile.NamedTemporaryFile(mode="wb", delete=False, suffix="." + output_format.lower()) as tmp:
70
  if output_format == "JSON":
71
  json_data = {
72
  "text": text,
73
  "tables": [table.to_dict(orient='records') for table in tables if not table.columns.duplicated().any()],
74
  "images": images
75
  }
76
+ json.dump(json_data, tmp, indent=4)
77
  elif output_format == "Markdown":
78
  markdown_text = f"# Extracted Text\n\n{text}\n\n# Tables\n"
79
  for i, table in enumerate(tables):
 
84
  for image in images:
85
  image_path = os.path.join(os.getcwd(), image["filename"])
86
  markdown_text += f'![Image]({image_path})\n'
87
+ tmp.write(markdown_text.encode('utf-8'))
88
  elif output_format == "HTML":
89
  html_text = f"<p>{text}</p>\n\n<h2>Tables</h2>\n"
90
  for i, table in enumerate(tables):
 
95
  for image in images:
96
  image_path = os.path.join(os.getcwd(), image["filename"])
97
  html_text += f'<img src="{image_path}" alt="Image"><br>\n'
98
+ tmp.write(html_text.encode('utf-8'))
99
  download_path = tmp.name
100
+
101
  return text, download_path
102
 
103
  except Exception as main_e:
104
+ traceback.print_exc() # Print full traceback to console
105
  print(f"A main error occurred: {main_e}")
106
  return "", None
107