GAS17 commited on
Commit
6b1b55d
verified
1 Parent(s): cdfa935

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -1,48 +1,32 @@
1
  import gradio as gr
2
- from doctr.io import DocumentFile
3
  from doctr.models import ocr_predictor
4
- import fitz # PyMuPDF
5
- import io
6
- from PIL import Image
7
-
8
- # Initialize the OCR model
9
- model = ocr_predictor(pretrained=True)
10
 
11
- def perform_ocr(file):
12
- if file.name.lower().endswith('.pdf'):
13
- # Process PDF
14
- text = ""
15
- pdf_document = fitz.open(file.name)
16
- for page_num in range(pdf_document.page_count):
17
- page = pdf_document[page_num]
18
- pix = page.get_pixmap()
19
- img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
20
-
21
- # Convert PIL Image to bytes
22
- img_byte_arr = io.BytesIO()
23
- img.save(img_byte_arr, format='PNG')
24
- img_byte_arr = img_byte_arr.getvalue()
25
-
26
- # Perform OCR on the image
27
- doc = DocumentFile.from_images(img_byte_arr)
28
- result = model(doc)
29
- text += result.render() + "\n\n" # Add newlines between pages
30
- return text.strip()
31
- else:
32
- # Process image
33
- doc = DocumentFile.from_images(file.name)
34
- result = model(doc)
35
- return result.render()
36
 
37
- # Create Gradio interface
38
- iface = gr.Interface(
39
- fn=perform_ocr,
40
- inputs=gr.File(label="Upload PDF or Image"),
41
- outputs="text",
42
- title="OCR with doctr (PDF and Images)",
43
- description="Upload a PDF file or an image to extract text using OCR."
44
- )
 
45
 
46
- # Launch the interface
47
- iface.launch()
 
 
 
 
 
 
 
 
 
48
 
 
 
 
 
1
  import gradio as gr
 
2
  from doctr.models import ocr_predictor
3
+ from doctr.io import DocumentFile
 
 
 
 
 
4
 
5
+ # Cargar el modelo OCR de DocTR
6
+ ocr_model = ocr_predictor(pretrained=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Funci贸n para procesar un PDF y extraer texto
9
+ def extract_text_from_pdf(pdf_file):
10
+ # Leer el PDF con DocTR
11
+ doc = DocumentFile.from_pdf(pdf_file)
12
+ # Ejecutar el OCR
13
+ result = ocr_model(doc)
14
+ # Extraer el texto
15
+ text = "\n".join([block[1] for page in result.pages for block in page.blocks])
16
+ return text
17
 
18
+ # Crear la interfaz Gradio
19
+ with gr.Blocks() as demo:
20
+ gr.Markdown("# DocTR OCR para PDFs con Gradio")
21
+ gr.Markdown("Sube un archivo PDF para extraer el texto.")
22
+
23
+ with gr.Row():
24
+ pdf_input = gr.File(label="Sube tu PDF", type="file")
25
+ text_output = gr.Textbox(label="Texto Extra铆do", lines=10)
26
+
27
+ extract_button = gr.Button("Extraer Texto")
28
+ extract_button.click(extract_text_from_pdf, inputs=pdf_input, outputs=text_output)
29
 
30
+ # Ejecutar la aplicaci贸n
31
+ if __name__ == "__main__":
32
+ demo.launch()