andreeabodea commited on
Commit
e7d71ae
·
verified ·
1 Parent(s): 2068c20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -37
app.py CHANGED
@@ -9,44 +9,44 @@ import json
9
  # #files = [f for f in os.listdir("/Users/andreeabodea/") if f.endswith(".pdf")]
10
  # #print(files)
11
 
12
- # """
13
- # Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
14
- # Parameters:
15
- # - path (str): The file path to the PDF file.
16
- # - wanted_section (str): The section to start extracting text from.
17
- # - next_section (str): The section to stop extracting text at.
18
- # Returns:
19
- # - text (str): The extracted text from the specified section range.
20
- # """
21
- # def get_section(path, wanted_section, next_section):
22
- # print(wanted_section)
23
 
24
- # # Open the PDF file
25
- # doc = pdfplumber.open(path)
26
- # start_page = []
27
- # end_page = []
28
 
29
- # # Find the all the pages for the specified sections
30
- # for page in range(len(doc.pages)):
31
- # if len(doc.pages[page].search(wanted_section, return_chars = False, case = False)) > 0:
32
- # start_page.append(page)
33
- # if len(doc.pages[page].search(next_section, return_chars = False, case = False)) > 0:
34
- # end_page.append(page)
35
- # print(max(start_page))
36
- # print(max(end_page))
37
 
38
- # # Extract the text between the start and end page of the wanted section
39
- # text = []
40
- # for page_num in range(max(start_page), max(end_page)):
41
- # page = doc.pages[page_num]
42
- # text.append(page.extract_text())
43
- # text = " ".join(text)
44
- # new_text = text.replace("\n", " ")
45
- # special_char_unicode_list = ["\u00e4", "\u00f6", "\u00fc", "\u00df"]
46
- # special_char_replacement_list = ["ae", "oe", "ue", "ss"]
47
- # for index, special_char in enumerate(special_char_unicode_list):
48
- # final_text = new_text.replace(special_char, special_char_replacement_list[index])
49
- # return final_text
50
 
51
 
52
  def process_pdf(path):
@@ -85,8 +85,6 @@ def process_pdf(path):
85
  #json_string = json.dumps(results_dict, indent=4)
86
  #print(json_string)
87
 
88
-
89
-
90
  # Define the Gradio interface
91
  iface = gr.Interface(fn=process_pdf,
92
  inputs=gr.File(type="binary", label="Upload PDF"),
 
9
  # #files = [f for f in os.listdir("/Users/andreeabodea/") if f.endswith(".pdf")]
10
  # #print(files)
11
 
12
+ """
13
+ Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
14
+ Parameters:
15
+ - path (str): The file path to the PDF file.
16
+ - wanted_section (str): The section to start extracting text from.
17
+ - next_section (str): The section to stop extracting text at.
18
+ Returns:
19
+ - text (str): The extracted text from the specified section range.
20
+ """
21
+ def get_section(path, wanted_section, next_section):
22
+ print(wanted_section)
23
 
24
+ # Open the PDF file
25
+ doc = pdfplumber.open(path)
26
+ start_page = []
27
+ end_page = []
28
 
29
+ # Find the all the pages for the specified sections
30
+ for page in range(len(doc.pages)):
31
+ if len(doc.pages[page].search(wanted_section, return_chars = False, case = False)) > 0:
32
+ start_page.append(page)
33
+ if len(doc.pages[page].search(next_section, return_chars = False, case = False)) > 0:
34
+ end_page.append(page)
35
+ print(max(start_page))
36
+ print(max(end_page))
37
 
38
+ # Extract the text between the start and end page of the wanted section
39
+ text = []
40
+ for page_num in range(max(start_page), max(end_page)):
41
+ page = doc.pages[page_num]
42
+ text.append(page.extract_text())
43
+ text = " ".join(text)
44
+ new_text = text.replace("\n", " ")
45
+ special_char_unicode_list = ["\u00e4", "\u00f6", "\u00fc", "\u00df"]
46
+ special_char_replacement_list = ["ae", "oe", "ue", "ss"]
47
+ for index, special_char in enumerate(special_char_unicode_list):
48
+ final_text = new_text.replace(special_char, special_char_replacement_list[index])
49
+ return final_text
50
 
51
 
52
  def process_pdf(path):
 
85
  #json_string = json.dumps(results_dict, indent=4)
86
  #print(json_string)
87
 
 
 
88
  # Define the Gradio interface
89
  iface = gr.Interface(fn=process_pdf,
90
  inputs=gr.File(type="binary", label="Upload PDF"),