Spaces:
Sleeping
Sleeping
Download app.py from ghost0912/Image-Transformer: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/ghost0912/Image-Transformer/resolve/main/app.py
- Command line
-
hf download hf://spaces/ghost0912/Image-Transformer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ghost0912/Image-Transformer/resolve/main/app.py
1.13 kB
| 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 | |
| } | |
| def root(): | |
| return {"status": "ok"} | |
| 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 | |
| } | |
| 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"] | |
| } | |