Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -129,9 +129,10 @@ from typing import List
|
|
129 |
from datetime import datetime
|
130 |
import uuid, os
|
131 |
|
|
|
132 |
@app.post("/upload")
|
133 |
async def upload_images(files: List[UploadFile] = File(...)):
|
134 |
-
# Step 1:
|
135 |
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
136 |
unique_id = uuid.uuid4().hex[:6]
|
137 |
folder_name = f"upload_{timestamp}_{unique_id}"
|
@@ -139,20 +140,19 @@ async def upload_images(files: List[UploadFile] = File(...)):
|
|
139 |
|
140 |
responses = []
|
141 |
|
|
|
142 |
for file in files:
|
143 |
filename = file.filename
|
144 |
contents = await file.read()
|
145 |
-
|
146 |
temp_path = f"/tmp/{filename}"
|
147 |
with open(temp_path, "wb") as f:
|
148 |
f.write(contents)
|
149 |
|
150 |
try:
|
151 |
-
# Upload to Hugging Face with dynamic folder path
|
152 |
upload_file(
|
153 |
path_or_fileobj=temp_path,
|
154 |
path_in_repo=f"{hf_folder_prefix}/{filename}",
|
155 |
-
repo_id=
|
156 |
repo_type="model",
|
157 |
commit_message=f"Upload {filename} to {hf_folder_prefix}",
|
158 |
token=True,
|
@@ -168,13 +168,22 @@ async def upload_images(files: List[UploadFile] = File(...)):
|
|
168 |
"status": f"❌ failed: {str(e)}"
|
169 |
})
|
170 |
|
171 |
-
# Clean up
|
172 |
os.remove(temp_path)
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
return {
|
175 |
-
"message": f"{len(files)} file(s)
|
176 |
"upload_folder": hf_folder_prefix,
|
177 |
-
"results": responses
|
|
|
178 |
}
|
179 |
|
180 |
|
|
|
129 |
from datetime import datetime
|
130 |
import uuid, os
|
131 |
|
132 |
+
|
133 |
@app.post("/upload")
|
134 |
async def upload_images(files: List[UploadFile] = File(...)):
|
135 |
+
# Step 1: Generate dynamic folder name
|
136 |
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
137 |
unique_id = uuid.uuid4().hex[:6]
|
138 |
folder_name = f"upload_{timestamp}_{unique_id}"
|
|
|
140 |
|
141 |
responses = []
|
142 |
|
143 |
+
# Step 2: Save and upload each image
|
144 |
for file in files:
|
145 |
filename = file.filename
|
146 |
contents = await file.read()
|
|
|
147 |
temp_path = f"/tmp/{filename}"
|
148 |
with open(temp_path, "wb") as f:
|
149 |
f.write(contents)
|
150 |
|
151 |
try:
|
|
|
152 |
upload_file(
|
153 |
path_or_fileobj=temp_path,
|
154 |
path_in_repo=f"{hf_folder_prefix}/{filename}",
|
155 |
+
repo_id=T_REPO_ID,
|
156 |
repo_type="model",
|
157 |
commit_message=f"Upload {filename} to {hf_folder_prefix}",
|
158 |
token=True,
|
|
|
168 |
"status": f"❌ failed: {str(e)}"
|
169 |
})
|
170 |
|
171 |
+
# Clean up
|
172 |
os.remove(temp_path)
|
173 |
|
174 |
+
# Step 3: Call the filter function after upload
|
175 |
+
try:
|
176 |
+
filter_result = filter_and_rename_images(folder=hf_folder_prefix)
|
177 |
+
except Exception as e:
|
178 |
+
filter_result = {
|
179 |
+
"error": f"❌ Filtering failed: {str(e)}"
|
180 |
+
}
|
181 |
+
|
182 |
return {
|
183 |
+
"message": f"{len(files)} file(s) uploaded",
|
184 |
"upload_folder": hf_folder_prefix,
|
185 |
+
"results": responses,
|
186 |
+
"filter_result": filter_result
|
187 |
}
|
188 |
|
189 |
|