Create test.py
Browse files
test.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import gdown
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import subprocess
|
6 |
+
|
7 |
+
def download_and_extract(file_id: str, folder_name: str):
|
8 |
+
try:
|
9 |
+
base_path = "/workspace/kohya_ss/"
|
10 |
+
target_path = os.path.join(base_path, folder_name)
|
11 |
+
os.makedirs(target_path, exist_ok=True)
|
12 |
+
|
13 |
+
url = f"https://drive.google.com/uc?id={file_id}"
|
14 |
+
output = os.path.join(target_path, "downloaded_file")
|
15 |
+
|
16 |
+
gdown.download(url, output, quiet=False)
|
17 |
+
|
18 |
+
if output.endswith('.rar'):
|
19 |
+
cmd = ["rar", "x", output, target_path]
|
20 |
+
elif output.endswith('.7z'):
|
21 |
+
cmd = ["7z", "x", output, f"-o{target_path}"]
|
22 |
+
elif output.endswith('.zip'):
|
23 |
+
cmd = ["unzip", output, "-d", target_path]
|
24 |
+
else:
|
25 |
+
return "Unsupported file type. Only .rar, .7z, and .zip are supported."
|
26 |
+
|
27 |
+
process = subprocess.run(cmd, capture_output=True, text=True)
|
28 |
+
|
29 |
+
if process.returncode == 0:
|
30 |
+
|
31 |
+
os.remove(output)
|
32 |
+
return f"File downloaded and extracted successfully in {target_path}."
|
33 |
+
else:
|
34 |
+
return f"Error during extraction: {process.stderr}"
|
35 |
+
except Exception as e:
|
36 |
+
return str(e)
|
37 |
+
|
38 |
+
demo = gr.Interface(
|
39 |
+
fn=download_and_extract,
|
40 |
+
inputs=[gr.Textbox(label="Google Drive File ID"), gr.Textbox(label="Folder Name")],
|
41 |
+
outputs=[gr.Textbox(label="Status")],
|
42 |
+
title="Google Drive File Downloader and Extractor",
|
43 |
+
description="Enter a Google Drive file ID and folder name to download and extract its contents. Supported formats: .rar, .7z, .zip."
|
44 |
+
)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
demo.launch()
|