|
import gradio as gr |
|
import gdown |
|
import os |
|
import shutil |
|
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) |
|
|
|
url = f"https://drive.google.com/uc?id={file_id}" |
|
output = os.path.join(target_path, "downloaded_file") |
|
|
|
gdown.download(url, output, quiet=False) |
|
|
|
if output.endswith('.rar'): |
|
cmd = ["rar", "x", output, target_path] |
|
elif output.endswith('.7z'): |
|
cmd = ["7z", "x", output, f"-o{target_path}"] |
|
elif output.endswith('.zip'): |
|
cmd = ["unzip", output, "-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(output) |
|
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) |
|
|