GAS17 commited on
Commit
2fd5bcb
verified
1 Parent(s): 9204aaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -57
app.py CHANGED
@@ -1,58 +1,38 @@
1
  import gradio as gr
2
- import io
3
- import sys
4
-
5
- try:
6
- from doctr.io import DocumentFile
7
- from doctr.models import ocr_predictor
8
- except ImportError:
9
- print("Error: Failed to import doctr. Please ensure it's installed correctly.")
10
- print("Python version:", sys.version)
11
- print("Python path:", sys.path)
12
- raise
13
-
14
- # Initialize the OCR model
15
- try:
16
- model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
17
- except Exception as e:
18
- print(f"Error initializing OCR model: {e}")
19
- raise
20
-
21
- def ocr_process(file):
22
- try:
23
- # Read the uploaded file
24
- if file.name.lower().endswith('.pdf'):
25
- doc = DocumentFile.from_pdf(file.name)
26
- else:
27
- # Assume it's an image if not PDF
28
- image_stream = io.BytesIO(file.read())
29
- doc = DocumentFile.from_images(image_stream)
30
-
31
- # Perform OCR
32
- result = model(doc)
33
-
34
- # Extract text from the result
35
- extracted_text = ""
36
- for page in result.pages:
37
- for block in page.blocks:
38
- for line in block.lines:
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()