Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import shutil | |
| import tempfile | |
| from reduce_and_convert_PDF import reduce_and_convert | |
| def clear_gradio_temp(exclude_files): | |
| temp_dir = tempfile.gettempdir() | |
| for folder in os.listdir(temp_dir): | |
| folder_path = os.path.join(temp_dir, folder) | |
| if "gradio" in folder_path.lower(): | |
| should_skip = any(file_path.startswith(folder_path) for file_path in exclude_files) | |
| if should_skip: | |
| continue | |
| try: | |
| shutil.rmtree(folder_path) | |
| print(f"Deleted: {folder_path}") | |
| except Exception as e: | |
| print(f"Failed to delete {folder_path}: {e}") | |
| def ui(input_files): | |
| clear_gradio_temp(input_files) | |
| output_zip = "./output.zip" | |
| if os.path.exists(output_zip): | |
| os.remove(output_zip) | |
| input_folder = "./input_folder" | |
| if os.path.exists(input_folder): | |
| shutil.rmtree(input_folder) | |
| os.makedirs(input_folder) | |
| # Move files into the extract_folder | |
| for file_path in input_files: | |
| print(file_path) | |
| shutil.copy(file_path, input_folder) | |
| reduce_and_convert(input_folder) | |
| return output_zip | |
| with gr.Blocks() as appli: | |
| gr.Markdown("## CBCR PDF to Excel Conversion Tool") | |
| input_files = gr.File(label="Select an input folder", file_count="directory") | |
| process_button = gr.Button("Process Files") | |
| download_link = gr.File(label="Processed Zip") | |
| process_button.click(fn=ui, inputs=input_files, outputs=download_link) | |
| appli.launch() | |