Shami96 commited on
Commit
cce0884
·
verified ·
1 Parent(s): 6afedff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -50
app.py CHANGED
@@ -1,61 +1,46 @@
1
- # app.py
2
  import gradio as gr
3
  import tempfile
4
- from pdf_extractor import extract_label_value_pairs
5
- from word_extractor import extract_red_text_with_labels, is_red_font
6
- from docx import Document
7
- from docx.shared import RGBColor
8
- import difflib
9
-
10
-
11
- def find_best_match_label(target_label, pdf_data):
12
- keys = list(pdf_data.keys())
13
- match = difflib.get_close_matches(target_label.lower(), keys, n=1, cutoff=0.4)
14
- return match[0] if match else None
15
-
16
-
17
- def replace_red_text_by_label(word_path, label_value_map):
18
- doc = Document(word_path)
19
-
20
- for table in doc.tables:
21
- for row in table.rows:
22
- cells = row.cells
23
- if len(cells) >= 2:
24
- label = cells[0].text.strip().replace(":", "").replace("\n", " ")
25
- matched_label = find_best_match_label(label, label_value_map)
26
- if not matched_label:
27
- continue
28
-
29
- new_value = label_value_map[matched_label]
30
-
31
- for para in cells[1].paragraphs:
32
- for run in para.runs:
33
- if is_red_font(run):
34
- run.text = new_value
35
- run.font.color.rgb = RGBColor(0, 0, 0) # make black
36
-
37
- temp_dir = tempfile.mkdtemp()
38
- updated_path = f"{temp_dir}/updated.docx"
39
- doc.save(updated_path)
40
- return updated_path
41
-
42
 
43
  def process_files(pdf_file, word_file):
44
- pdf_path = pdf_file.name
45
- word_path = word_file.name
46
-
47
- pdf_data = extract_label_value_pairs(pdf_path) # {label: value}
48
- word_data = extract_red_text_with_labels(word_path) # {label: [red_texts]}
49
-
50
- updated_doc_path = replace_red_text_by_label(word_path, pdf_data)
51
- return updated_doc_path
52
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  gr.Interface(
55
  fn=process_files,
56
  inputs=[
57
- gr.File(label="Upload PDF File", type="filepath"),
58
- gr.File(label="Upload Word File", type="filepath")
59
  ],
60
  outputs=gr.File(label="Download Updated Word File"),
61
  title="Red Text Replacer",
 
 
1
  import gradio as gr
2
  import tempfile
3
+ import os
4
+ import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def process_files(pdf_file, word_file):
7
+ # 1. Save the uploaded files to temp
8
+ temp_dir = tempfile.mkdtemp()
9
+ pdf_path = os.path.join(temp_dir, "input.pdf")
10
+ word_path = os.path.join(temp_dir, "input.docx")
11
+ with open(pdf_path, "wb") as f:
12
+ f.write(pdf_file.read())
13
+ with open(word_path, "wb") as f:
14
+ f.write(word_file.read())
15
+
16
+ # 2. Step 1: Extract PDF data to txt
17
+ pdf_txt_path = os.path.join(temp_dir, "pdf_data.txt")
18
+ subprocess.run(["python", "extract_pdf_data.py", pdf_path, pdf_txt_path], check=True)
19
+
20
+ # 3. Step 2: Extract red text from Word to JSON
21
+ word_json_path = os.path.join(temp_dir, "word_data.json")
22
+ subprocess.run(["python", "extract_red_text.py", word_path, word_json_path], check=True)
23
+
24
+ # 4. Step 3: Update docx JSON with PDF txt, output updated JSON
25
+ updated_json_path = os.path.join(temp_dir, "updated_word_data.json")
26
+ subprocess.run([
27
+ "python", "update_docx_with_pdf.py", word_json_path, pdf_txt_path, updated_json_path
28
+ ], check=True)
29
+
30
+ # 5. Step 4: Compare word file with updated JSON and update docx
31
+ final_docx_path = os.path.join(temp_dir, "updated.docx")
32
+ subprocess.run([
33
+ "python", "updated_word.py", word_path, updated_json_path, final_docx_path
34
+ ], check=True)
35
+
36
+ # 6. Return final docx file
37
+ return final_docx_path
38
 
39
  gr.Interface(
40
  fn=process_files,
41
  inputs=[
42
+ gr.File(label="Upload PDF File"),
43
+ gr.File(label="Upload Word File")
44
  ],
45
  outputs=gr.File(label="Download Updated Word File"),
46
  title="Red Text Replacer",