File size: 2,091 Bytes
5fb914a
 
 
 
 
 
 
 
 
ac143fb
 
5fb914a
ac143fb
 
587d6d3
ac143fb
 
 
 
 
 
 
 
 
 
 
 
5fb914a
 
 
 
 
ac143fb
5fb914a
ac143fb
 
5fb914a
 
 
 
 
 
 
 
 
 
 
 
 
 
b49a5b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)