Dash-inc commited on
Commit
8891183
·
verified ·
1 Parent(s): 873d584

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -4
main.py CHANGED
@@ -219,10 +219,36 @@ def process_image(image_url):
219
 
220
 
221
  @app.post("/upscale-image", response_model=dict)
222
- async def upscale_image(request: UpscaleRequest, background_tasks: BackgroundTasks):
223
- # Add the task directly to the background
224
- background_tasks.add_task(process_image, request.image_url)
225
- return {"status": "Processing"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
 
228
 
 
219
 
220
 
221
  @app.post("/upscale-image", response_model=dict)
222
+ async def upscale_image(request: UpscaleRequest):
223
+ if aura_sr is None:
224
+ raise HTTPException(status_code=500, detail="Upscaling model not initialized.")
225
+
226
+ try:
227
+ # Fetch the image from the provided URL
228
+ response = requests.get(request.image_url)
229
+ if response.status_code != 200:
230
+ raise HTTPException(status_code=400, detail="Failed to fetch the image from the provided URL.")
231
+
232
+ img = Image.open(BytesIO(response.content))
233
+
234
+ # Perform upscaling
235
+ upscaled_image = aura_sr.upscale_4x_overlapped(img)
236
+
237
+ # Save the upscaled image locally
238
+ filename = f"upscaled_{uuid.uuid4()}.png"
239
+ filepath = save_image_locally(upscaled_image, filename)
240
+
241
+ # Generate a public URL for the upscaled image
242
+ file_url = f"/images/{filename}"
243
+
244
+ return {
245
+ "status": "Upscaling successful",
246
+ "file_path": filepath,
247
+ "file_url": file_url,
248
+ }
249
+ except Exception as e:
250
+ raise HTTPException(status_code=500, detail=f"Error during upscaling: {str(e)}")
251
+
252
 
253
 
254