Spaces:
Running
Running
Update app.py
Browse files127 unmodified lines1281291301311321331 unmodified line13513613713813914014161 unmodified lines2032042052062072082098 unmodified lines218219220221222223224225226227228229230231232233234235236237127 unmodified linesdef process_file_and_create_pdf(file):"""Main function to process a file (image or PDF) and return a path to a new PDF.The @GPU decorator ensures this function is run on the GPU."""if file is None:1 unmodified linetemp_output_dir = tempfile.mkdtemp()output_pdf_path = os.path.join(temp_output_dir, "ocr_results.pdf")input_image_for_display = Nonetry:61 unmodified linestemp_img_path = os.path.join(temp_output_dir, "original_image.png")original_image.save(temp_img_path)# Corrected the typo: `T` changed to `True`c.drawImage(temp_img_path, 50, height - 300, width=200, preserveAspectRatio=True)os.remove(temp_img_path)8 unmodified linesy = height - 50c.save()return output_pdf_path, input_image_for_displayexcept Exception as e:print(f"An error occurred: {e}")if os.path.exists(temp_output_dir):shutil.rmtree(temp_output_dir)return None, None# Gradio Interface@GPUdef process_file_for_gradio(file):output_path, input_image = process_file_and_create_pdf(file)if output_path is None:return None, Nonereturn output_path, input_imagedemo = gr.Interface(127 unmodified lines1281291301311321331341 unmodified line13613713813914014161 unmodified lines2032042052062072088 unmodified lines217218219220221222223224225226227228229230231232233234235236237238239240241242243244127 unmodified linesdef process_file_and_create_pdf(file):"""Main function to process a file (image or PDF) and return a path to a new PDF.This function will ensure the temporary output directory is cleaned up safely.The @GPU decorator ensures this function is run on the GPU."""if file is None:1 unmodified linetemp_output_dir = tempfile.mkdtemp()output_pdf_path = os.path.join(temp_output_dir, "ocr_results.pdf")input_image_for_display = Nonetry:61 unmodified linestemp_img_path = os.path.join(temp_output_dir, "original_image.png")original_image.save(temp_img_path)c.drawImage(temp_img_path, 50, height - 300, width=200, preserveAspectRatio=True)os.remove(temp_img_path)8 unmodified linesy = height - 50c.save()print(f"Generated PDF path: {output_pdf_path}")return output_pdf_path, input_image_for_displayexcept Exception as e:print(f"An error occurred: {e}")# Return None, None on errorreturn None, Nonefinally:# Ensure temporary directory is cleaned up after the function returnsif os.path.exists(temp_output_dir):print(f"Cleaning up temporary directory: {temp_output_dir}")shutil.rmtree(temp_output_dir)# Gradio Interface@GPUdef process_file_for_gradio(file):"""Wrapper function for Gradio interface.This function calls the main processing logic and returns the outputsin the format required by the gr.Interface."""output_path, input_image = process_file_and_create_pdf(file)return output_path, input_imagedemo = gr.Interface(
app.py+12-5