Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,9 @@
|
|
1 |
# app.py
|
2 |
-
import os
|
|
|
|
|
|
|
|
|
3 |
from flask import Flask, request, render_template_string
|
4 |
import gdown
|
5 |
from huggingface_hub import HfApi, login, upload_folder
|
@@ -25,43 +29,38 @@ def run_backup():
|
|
25 |
log_entries = []
|
26 |
try:
|
27 |
log_entries.append("Starting backup process...")
|
28 |
-
# Clean previous
|
29 |
shutil.rmtree(DOWNLOAD_DIR, ignore_errors=True)
|
30 |
shutil.rmtree(EXTRACT_DIR, ignore_errors=True)
|
31 |
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
32 |
os.makedirs(EXTRACT_DIR, exist_ok=True)
|
33 |
-
log_entries.append(f"
|
34 |
|
35 |
-
|
36 |
-
log_entries.append(f"Downloading from: {FOLDER_URL}")
|
37 |
gdown.download_folder(url=FOLDER_URL, output=DOWNLOAD_DIR, use_cookies=False, quiet=True)
|
38 |
-
log_entries.append("Download
|
39 |
|
40 |
-
# Extract all zip files
|
41 |
for root, _, files in os.walk(DOWNLOAD_DIR):
|
42 |
for f in files:
|
43 |
if f.endswith(".zip"):
|
44 |
zp = os.path.join(root, f)
|
45 |
with zipfile.ZipFile(zp) as z:
|
46 |
z.extractall(EXTRACT_DIR)
|
47 |
-
log_entries.append(f"Extracted: {
|
48 |
|
49 |
-
# Fix
|
50 |
bad = os.path.join(EXTRACT_DIR, "world_nither")
|
51 |
good = os.path.join(EXTRACT_DIR, "world_nether")
|
52 |
if os.path.exists(bad) and not os.path.exists(good):
|
53 |
os.rename(bad, good)
|
54 |
-
log_entries.append(
|
55 |
|
56 |
-
# Authenticate and prepare dataset
|
57 |
login(token=TOKEN)
|
58 |
api = HfApi()
|
59 |
-
log_entries.append("
|
60 |
-
|
61 |
api.create_repo(repo_id=REPO_ID, repo_type="dataset", private=False, exist_ok=True, token=TOKEN)
|
62 |
-
log_entries.append(f"Dataset
|
63 |
|
64 |
-
# Upload subfolders
|
65 |
subfolders = {
|
66 |
"world": os.path.join(EXTRACT_DIR, "world"),
|
67 |
"world_nether": os.path.join(EXTRACT_DIR, "world_nether"),
|
@@ -70,7 +69,7 @@ def run_backup():
|
|
70 |
}
|
71 |
for name, path in subfolders.items():
|
72 |
if os.path.exists(path):
|
73 |
-
log_entries.append(f"Uploading
|
74 |
upload_folder(
|
75 |
repo_id=REPO_ID,
|
76 |
folder_path=path,
|
@@ -79,17 +78,17 @@ def run_backup():
|
|
79 |
path_in_repo=name,
|
80 |
commit_message=f"add {name}"
|
81 |
)
|
82 |
-
log_entries.append(f"Uploaded
|
83 |
else:
|
84 |
-
log_entries.append(f"
|
85 |
|
86 |
last_backup_time = time.ctime()
|
87 |
-
log_entries.append(f"Backup
|
88 |
except Exception as e:
|
89 |
log_entries.append(f"Error: {str(e)}")
|
90 |
return "<br>".join(log_entries)
|
91 |
|
92 |
-
#
|
93 |
def schedule_loop():
|
94 |
while True:
|
95 |
if schedule_interval > 0:
|
@@ -100,35 +99,35 @@ def schedule_loop():
|
|
100 |
|
101 |
threading.Thread(target=schedule_loop, daemon=True).start()
|
102 |
|
103 |
-
# HTML UI
|
104 |
HTML = '''
|
105 |
<!DOCTYPE html>
|
106 |
<html>
|
107 |
<head>
|
108 |
<meta charset="utf-8">
|
109 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
110 |
-
<title>
|
111 |
<style>
|
112 |
body { font-family: sans-serif; padding: 20px; max-width: 600px; margin: auto; }
|
113 |
-
h2 { font-size:
|
114 |
input, button { width: 100%; padding: 12px; margin: 8px 0; font-size: 16px; border-radius: 6px; border: 1px solid #ccc; }
|
115 |
-
.
|
116 |
</style>
|
117 |
</head>
|
118 |
<body>
|
119 |
<h2>Minecraft Backup Controller</h2>
|
120 |
<form method="post">
|
121 |
-
<label>
|
122 |
<input type="number" name="interval" value="{{ interval }}" min="1">
|
123 |
<button type="submit">Set Timer</button>
|
124 |
</form>
|
125 |
<form method="post">
|
126 |
<input type="hidden" name="manual_run" value="1">
|
127 |
-
<button type="submit">Run
|
128 |
</form>
|
129 |
-
<div class="
|
130 |
-
|
131 |
-
|
132 |
</div>
|
133 |
</body>
|
134 |
</html>
|
@@ -144,7 +143,7 @@ def index():
|
|
144 |
else:
|
145 |
try:
|
146 |
schedule_interval = int(request.form.get("interval", "0"))
|
147 |
-
status = f"Timer set
|
148 |
except:
|
149 |
status = "Invalid interval"
|
150 |
return render_template_string(HTML, last_run=last_backup_time, interval=schedule_interval, status=status)
|
|
|
1 |
# app.py
|
2 |
+
import os
|
3 |
+
# Ensure Hugging Face cache writes to tmp
|
4 |
+
os.environ["HF_HOME"] = "/tmp/hf_home"
|
5 |
+
|
6 |
+
import shutil, zipfile, threading, time
|
7 |
from flask import Flask, request, render_template_string
|
8 |
import gdown
|
9 |
from huggingface_hub import HfApi, login, upload_folder
|
|
|
29 |
log_entries = []
|
30 |
try:
|
31 |
log_entries.append("Starting backup process...")
|
|
|
32 |
shutil.rmtree(DOWNLOAD_DIR, ignore_errors=True)
|
33 |
shutil.rmtree(EXTRACT_DIR, ignore_errors=True)
|
34 |
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
35 |
os.makedirs(EXTRACT_DIR, exist_ok=True)
|
36 |
+
log_entries.append(f"Reset directories")
|
37 |
|
38 |
+
log_entries.append(f"Downloading: {FOLDER_URL}")
|
|
|
39 |
gdown.download_folder(url=FOLDER_URL, output=DOWNLOAD_DIR, use_cookies=False, quiet=True)
|
40 |
+
log_entries.append("Download finished")
|
41 |
|
|
|
42 |
for root, _, files in os.walk(DOWNLOAD_DIR):
|
43 |
for f in files:
|
44 |
if f.endswith(".zip"):
|
45 |
zp = os.path.join(root, f)
|
46 |
with zipfile.ZipFile(zp) as z:
|
47 |
z.extractall(EXTRACT_DIR)
|
48 |
+
log_entries.append(f"Extracted: {f}")
|
49 |
|
50 |
+
# Fix folder typo
|
51 |
bad = os.path.join(EXTRACT_DIR, "world_nither")
|
52 |
good = os.path.join(EXTRACT_DIR, "world_nether")
|
53 |
if os.path.exists(bad) and not os.path.exists(good):
|
54 |
os.rename(bad, good)
|
55 |
+
log_entries.append("Fixed folder name typo")
|
56 |
|
|
|
57 |
login(token=TOKEN)
|
58 |
api = HfApi()
|
59 |
+
log_entries.append("Logged into Hugging Face")
|
60 |
+
|
61 |
api.create_repo(repo_id=REPO_ID, repo_type="dataset", private=False, exist_ok=True, token=TOKEN)
|
62 |
+
log_entries.append(f"Dataset ready: {REPO_ID}")
|
63 |
|
|
|
64 |
subfolders = {
|
65 |
"world": os.path.join(EXTRACT_DIR, "world"),
|
66 |
"world_nether": os.path.join(EXTRACT_DIR, "world_nether"),
|
|
|
69 |
}
|
70 |
for name, path in subfolders.items():
|
71 |
if os.path.exists(path):
|
72 |
+
log_entries.append(f"Uploading {name}")
|
73 |
upload_folder(
|
74 |
repo_id=REPO_ID,
|
75 |
folder_path=path,
|
|
|
78 |
path_in_repo=name,
|
79 |
commit_message=f"add {name}"
|
80 |
)
|
81 |
+
log_entries.append(f"Uploaded {name}")
|
82 |
else:
|
83 |
+
log_entries.append(f"Missing {name}, skipped")
|
84 |
|
85 |
last_backup_time = time.ctime()
|
86 |
+
log_entries.append(f"Backup done at {last_backup_time}")
|
87 |
except Exception as e:
|
88 |
log_entries.append(f"Error: {str(e)}")
|
89 |
return "<br>".join(log_entries)
|
90 |
|
91 |
+
# Scheduler thread
|
92 |
def schedule_loop():
|
93 |
while True:
|
94 |
if schedule_interval > 0:
|
|
|
99 |
|
100 |
threading.Thread(target=schedule_loop, daemon=True).start()
|
101 |
|
102 |
+
# HTML UI
|
103 |
HTML = '''
|
104 |
<!DOCTYPE html>
|
105 |
<html>
|
106 |
<head>
|
107 |
<meta charset="utf-8">
|
108 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
109 |
+
<title>Backup Panel</title>
|
110 |
<style>
|
111 |
body { font-family: sans-serif; padding: 20px; max-width: 600px; margin: auto; }
|
112 |
+
h2 { font-size: 22px; }
|
113 |
input, button { width: 100%; padding: 12px; margin: 8px 0; font-size: 16px; border-radius: 6px; border: 1px solid #ccc; }
|
114 |
+
.status { background: #f0f0f0; padding: 15px; border-radius: 8px; margin-top: 20px; white-space: pre-wrap; }
|
115 |
</style>
|
116 |
</head>
|
117 |
<body>
|
118 |
<h2>Minecraft Backup Controller</h2>
|
119 |
<form method="post">
|
120 |
+
<label>Interval (minutes):</label>
|
121 |
<input type="number" name="interval" value="{{ interval }}" min="1">
|
122 |
<button type="submit">Set Timer</button>
|
123 |
</form>
|
124 |
<form method="post">
|
125 |
<input type="hidden" name="manual_run" value="1">
|
126 |
+
<button type="submit">Run Now</button>
|
127 |
</form>
|
128 |
+
<div class="status">
|
129 |
+
Last: {{ last_run }}<br>
|
130 |
+
Log:<br>{{ status|safe }}
|
131 |
</div>
|
132 |
</body>
|
133 |
</html>
|
|
|
143 |
else:
|
144 |
try:
|
145 |
schedule_interval = int(request.form.get("interval", "0"))
|
146 |
+
status = f"Timer set: every {schedule_interval} min"
|
147 |
except:
|
148 |
status = "Invalid interval"
|
149 |
return render_template_string(HTML, last_run=last_backup_time, interval=schedule_interval, status=status)
|