acharyaaditya26 commited on
Commit
2b25a9c
1 Parent(s): 9a34ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- import fitz # PyMuPDF
3
  from transformers import AutoModel, AutoTokenizer
4
  from PIL import Image
5
  import numpy as np
@@ -31,29 +31,33 @@ def image_to_base64(image):
31
  image.save(buffered, format="PNG")
32
  return base64.b64encode(buffered.getvalue()).decode()
33
 
34
- def pdf_to_images(pdf_path):
35
- images = []
36
- pdf_document = fitz.open(pdf_path)
37
- for page_num in range(len(pdf_document)):
38
- page = pdf_document.load_page(page_num)
39
- pix = page.get_pixmap()
40
- img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
41
- images.append(img)
42
- return images
 
 
 
 
 
 
 
43
 
44
  def run_GOT(pdf_file):
45
  unique_id = str(uuid.uuid4())
46
  pdf_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.pdf")
47
  shutil.copy(pdf_file, pdf_path)
48
 
49
- images = pdf_to_images(pdf_path)
50
  results = []
51
 
52
  try:
53
- for i, image in enumerate(images):
54
- image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}_page_{i+1}.jpg")
55
- image.save(image_path)
56
-
57
  result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}_page_{i+1}.html")
58
 
59
  res = model.chat_crop(tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
 
1
  import gradio as gr
2
+ from pdf2image import convert_from_path
3
  from transformers import AutoModel, AutoTokenizer
4
  from PIL import Image
5
  import numpy as np
 
31
  image.save(buffered, format="PNG")
32
  return base64.b64encode(buffered.getvalue()).decode()
33
 
34
+ def convert_pdf_to_images(pdf_path, output_folder):
35
+ # Ensure the output folder exists
36
+ if not os.path.exists(output_folder):
37
+ os.makedirs(output_folder)
38
+
39
+ # Convert PDF to images
40
+ images = convert_from_path(pdf_path)
41
+
42
+ # Save each image to the output folder
43
+ image_paths = []
44
+ for i, image in enumerate(images):
45
+ image_path = os.path.join(output_folder, f"page_{i + 1}.png")
46
+ image.save(image_path, 'JPEG')
47
+ image_paths.append(image_path)
48
+ print(f"Saved {image_path}")
49
+ return image_paths
50
 
51
  def run_GOT(pdf_file):
52
  unique_id = str(uuid.uuid4())
53
  pdf_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.pdf")
54
  shutil.copy(pdf_file, pdf_path)
55
 
56
+ images = convert_pdf_to_images(pdf_path, UPLOAD_FOLDER)
57
  results = []
58
 
59
  try:
60
+ for i, image_path in enumerate(images):
 
 
 
61
  result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}_page_{i+1}.html")
62
 
63
  res = model.chat_crop(tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)