Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import gradio as grimport tempfileimport osimport subprocessimport uuidimport shutildef 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")shutil.copy(pdf_file, pdf_path)shutil.copy(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",123456789101112131415161718192021222324252627282930313233343536373839404142434445import gradio as grimport tempfileimport osimport shutilimport subprocessdef process_files(pdf_file, word_file):# Create a unique temporary directory for this runtemp_dir = tempfile.mkdtemp(prefix="hf_redtext_")# Define standard filenames for use in the pipelinepdf_path = os.path.join(temp_dir, "input.pdf")word_path = os.path.join(temp_dir, "input.docx")pdf_txt_path = os.path.join(temp_dir, "pdf_data.txt")word_json_path = os.path.join(temp_dir, "word_data.json")updated_json_path = os.path.join(temp_dir, "updated_word_data.json")final_docx_path = os.path.join(temp_dir, "updated.docx")# Copy the uploaded files to the temp directoryshutil.copy(pdf_file, pdf_path)shutil.copy(word_file, word_path)# Step 1: Extract text from the PDFsubprocess.run(["python", "extract_pdf_data.py", pdf_path, pdf_txt_path], check=True)# Step 2: Extract red text from the Word documentsubprocess.run(["python", "extract_red_text.py", word_path, word_json_path], check=True)# Step 3: Update the Word JSON using the PDF text (calls OpenAI)subprocess.run(["python", "update_docx_with_pdf.py", word_json_path, pdf_txt_path, updated_json_path], check=True)# Step 4: Apply the updated JSON to the Word doc to create the final outputsubprocess.run(["python", "updated_word.py", word_path, updated_json_path, final_docx_path], check=True)# Return the final .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",
app.py+20-32