Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,6 +24,10 @@ def health_check():
|
|
24 |
|
25 |
|
26 |
|
|
|
|
|
|
|
|
|
27 |
@app.post("/upload-zip")
|
28 |
async def upload_zip(file: UploadFile = File(...)):
|
29 |
if not file.filename.endswith(".zip"):
|
@@ -34,32 +38,42 @@ async def upload_zip(file: UploadFile = File(...)):
|
|
34 |
with open(temp_zip_path, "wb") as f:
|
35 |
f.write(await file.read())
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
try:
|
38 |
with tempfile.TemporaryDirectory() as extract_dir:
|
39 |
-
# Extract
|
40 |
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
|
41 |
zip_ref.extractall(extract_dir)
|
42 |
|
43 |
uploaded_files = []
|
44 |
|
45 |
-
#
|
46 |
for root_dir, _, files in os.walk(extract_dir):
|
47 |
for name in files:
|
48 |
file_path = os.path.join(root_dir, name)
|
49 |
relative_path = os.path.relpath(file_path, extract_dir)
|
50 |
-
repo_path = f"
|
51 |
|
52 |
upload_file(
|
53 |
path_or_fileobj=file_path,
|
54 |
path_in_repo=repo_path,
|
55 |
repo_id="rahul7star/ohamlab",
|
56 |
repo_type="model",
|
57 |
-
commit_message=f"Upload {relative_path}
|
58 |
token=True,
|
59 |
)
|
60 |
uploaded_files.append(repo_path)
|
61 |
|
62 |
-
return {
|
|
|
|
|
|
|
|
|
63 |
|
64 |
except Exception as e:
|
65 |
return {"error": f"β Failed to process zip: {str(e)}"}
|
|
|
24 |
|
25 |
|
26 |
|
27 |
+
from datetime import datetime
|
28 |
+
import tempfile
|
29 |
+
import uuid
|
30 |
+
|
31 |
@app.post("/upload-zip")
|
32 |
async def upload_zip(file: UploadFile = File(...)):
|
33 |
if not file.filename.endswith(".zip"):
|
|
|
38 |
with open(temp_zip_path, "wb") as f:
|
39 |
f.write(await file.read())
|
40 |
|
41 |
+
# Create a unique subfolder name inside 'demo/'
|
42 |
+
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
43 |
+
unique_id = uuid.uuid4().hex[:6]
|
44 |
+
folder_name = f"upload_{timestamp}_{unique_id}"
|
45 |
+
hf_folder_prefix = f"demo/{folder_name}"
|
46 |
+
|
47 |
try:
|
48 |
with tempfile.TemporaryDirectory() as extract_dir:
|
49 |
+
# Extract zip
|
50 |
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
|
51 |
zip_ref.extractall(extract_dir)
|
52 |
|
53 |
uploaded_files = []
|
54 |
|
55 |
+
# Upload all extracted files
|
56 |
for root_dir, _, files in os.walk(extract_dir):
|
57 |
for name in files:
|
58 |
file_path = os.path.join(root_dir, name)
|
59 |
relative_path = os.path.relpath(file_path, extract_dir)
|
60 |
+
repo_path = f"{hf_folder_prefix}/{relative_path}".replace("\\", "/")
|
61 |
|
62 |
upload_file(
|
63 |
path_or_fileobj=file_path,
|
64 |
path_in_repo=repo_path,
|
65 |
repo_id="rahul7star/ohamlab",
|
66 |
repo_type="model",
|
67 |
+
commit_message=f"Upload {relative_path} to {folder_name}",
|
68 |
token=True,
|
69 |
)
|
70 |
uploaded_files.append(repo_path)
|
71 |
|
72 |
+
return {
|
73 |
+
"message": f"β
Uploaded {len(uploaded_files)} files",
|
74 |
+
"folder": folder_name,
|
75 |
+
"files": uploaded_files,
|
76 |
+
}
|
77 |
|
78 |
except Exception as e:
|
79 |
return {"error": f"β Failed to process zip: {str(e)}"}
|