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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Search models, datasets, users...
|
| 4 |
+
Models
|
| 5 |
+
Datasets
|
| 6 |
+
Spaces
|
| 7 |
+
Posts
|
| 8 |
+
Docs
|
| 9 |
+
Solutions
|
| 10 |
+
Pricing
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Spaces:
|
| 15 |
+
|
| 16 |
+
andreeabodea
|
| 17 |
+
/
|
| 18 |
+
Extract_Project_Report_Section_1
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
like
|
| 22 |
+
0
|
| 23 |
+
|
| 24 |
+
Logs
|
| 25 |
+
App
|
| 26 |
+
Files
|
| 27 |
+
Community
|
| 28 |
+
Settings
|
| 29 |
+
Extract_Project_Report_Section_1
|
| 30 |
+
/
|
| 31 |
+
app.py
|
| 32 |
+
|
| 33 |
+
andreeabodea's picture
|
| 34 |
+
andreeabodea
|
| 35 |
+
Update app.py
|
| 36 |
+
536f374
|
| 37 |
+
VERIFIED
|
| 38 |
+
about 2 hours ago
|
| 39 |
+
raw
|
| 40 |
+
history
|
| 41 |
+
blame
|
| 42 |
+
edit
|
| 43 |
+
delete
|
| 44 |
+
No virus
|
| 45 |
+
5.51 kB
|
| 46 |
+
import os
|
| 47 |
+
import pdfplumber
|
| 48 |
+
import re
|
| 49 |
+
import gradio as gr
|
| 50 |
+
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
| 51 |
+
from io import BytesIO
|
| 52 |
+
import torch
|
| 53 |
+
|
| 54 |
+
"""
|
| 55 |
+
Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
|
| 56 |
+
Parameters:
|
| 57 |
+
- path (str): The file path to the PDF file.
|
| 58 |
+
- wanted_section (str): The section to start extracting text from.
|
| 59 |
+
- next_section (str): The section to stop extracting text at.
|
| 60 |
+
Returns:
|
| 61 |
+
- text (str): The extracted text from the specified section range.
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def get_section(path, wanted_section, next_section):
|
| 66 |
+
print(wanted_section)
|
| 67 |
+
|
| 68 |
+
# Open the PDF file
|
| 69 |
+
doc = pdfplumber.open(BytesIO(path))
|
| 70 |
+
start_page = []
|
| 71 |
+
end_page = []
|
| 72 |
+
|
| 73 |
+
# Find the all the pages for the specified sections
|
| 74 |
+
for page in range(len(doc.pages)):
|
| 75 |
+
if len(doc.pages[page].search(wanted_section, return_chars=False, case=False)) > 0:
|
| 76 |
+
start_page.append(page)
|
| 77 |
+
if len(doc.pages[page].search(next_section, return_chars=False, case=False)) > 0:
|
| 78 |
+
end_page.append(page)
|
| 79 |
+
|
| 80 |
+
# Extract the text between the start and end page of the wanted section
|
| 81 |
+
text = []
|
| 82 |
+
for page_num in range(max(start_page), max(end_page)+1):
|
| 83 |
+
page = doc.pages[page_num]
|
| 84 |
+
text.append(page.extract_text())
|
| 85 |
+
text = " ".join(text)
|
| 86 |
+
final_text = text.replace("\n", " ")
|
| 87 |
+
return final_text
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def extract_between(big_string, start_string, end_string):
|
| 91 |
+
# Use a non-greedy match for content between start_string and end_string
|
| 92 |
+
pattern = re.escape(start_string) + '(.*?)' + re.escape(end_string)
|
| 93 |
+
match = re.search(pattern, big_string, re.DOTALL)
|
| 94 |
+
|
| 95 |
+
if match:
|
| 96 |
+
# Return the content without the start and end strings
|
| 97 |
+
return match.group(1)
|
| 98 |
+
else:
|
| 99 |
+
# Return None if the pattern is not found
|
| 100 |
+
return None
|
| 101 |
+
|
| 102 |
+
def format_section1(section1_text):
|
| 103 |
+
result_section1_dict = {}
|
| 104 |
+
|
| 105 |
+
result_section1_dict['TOPIC'] = extract_between(section1_text, "Sektor", "EZ-Programm")
|
| 106 |
+
result_section1_dict['PROGRAM'] = extract_between(section1_text, "Sektor", "EZ-Programm")
|
| 107 |
+
result_section1_dict['PROJECT DESCRIPTION'] = extract_between(section1_text, "EZ-Programmziel", "Datum der letzten BE")
|
| 108 |
+
result_section1_dict['PROJECT NAME'] = extract_between(section1_text, "Modul", "Modulziel")
|
| 109 |
+
result_section1_dict['OBJECTIVE'] = extract_between(section1_text, "Modulziel", "Berichtszeitraum")
|
| 110 |
+
result_section1_dict['PROGRESS'] = extract_between(section1_text, "Zielerreichung des Moduls", "Massnahme im Zeitplan")
|
| 111 |
+
result_section1_dict['STATUS'] = extract_between(section1_text, "Massnahme im Zeitplan", "Risikoeinschätzung")
|
| 112 |
+
result_section1_dict['RECOMMENDATIONS'] = extract_between(section1_text, "Vorschläge zur Modulanpas-", "Voraussichtliche")
|
| 113 |
+
|
| 114 |
+
return result_section1_dict
|
| 115 |
+
|
| 116 |
+
def answer_questions(text,language="de"):
|
| 117 |
+
# Initialize the zero-shot classification pipeline
|
| 118 |
+
model_name = "deepset/gelectra-large-germanquad"
|
| 119 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
| 120 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 121 |
+
|
| 122 |
+
# Initialize the QA pipeline
|
| 123 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
| 124 |
+
questions = [
|
| 125 |
+
"Welches ist das Titel des Moduls?",
|
| 126 |
+
"Welches ist das Sektor oder das Kernthema?",
|
| 127 |
+
"Welches ist das Land?",
|
| 128 |
+
"Zu welchem Program oder EZ-Programm gehort das Projekt?"
|
| 129 |
+
#"Welche Durchführungsorganisation aus den 4 Varianten 'giz', 'kfw', 'ptb' und 'bgr' implementiert das Projekt?"
|
| 130 |
+
# "In dem Dokument was steht bei Sektor?",
|
| 131 |
+
# "In dem Dokument was steht von 'EZ-Programm' bis 'EZ-Programmziel'?",
|
| 132 |
+
# "In dem Dokument was steht bei EZ-Programmziel?",
|
| 133 |
+
# "In dem Dokument in dem Abschnitt '1. Kurzbeschreibung' was steht bei Modul?",
|
| 134 |
+
# "In dem Dokument was steht bei Zielerreichung des Moduls?",
|
| 135 |
+
# "In dem Dokument in dem Abschnitt '1. Kurzbeschreibung' was steht bei Maßnahme im Zeitplan?",
|
| 136 |
+
# "In dem Dokument was steht bei Vorschläge zur Modulanpassung?",
|
| 137 |
+
# "In dem Dokument in dem Abschnitt 'Anlage 1: Wirkungsmatrix des Moduls' was steht unter Laufzeit als erstes Datum?",
|
| 138 |
+
# "In dem Dokument in dem Abschnitt 'Anlage 1: Wirkungsmatrix des Moduls' was steht unter Laufzeit als zweites Datum?"
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
# Iterate over each question and get answers
|
| 142 |
+
answers_dict = {}
|
| 143 |
+
|
| 144 |
+
for question in questions:
|
| 145 |
+
result = qa_pipeline(question=question, context=text)
|
| 146 |
+
# print(f"Question: {question}")
|
| 147 |
+
# print(f"Answer: {result['answer']}\n")
|
| 148 |
+
answers_dict[question] = result['answer']
|
| 149 |
+
return answers_dict
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def process_pdf(path):
|
| 153 |
+
results_dict = {}
|
| 154 |
+
results_dict["1. Kurzbeschreibung"] = \
|
| 155 |
+
get_section(path, "1. Kurzbeschreibung", "2. Einordnung des Moduls")
|
| 156 |
+
answers = answer_questions(results_dict["1. Kurzbeschreibung"])
|
| 157 |
+
return answers
|
| 158 |
+
|
| 159 |
+
def get_first_page_text(file_data):
|
| 160 |
+
doc = pdfplumber.open(BytesIO(file_data))
|
| 161 |
+
if len(doc.pages):
|
| 162 |
+
return doc.pages[0].extract_text()
|
| 163 |
+
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
|
| 166 |
+
# Define the Gradio interface
|
| 167 |
+
# iface = gr.Interface(fn=process_pdf,
|
| 168 |
+
demo = gr.Interface(fn=process_pdf,
|
| 169 |
+
inputs=gr.File(type="binary", label="Upload PDF"),
|
| 170 |
+
outputs=gr.Textbox(label="Extracted Text"),
|
| 171 |
+
title="PDF Text Extractor",
|
| 172 |
+
description="Upload a PDF file to extract.")
|
| 173 |
+
demo.launch()
|