Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
import pdfplumber
|
| 5 |
import re
|
| 6 |
-
import
|
| 7 |
-
import json
|
| 8 |
|
| 9 |
"""
|
| 10 |
Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
|
|
@@ -31,8 +28,6 @@ def get_section(path, wanted_section, next_section):
|
|
| 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 = []
|
|
@@ -40,11 +35,7 @@ def get_section(path, wanted_section, next_section):
|
|
| 40 |
page = doc.pages[page_num]
|
| 41 |
text.append(page.extract_text())
|
| 42 |
text = " ".join(text)
|
| 43 |
-
|
| 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 |
|
|
@@ -74,51 +65,45 @@ def format_section1(section1_text):
|
|
| 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 |
return result_section1_dict['TOPIC']
|
| 123 |
|
| 124 |
def get_first_page_text(path):
|
|
@@ -127,8 +112,8 @@ def get_first_page_text(path):
|
|
| 127 |
return doc.pages[0].extract_text()
|
| 128 |
|
| 129 |
# Define the Gradio interface
|
| 130 |
-
iface = gr.Interface(fn=get_first_page_text,
|
| 131 |
-
|
| 132 |
inputs=gr.File(type="binary", label="Upload PDF"),
|
| 133 |
outputs=gr.Textbox(label="Extracted Text"),
|
| 134 |
title="PDF Text Extractor",
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import pdfplumber
|
| 3 |
import re
|
| 4 |
+
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
|
|
|
| 5 |
|
| 6 |
"""
|
| 7 |
Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
|
|
|
|
| 28 |
start_page.append(page)
|
| 29 |
if len(doc.pages[page].search(next_section, return_chars=False, case=False)) > 0:
|
| 30 |
end_page.append(page)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Extract the text between the start and end page of the wanted section
|
| 33 |
text = []
|
|
|
|
| 35 |
page = doc.pages[page_num]
|
| 36 |
text.append(page.extract_text())
|
| 37 |
text = " ".join(text)
|
| 38 |
+
final_text = text.replace("\n", " ")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return final_text
|
| 40 |
|
| 41 |
|
|
|
|
| 65 |
|
| 66 |
return result_section1_dict
|
| 67 |
|
| 68 |
+
def answer_questions(text,language="de"):
|
| 69 |
+
# Initialize the zero-shot classification pipeline
|
| 70 |
+
model_name = "deepset/gelectra-large-germanquad"
|
| 71 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
| 72 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 73 |
+
|
| 74 |
+
# Initialize the QA pipeline
|
| 75 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
| 76 |
+
questions = [
|
| 77 |
+
"Welches ist das Titel des Moduls?",
|
| 78 |
+
"Welches ist das Sektor oder das Kernthema?",
|
| 79 |
+
"Welches ist das Land?",
|
| 80 |
+
"Zu welchem Program oder EZ-Programm gehort das Projekt?"
|
| 81 |
+
#"Welche Durchführungsorganisation aus den 4 Varianten 'giz', 'kfw', 'ptb' und 'bgr' implementiert das Projekt?"
|
| 82 |
+
# "In dem Dokument was steht bei Sektor?",
|
| 83 |
+
# "In dem Dokument was steht von 'EZ-Programm' bis 'EZ-Programmziel'?",
|
| 84 |
+
# "In dem Dokument was steht bei EZ-Programmziel?",
|
| 85 |
+
# "In dem Dokument in dem Abschnitt '1. Kurzbeschreibung' was steht bei Modul?",
|
| 86 |
+
# "In dem Dokument was steht bei Zielerreichung des Moduls?",
|
| 87 |
+
# "In dem Dokument in dem Abschnitt '1. Kurzbeschreibung' was steht bei Maßnahme im Zeitplan?",
|
| 88 |
+
# "In dem Dokument was steht bei Vorschläge zur Modulanpassung?",
|
| 89 |
+
# "In dem Dokument in dem Abschnitt 'Anlage 1: Wirkungsmatrix des Moduls' was steht unter Laufzeit als erstes Datum?",
|
| 90 |
+
# "In dem Dokument in dem Abschnitt 'Anlage 1: Wirkungsmatrix des Moduls' was steht unter Laufzeit als zweites Datum?"
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
# Iterate over each question and get answers
|
| 94 |
+
for question in questions:
|
| 95 |
+
result = qa_pipeline(question=question, context=text)
|
| 96 |
+
# print(f"Question: {question}")
|
| 97 |
+
# print(f"Answer: {result['answer']}\n")
|
| 98 |
+
answers_dict[question] = result['answer']
|
| 99 |
+
return answers_dict
|
| 100 |
+
|
| 101 |
|
| 102 |
def process_pdf(path):
|
| 103 |
results_dict = {}
|
| 104 |
results_dict["1. Kurzbeschreibung"] = \
|
| 105 |
get_section(path, "1. Kurzbeschreibung", "2. Einordnung des Moduls")
|
| 106 |
+
answers = answer_questions(results_dict["1. Kurzbeschreibung"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return result_section1_dict['TOPIC']
|
| 108 |
|
| 109 |
def get_first_page_text(path):
|
|
|
|
| 112 |
return doc.pages[0].extract_text()
|
| 113 |
|
| 114 |
# Define the Gradio interface
|
| 115 |
+
# iface = gr.Interface(fn=get_first_page_text,
|
| 116 |
+
iface = gr.Interface(fn=process_pdf,
|
| 117 |
inputs=gr.File(type="binary", label="Upload PDF"),
|
| 118 |
outputs=gr.Textbox(label="Extracted Text"),
|
| 119 |
title="PDF Text Extractor",
|