Spaces:
Running
Running
| import gradio as gr | |
| import os | |
| from docx import Document | |
| from utils import extract_text_from_pdf, parse_pdf_to_dict | |
| def replace_red_text_with_data(doc_path, data_dict): | |
| doc = Document(doc_path) | |
| for para in doc.paragraphs: | |
| for run in para.runs: | |
| if run.font.color and run.font.color.rgb and run.font.color.rgb.hex == "FF0000": | |
| text = run.text.strip() | |
| if text in data_dict: | |
| run.text = data_dict[text] | |
| return doc | |
| def process_files(pdf_file, template_docx): | |
| # Save uploaded files temporarily | |
| pdf_path = "temp_input.pdf" | |
| doc_path = "temp_template.docx" | |
| output_path = "filled_output.docx" | |
| pdf_file.save(pdf_path) | |
| template_docx.save(doc_path) | |
| # Extract and parse PDF | |
| raw_text = extract_text_from_pdf(pdf_path) | |
| data_dict = parse_pdf_to_dict(raw_text) | |
| # Replace red text with data | |
| final_doc = replace_red_text_with_data(doc_path, data_dict) | |
| # Save final document | |
| final_doc.save(output_path) | |
| return output_path | |
| demo = gr.Interface( | |
| fn=process_files, | |
| inputs=[ | |
| gr.File(label="Upload PDF Report", file_types=[".pdf"]), | |
| gr.File(label="Upload Word Template (.docx)", file_types=[".docx"]) | |
| ], | |
| outputs=gr.File(label="Download Filled Report (.docx)"), | |
| title="Audit Report Generator", | |
| description="Upload a PDF and a Word template. This tool will auto-fill red-highlighted fields with data from the PDF." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |