Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from fastapi import FastAPI, Query
|
|
8 |
from huggingface_hub import list_repo_files, hf_hub_download, upload_file
|
9 |
import io
|
10 |
import requests
|
11 |
-
|
12 |
from fastapi import FastAPI, UploadFile, File
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
|
@@ -131,7 +131,10 @@ import uuid, os
|
|
131 |
|
132 |
|
133 |
@app.post("/upload")
|
134 |
-
async def upload_images(
|
|
|
|
|
|
|
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]
|
@@ -168,22 +171,23 @@ 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 |
-
# Step 3:
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
181 |
|
182 |
return {
|
183 |
"message": f"{len(files)} file(s) uploaded",
|
184 |
"upload_folder": hf_folder_prefix,
|
185 |
"results": responses,
|
186 |
-
"
|
187 |
}
|
188 |
|
189 |
|
|
|
8 |
from huggingface_hub import list_repo_files, hf_hub_download, upload_file
|
9 |
import io
|
10 |
import requests
|
11 |
+
from fastapi import BackgroundTasks
|
12 |
from fastapi import FastAPI, UploadFile, File
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
|
|
|
131 |
|
132 |
|
133 |
@app.post("/upload")
|
134 |
+
async def upload_images(
|
135 |
+
background_tasks: BackgroundTasks,
|
136 |
+
files: List[UploadFile] = File(...)
|
137 |
+
):
|
138 |
# Step 1: Generate dynamic folder name
|
139 |
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
140 |
unique_id = uuid.uuid4().hex[:6]
|
|
|
171 |
"status": f"❌ failed: {str(e)}"
|
172 |
})
|
173 |
|
|
|
174 |
os.remove(temp_path)
|
175 |
|
176 |
+
# Step 3: Add filter job to background
|
177 |
+
def run_filter():
|
178 |
+
try:
|
179 |
+
result = filter_and_rename_images(folder=hf_folder_prefix)
|
180 |
+
print(f"🧼 Filter result: {result}")
|
181 |
+
except Exception as e:
|
182 |
+
print(f"❌ Filter failed: {str(e)}")
|
183 |
+
|
184 |
+
background_tasks.add_task(run_filter)
|
185 |
|
186 |
return {
|
187 |
"message": f"{len(files)} file(s) uploaded",
|
188 |
"upload_folder": hf_folder_prefix,
|
189 |
"results": responses,
|
190 |
+
"note": "Filtering started in background"
|
191 |
}
|
192 |
|
193 |
|