rahul7star commited on
Commit
bf79bba
·
verified ·
1 Parent(s): a1cf657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
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(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]
@@ -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: 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
 
 
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