Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647481 unmodified lineimport tempfileimport osimport subprocessdef process_files(pdf_file, word_file):# 1. Save the uploaded files to temptemp_dir = tempfile.mkdtemp()pdf_path = os.path.join(temp_dir, "input.pdf")word_path = os.path.join(temp_dir, "input.docx")with open(pdf_path, "wb") as f:f.write(pdf_file.read())with open(word_path, "wb") as f:f.write(word_file.read())# 2. Step 1: Extract PDF data to txtpdf_txt_path = os.path.join(temp_dir, "pdf_data.txt")subprocess.run(["python", "extract_pdf_data.py", pdf_path, pdf_txt_path], check=True)# 3. Step 2: Extract red text from Word to JSONword_json_path = os.path.join(temp_dir, "word_data.json")subprocess.run(["python", "extract_red_text.py", word_path, word_json_path], check=True)# 4. Step 3: Update docx JSON with PDF txt, output updated JSONupdated_json_path = os.path.join(temp_dir, "updated_word_data.json")subprocess.run(["python", "update_docx_with_pdf.py", word_json_path, pdf_txt_path, updated_json_path], check=True)# 5. Step 4: Compare word file with updated JSON and update docxfinal_docx_path = os.path.join(temp_dir, "updated.docx")subprocess.run(["python", "updated_word.py", word_path, updated_json_path, final_docx_path], check=True)# 6. Return final docx filereturn final_docx_pathgr.Interface(fn=process_files,inputs=[gr.File(label="Upload PDF File"),gr.File(label="Upload Word File")],outputs=gr.File(label="Download Updated Word File"),title="Red Text Replacer",description="Upload a PDF and Word document. Red-colored text in the Word doc will be replaced by matching content from the PDF.").launch()1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960611 unmodified lineimport tempfileimport osimport subprocessimport uuiddef process_files(pdf_file, word_file):# Each upload returns a path (str) with type="filepath"# Create a unique temp directory for each run (prevents parallel collision)temp_dir = tempfile.mkdtemp(prefix="hf_redtext_")# Copy user-uploaded files into temp directory with standard namespdf_path = os.path.join(temp_dir, "input.pdf")word_path = os.path.join(temp_dir, "input.docx")os.rename(pdf_file, pdf_path)os.rename(word_file, word_path)# Step 1: Extract PDF data to txtpdf_txt_path = os.path.join(temp_dir, "pdf_data.txt")subprocess.run(["python", "extract_pdf_data.py", pdf_path, pdf_txt_path],check=True)# Step 2: Extract red text from Word to JSONword_json_path = os.path.join(temp_dir, "word_data.json")subprocess.run(["python", "extract_red_text.py", word_path, word_json_path],check=True)# Step 3: Update docx JSON with PDF txt, output updated JSONupdated_json_path = os.path.join(temp_dir, "updated_word_data.json")subprocess.run(["python", "update_docx_with_pdf.py", word_json_path, pdf_txt_path, updated_json_path],check=True)# Step 4: Compare word file with updated JSON and update docxfinal_docx_path = os.path.join(temp_dir, "updated.docx")subprocess.run(["python", "updated_word.py", word_path, updated_json_path, final_docx_path],check=True)# Return final updated docx filereturn final_docx_pathiface = gr.Interface(fn=process_files,inputs=[gr.File(label="Upload PDF File", type="filepath"),gr.File(label="Upload Word File", type="filepath"),],outputs=gr.File(label="Download Updated Word File"),title="Red Text Replacer",description="Upload a PDF and Word document. Red-colored text in the Word doc will be replaced by matching content from the PDF.")if __name__ == "__main__":iface.launch()
app.py+36-23