-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (44 loc) · 1.63 KB
/
main.py
File metadata and controls
51 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import io
import base64
import uuid
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from PIL import Image
from tasks import image_processing # Import Celery task
from celery.result import AsyncResult
app = FastAPI()
@app.post("/process-two-images/")
async def process_two_images(
base_image: UploadFile = File(...),
target_image: UploadFile = File(...)
):
"""
API endpoint to process two uploaded images asynchronously.
Args:
base_image (UploadFile): The base image file.
target_image (UploadFile): The target image file.
Returns:
JSONResponse: Contains the task ID if successful or an error message if failed.
"""
try:
base_image_bytes = await base_image.read()
target_image_bytes = await target_image.read()
task = image_processing.delay(base_image_bytes, target_image_bytes)
return JSONResponse(content={"task_id": task.id, "message": "Processing started"})
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.get("/task-status/{task_id}")
async def get_task_status(task_id: str):
"""
API endpoint to check the status of a Celery task.
Args:
task_id (str): The unique ID of the Celery task.
Returns:
JSONResponse: Contains the task ID, current status, and result (if available).
"""
task_result = AsyncResult(task_id)
return JSONResponse(content={
"task_id": task_id,
"status": task_result.status, # PENDING, STARTED, SUCCESS, FAILURE
"result": task_result.result if task_result.ready() else None
})