Spaces:
Runtime error
Runtime error
File size: 5,341 Bytes
0b16387 dc64258 0b16387 d9f6797 0b16387 5772f88 0b16387 e7d71ae 220da13 9bcbb8b 220da13 9bcbb8b 220da13 9bcbb8b 220da13 0b16387 87af43d 9bcbb8b 220da13 5772f88 2068c20 0b16387 5fa7016 87af43d c40d6df 67a0567 3bbdac2 87af43d |
1 2 3 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import gradio as gr
import os
import pandas as pd
import pdfplumber
import re
import fitz # PyMuPDF
import json
import io
# #files = [f for f in os.listdir("/Users/andreeabodea/") if f.endswith(".pdf")]
# #print(files)
"""
Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
Parameters:
- path (str): The file path to the PDF file.
- wanted_section (str): The section to start extracting text from.
- next_section (str): The section to stop extracting text at.
Returns:
- text (str): The extracted text from the specified section range.
"""
def get_section(path, wanted_section, next_section):
print(wanted_section)
# Open the PDF file
doc = pdfplumber.open(io.BytesIO(path))
start_page = []
end_page = []
# Find the all the pages for the specified sections
for page in range(len(doc.pages)):
if len(doc.pages[page].search(wanted_section, return_chars = False, case = False)) > 0:
start_page.append(page)
if len(doc.pages[page].search(next_section, return_chars = False, case = False)) > 0:
end_page.append(page)
print(max(start_page))
print(max(end_page))
# Extract the text between the start and end page of the wanted section
text = []
for page_num in range(max(start_page), max(end_page)):
page = doc.pages[page_num]
text.append(page.extract_text())
text = " ".join(text)
new_text = text.replace("\n", " ")
special_char_unicode_list = ["\u00e4", "\u00f6", "\u00fc", "\u00df"]
special_char_replacement_list = ["ae", "oe", "ue", "ss"]
for index, special_char in enumerate(special_char_unicode_list):
final_text = new_text.replace(special_char, special_char_replacement_list[index])
return final_text
def process_pdf(path):
results_dict = {}
results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \
get_section(path, "2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm", "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls")
"""
results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \
get_section(path,"2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm", "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls")
results_dict["2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls"] = \
get_section(path, "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls", "3. Entwicklungen im Interventionsbereich")
results_dict["3. Entwicklungen im Interventionsbereich"] = \
get_section(path, "3. Entwicklungen im Interventionsbereich", "4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren")
results_dict["4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren"] = \
get_section(path, "4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren", "4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums")
results_dict["4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums"] = \
get_section(path, "4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums", "4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit")
results_dict["4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit des Vorhabens"] = \
get_section(path, "4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit", "4.4 Laufzeit und Zeitplan")
results_dict["4.4 Laufzeit und Zeitplan"] = \
get_section(path, "4.4 Laufzeit und Zeitplan", "4.5 Entstandene Kosten und Kostenverschiebungen")
results_dict["4.5 Entstandene Kosten und Kostenverschiebungen"] = \
get_section(path, "4.5 Entstandene Kosten und Kostenverschiebungen", "4.6 Bewertung der Wirkungen und Risiken")
results_dict["4.6 Bewertung der Wirkungen und Risiken"] = \
get_section(path, "4.6 Bewertung der Wirkungen und Risiken", "5. Übergeordnete Empfehlungen")
results_dict["5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog"] = \
get_section(path, "5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog", "5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme")
results_dict["5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme interessant sein könnten"] = \
get_section(path, "5.2 Lernerfahrungen", "6. Testat")
results_dict["6. Testat (TZ)"] = \
get_section(path, "6. Testat", "Anlage 1: Wirkungsmatrix des Moduls")
"""
return results_dict
#json_string = json.dumps(results_dict, indent=4)
#print(json_string)
def get_first_page_text(path):
doc = pdfplumber.open(io.BytesIO(path))
if len(doc.pages):
return doc.pages[0].extract_text()
# Define the Gradio interface
iface = gr.Interface(fn=get_first_page_text,
inputs=gr.File(type="binary", label="Upload PDF"),
outputs=gr.Textbox(label="Extracted Text"),
title="PDF Text Extractor",
description="Upload a PDF file to extract all its text.")
iface.launch() |