Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
from doctr.io import DocumentFile
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
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 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
text_output = gr.Textbox(label="Texto Extra铆do", lines=10)
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
|
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()
|
|