Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
for word in line.words:
|
40 |
-
extracted_text += word.value + " "
|
41 |
-
extracted_text += "\n"
|
42 |
-
extracted_text += "\n"
|
43 |
-
|
44 |
-
return extracted_text.strip()
|
45 |
-
except Exception as e:
|
46 |
-
return f"Error processing file: {str(e)}"
|
47 |
-
|
48 |
-
# Create Gradio interface
|
49 |
-
iface = gr.Interface(
|
50 |
-
fn=ocr_process,
|
51 |
-
inputs=gr.File(label="Upload PDF or Image"),
|
52 |
-
outputs=gr.Textbox(label="Extracted Text"),
|
53 |
-
title="OCR with doctr",
|
54 |
-
description="Upload a PDF or image file to extract text using OCR."
|
55 |
-
)
|
56 |
-
|
57 |
-
# Launch the interface
|
58 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from doctr.io import DocumentFile
|
3 |
+
from doctr.models import ocr_predictor
|
4 |
+
|
5 |
+
# Cargar el modelo preentrenado
|
6 |
+
model = ocr_predictor(pretrained=True)
|
7 |
+
|
8 |
+
def process_file(file):
|
9 |
+
"""Procesa un archivo (PDF o imagen) con docTR y retorna el texto extra铆do."""
|
10 |
+
if file is None:
|
11 |
+
return "Por favor, sube un archivo."
|
12 |
+
|
13 |
+
# Leer el archivo subido
|
14 |
+
doc = DocumentFile.from_pdf(file.name) if file.name.endswith('.pdf') else DocumentFile.from_images(file.name)
|
15 |
+
|
16 |
+
# Realizar OCR
|
17 |
+
result = model(doc)
|
18 |
+
|
19 |
+
# Extraer el texto y retornarlo
|
20 |
+
extracted_text = "\n".join([block['text'] for page in result.pages for block in page['blocks']])
|
21 |
+
return extracted_text
|
22 |
+
|
23 |
+
# Configuraci贸n de la interfaz de Gradio
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
gr.Markdown("## OCR con docTR")
|
26 |
+
gr.Markdown("Sube un archivo PDF o una imagen para extraer texto utilizando un modelo preentrenado de docTR.")
|
27 |
+
|
28 |
+
with gr.Row():
|
29 |
+
input_file = gr.File(label="Subir archivo (PDF o imagen)")
|
30 |
+
output_text = gr.Textbox(label="Texto extra铆do", lines=10)
|
31 |
+
|
32 |
+
process_button = gr.Button("Procesar archivo")
|
33 |
+
|
34 |
+
process_button.click(fn=process_file, inputs=[input_file], outputs=[output_text])
|
35 |
+
|
36 |
+
# Ejecutar la app
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|