Spaces:
Runtime error
Runtime error
| 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(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): | |
| doc = pdfplumber.open(io.BytesIO(path)) | |
| return doc | |
| # 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) | |
| # Define the Gradio interface | |
| iface = gr.Interface(fn=process_pdf, | |
| 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() |