Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pdfplumber
|
| 5 |
+
import re
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
"""
|
| 10 |
+
Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
|
| 11 |
+
Parameters:
|
| 12 |
+
- path (str): The file path to the PDF file.
|
| 13 |
+
- wanted_section (str): The section to start extracting text from.
|
| 14 |
+
- next_section (str): The section to stop extracting text at.
|
| 15 |
+
Returns:
|
| 16 |
+
- text (str): The extracted text from the specified section range.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_section(path, wanted_section, next_section):
|
| 21 |
+
print(wanted_section)
|
| 22 |
+
|
| 23 |
+
# Open the PDF file
|
| 24 |
+
doc = pdfplumber.open(io.BytesIO(path))
|
| 25 |
+
start_page = []
|
| 26 |
+
end_page = []
|
| 27 |
+
|
| 28 |
+
# Find the all the pages for the specified sections
|
| 29 |
+
for page in range(len(doc.pages)):
|
| 30 |
+
if len(doc.pages[page].search(wanted_section, return_chars=False, case=False)) > 0:
|
| 31 |
+
start_page.append(page)
|
| 32 |
+
if len(doc.pages[page].search(next_section, return_chars=False, case=False)) > 0:
|
| 33 |
+
end_page.append(page)
|
| 34 |
+
print(max(start_page))
|
| 35 |
+
print(max(end_page))
|
| 36 |
+
|
| 37 |
+
# Extract the text between the start and end page of the wanted section
|
| 38 |
+
text = []
|
| 39 |
+
for page_num in range(max(start_page), max(end_page)+1):
|
| 40 |
+
page = doc.pages[page_num]
|
| 41 |
+
text.append(page.extract_text())
|
| 42 |
+
text = " ".join(text)
|
| 43 |
+
new_text = text.replace("\n", " ")
|
| 44 |
+
special_char_unicode_list = ["\u00e4", "\u00f6", "\u00fc", "\u00df"]
|
| 45 |
+
special_char_replacement_list = ["ae", "oe", "ue", "ss"]
|
| 46 |
+
for index, special_char in enumerate(special_char_unicode_list):
|
| 47 |
+
final_text = new_text.replace(special_char, special_char_replacement_list[index])
|
| 48 |
+
return final_text
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def extract_between(big_string, start_string, end_string):
|
| 52 |
+
# Use a non-greedy match for content between start_string and end_string
|
| 53 |
+
pattern = re.escape(start_string) + '(.*?)' + re.escape(end_string)
|
| 54 |
+
match = re.search(pattern, big_string, re.DOTALL)
|
| 55 |
+
|
| 56 |
+
if match:
|
| 57 |
+
# Return the content without the start and end strings
|
| 58 |
+
return match.group(1)
|
| 59 |
+
else:
|
| 60 |
+
# Return None if the pattern is not found
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
def format_section1(section1_text):
|
| 64 |
+
result_section1_dict = {}
|
| 65 |
+
|
| 66 |
+
result_section1_dict['TOPIC'] = extract_between(section1_text, "Sektor", "EZ-Programm")
|
| 67 |
+
result_section1_dict['PROGRAM'] = extract_between(section1_text, "Sektor", "EZ-Programm")
|
| 68 |
+
result_section1_dict['PROJECT DESCRIPTION'] = extract_between(section1_text, "EZ-Programmziel", "Datum der letzten BE")
|
| 69 |
+
result_section1_dict['PROJECT NAME'] = extract_between(section1_text, "Modul", "Modulziel")
|
| 70 |
+
result_section1_dict['OBJECTIVE'] = extract_between(section1_text, "Modulziel", "Berichtszeitraum")
|
| 71 |
+
result_section1_dict['PROGRESS'] = extract_between(section1_text, "Zielerreichung des Moduls", "Massnahme im Zeitplan")
|
| 72 |
+
result_section1_dict['STATUS'] = extract_between(section1_text, "Massnahme im Zeitplan", "Risikoeinschätzung")
|
| 73 |
+
result_section1_dict['RECOMMENDATIONS'] = extract_between(section1_text, "Vorschläge zur Modulanpas-", "Voraussichtliche")
|
| 74 |
+
|
| 75 |
+
return result_section1_dict
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def process_pdf(path):
|
| 79 |
+
results_dict = {}
|
| 80 |
+
results_dict["1. Kurzbeschreibung"] = \
|
| 81 |
+
get_section(path, "1. Kurzbeschreibung", "2. Einordnung des Moduls")
|
| 82 |
+
"""
|
| 83 |
+
results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \
|
| 84 |
+
get_section(path, "2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm",
|
| 85 |
+
"2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls")
|
| 86 |
+
results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \
|
| 87 |
+
get_section(path, "2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm",
|
| 88 |
+
"2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls")
|
| 89 |
+
results_dict["2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls"] = \
|
| 90 |
+
get_section(path, "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls",
|
| 91 |
+
"3. Entwicklungen im Interventionsbereich")
|
| 92 |
+
results_dict["3. Entwicklungen im Interventionsbereich"] = \
|
| 93 |
+
get_section(path, "3. Entwicklungen im Interventionsbereich",
|
| 94 |
+
"4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren")
|
| 95 |
+
results_dict["4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren"] = \
|
| 96 |
+
get_section(path, "4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren",
|
| 97 |
+
"4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums")
|
| 98 |
+
results_dict["4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums"] = \
|
| 99 |
+
get_section(path, "4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums",
|
| 100 |
+
"4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit")
|
| 101 |
+
results_dict["4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit des Vorhabens"] = \
|
| 102 |
+
get_section(path, "4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit",
|
| 103 |
+
"4.4 Laufzeit und Zeitplan")
|
| 104 |
+
results_dict["4.4 Laufzeit und Zeitplan"] = \
|
| 105 |
+
get_section(path, "4.4 Laufzeit und Zeitplan", "4.5 Entstandene Kosten und Kostenverschiebungen")
|
| 106 |
+
results_dict["4.5 Entstandene Kosten und Kostenverschiebungen"] = \
|
| 107 |
+
get_section(path, "4.5 Entstandene Kosten und Kostenverschiebungen", "4.6 Bewertung der Wirkungen und Risiken")
|
| 108 |
+
results_dict["4.6 Bewertung der Wirkungen und Risiken"] = \
|
| 109 |
+
get_section(path, "4.6 Bewertung der Wirkungen und Risiken", "5. Übergeordnete Empfehlungen")
|
| 110 |
+
results_dict["5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog"] = \
|
| 111 |
+
get_section(path, "5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog",
|
| 112 |
+
"5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme")
|
| 113 |
+
results_dict[
|
| 114 |
+
"5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme interessant sein könnten"] = \
|
| 115 |
+
get_section(path, "5.2 Lernerfahrungen", "6. Testat")
|
| 116 |
+
results_dict["6. Testat (TZ)"] = \
|
| 117 |
+
get_section(path, "6. Testat", "Anlage 1: Wirkungsmatrix des Moduls")
|
| 118 |
+
"""
|
| 119 |
+
# print(results_dict)
|
| 120 |
+
result_section1_dict = format_section1(results_dict.get("1. Kurzbeschreibung"))
|
| 121 |
+
# print(result_section1_dict)
|
| 122 |
+
|
| 123 |
+
"""
|
| 124 |
+
def get_first_page_text(path):
|
| 125 |
+
doc = pdfplumber.open(io.BytesIO(path))
|
| 126 |
+
if len(doc.pages):
|
| 127 |
+
return doc.pages[0].extract_text()
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
# Define the Gradio interface
|
| 131 |
+
# iface = gr.Interface(fn=get_first_page_text,
|
| 132 |
+
iface = gr.Interface(fn=process_pdf,
|
| 133 |
+
inputs=gr.File(type="binary", label="Upload PDF"),
|
| 134 |
+
outputs=gr.Textbox(label="Extracted Text"),
|
| 135 |
+
title="PDF Text Extractor",
|
| 136 |
+
description="Upload a PDF file to extract all its text.")
|
| 137 |
+
|
| 138 |
+
iface.launch()
|