import gradio as gr import os import subprocess def download_and_extract(file_id: str, folder_name: str): try: base_path = "/workspace/kohya_ss/" target_path = os.path.join(base_path, folder_name) os.makedirs(target_path, exist_ok=True) download_cmd = ["gdown", "--id", file_id, "--output", target_path] download_process = subprocess.run(download_cmd, capture_output=True, text=True) if download_process.returncode != 0: return f"Failed to download file: {download_process.stderr}" # Находим загруженный файл files = os.listdir(target_path) if not files: return "No files found in the target directory." downloaded_file = os.path.join(target_path, files[0]) if downloaded_file.endswith('.rar'): cmd = ["rar", "x", downloaded_file, target_path] elif downloaded_file.endswith('.7z'): cmd = ["7z", "x", downloaded_file, f"-o{target_path}"] elif downloaded_file.endswith('.zip'): cmd = ["unzip", downloaded_file, "-d", target_path] else: return "Unsupported file type. Only .rar, .7z, and .zip are supported." process = subprocess.run(cmd, capture_output=True, text=True) # Проверка на успешность распаковки if process.returncode == 0: os.remove(downloaded_file) return f"File downloaded and extracted successfully in {target_path}." else: return f"Error during extraction: {process.stderr}" except Exception as e: return str(e) demo = gr.Interface( fn=download_and_extract, inputs=[gr.Textbox(label="Google Drive File ID"), gr.Textbox(label="Folder Name")], outputs=[gr.Textbox(label="Status")], title="Google Drive File Downloader and Extractor", description="Enter a Google Drive file ID and folder name to download and extract its contents. Supported formats: .rar, .7z, .zip." ) if __name__ == "__main__": demo.launch(share=True)