Spaces:
Sleeping

File size: 1,126 Bytes
36555b0
 
 
 
 
 
ac81b20
36555b0
1e1e1b7
36555b0
1e1e1b7
 
36555b0
c911df7
36555b0
c911df7
36555b0
c911df7
36555b0
 
c911df7
36555b0
1e1e1b7
c911df7
1e1e1b7
36555b0
1e1e1b7
 
c911df7
1e1e1b7
c911df7
 
 
 
36555b0
c911df7
 
 
 
 
36555b0
c911df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
from fastapi import FastAPI, BackgroundTasks
from pydantic import BaseModel
from uuid import uuid4
import time

app = FastAPI()

TASKS = {}

class TransformInput(BaseModel):
    prompt: str
    image_base64: str


def run_transform(task_id: str, data: TransformInput):
    # Simulate long-running work (replace with your model later)
    time.sleep(20)

    TASKS[task_id] = {
        "status": "done",
        "image_base64": data.image_base64
    }


@app.get("/")
def root():
    return {"status": "ok"}


@app.post("/transform")
def transform(
    data: TransformInput,
    background_tasks: BackgroundTasks
):
    task_id = uuid4().hex

    TASKS[task_id] = {
        "status": "processing"
    }

    background_tasks.add_task(run_transform, task_id, data)

    return {
        "task_id": task_id
    }


@app.get("/result/{task_id}")
def get_result(task_id: str):
    task = TASKS.get(task_id)

    if not task:
        return {"status": "not_found"}

    if task["status"] != "done":
        return {"status": task["status"]}

    return {
        "status": "done",
        "image_base64": task["image_base64"]
    }