Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,22 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
|
4 |
-
def
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
for
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
iface = gr.Interface(
|
19 |
-
fn=consultar_pdf,
|
20 |
-
inputs=[
|
21 |
-
gr.File(label="Cargar PDF"), # Entrada para cargar el archivo PDF
|
22 |
-
gr.Textbox(label="Consulta", placeholder="Escribe tu consulta aquí") # Entrada para la consulta
|
23 |
-
],
|
24 |
-
outputs="text" # Salida de texto con el resultado de la consulta
|
25 |
-
)
|
26 |
|
27 |
-
#
|
28 |
-
|
|
|
|
|
|
1 |
+
import pytesseract
|
2 |
+
from pdf2image import convert_from_path
|
3 |
|
4 |
+
def pdf_to_text(pdf_path, output_path):
|
5 |
+
# Convert PDF to list of images
|
6 |
+
pages = convert_from_path(pdf_path, 300)
|
7 |
|
8 |
+
# Extract text from all pages and join them
|
9 |
+
text = ""
|
10 |
+
for page in pages:
|
11 |
+
text += pytesseract.image_to_string(page)
|
12 |
+
|
13 |
+
# Write text to file
|
14 |
+
with open(output_path, "w", encoding="utf-8") as file:
|
15 |
+
file.write(text)
|
16 |
+
|
17 |
+
print(f"OCR completed. Text saved to {output_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Usage
|
20 |
+
pdf_path = 'input.pdf'
|
21 |
+
output_path = 'output.txt'
|
22 |
+
pdf_to_text(pdf_path, output_path)
|