GAS17 commited on
Commit
966ab7d
verified
1 Parent(s): 80f2f53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -1,32 +1,43 @@
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()
 
1
  import gradio as gr
2
+ import io
3
  from doctr.io import DocumentFile
4
+ from doctr.models import ocr_predictor
5
 
6
+ # Initialize the OCR model
7
+ model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def ocr_process(file):
10
+ # Read the uploaded file
11
+ if file.name.lower().endswith('.pdf'):
12
+ doc = DocumentFile.from_pdf(file.name)
13
+ else:
14
+ # Assume it's an image if not PDF
15
+ image_stream = io.BytesIO(file.read())
16
+ doc = DocumentFile.from_images(image_stream)
17
 
18
+ # Perform OCR
19
+ result = model(doc)
 
20
 
21
+ # Extract text from the result
22
+ extracted_text = ""
23
+ for page in result.pages:
24
+ for block in page.blocks:
25
+ for line in block.lines:
26
+ for word in line.words:
27
+ extracted_text += word.value + " "
28
+ extracted_text += "\n"
29
+ extracted_text += "\n"
30
+
31
+ return extracted_text.strip()
32
+
33
+ # Create Gradio interface
34
+ iface = gr.Interface(
35
+ fn=ocr_process,
36
+ inputs=gr.File(label="Upload PDF or Image"),
37
+ outputs=gr.Textbox(label="Extracted Text"),
38
+ title="OCR with doctr",
39
+ description="Upload a PDF or image file to extract text using OCR."
40
+ )
41
 
42
+ # Launch the interface
43
+ iface.launch()