rahul7star commited on
Commit
35c2f39
Β·
verified Β·
1 Parent(s): 7e22793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py CHANGED
@@ -18,6 +18,52 @@ app.add_middleware(
18
  def health_check():
19
  return {"status": "βœ… FastAPI running on Hugging Face Spaces!"}
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @app.post("/upload")
22
  async def upload_image(file: UploadFile = File(...)):
23
  filename = file.filename
 
18
  def health_check():
19
  return {"status": "βœ… FastAPI running on Hugging Face Spaces!"}
20
 
21
+
22
+
23
+
24
+
25
+ @app.post("/upload-zip")
26
+ async def upload_zip(file: UploadFile = File(...)):
27
+ if not file.filename.endswith(".zip"):
28
+ return {"error": "Please upload a .zip file"}
29
+
30
+ # Save the ZIP to /tmp
31
+ temp_zip_path = f"/tmp/{file.filename}"
32
+ with open(temp_zip_path, "wb") as f:
33
+ f.write(await file.read())
34
+
35
+ try:
36
+ with tempfile.TemporaryDirectory() as extract_dir:
37
+ # Extract all contents of the zip file
38
+ with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
39
+ zip_ref.extractall(extract_dir)
40
+
41
+ uploaded_files = []
42
+
43
+ # Walk through the extracted files and upload
44
+ for root_dir, _, files in os.walk(extract_dir):
45
+ for name in files:
46
+ file_path = os.path.join(root_dir, name)
47
+ relative_path = os.path.relpath(file_path, extract_dir)
48
+ repo_path = f"demo/{relative_path}".replace("\\", "/") # handle Windows paths
49
+
50
+ upload_file(
51
+ path_or_fileobj=file_path,
52
+ path_in_repo=repo_path,
53
+ repo_id="rahul7star/ohamlab",
54
+ repo_type="model",
55
+ commit_message=f"Upload {relative_path} from zip",
56
+ token=True,
57
+ )
58
+ uploaded_files.append(repo_path)
59
+
60
+ return {"message": f"βœ… Uploaded {len(uploaded_files)} files", "files": uploaded_files}
61
+
62
+ except Exception as e:
63
+ return {"error": f"❌ Failed to process zip: {str(e)}"}
64
+
65
+
66
+
67
  @app.post("/upload")
68
  async def upload_image(file: UploadFile = File(...)):
69
  filename = file.filename