Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,30 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import io
|
| 3 |
-
import base64
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
@app.post("/generate")
|
| 10 |
-
async def
|
| 11 |
try:
|
| 12 |
body = await request.json()
|
| 13 |
base64_image = body.get("image")
|
| 14 |
-
prompt = body.get("prompt", "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
# 👇そのまま返す(エコーバック)
|
| 20 |
return JSONResponse(content={
|
| 21 |
"success": True,
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
})
|
| 25 |
-
|
| 26 |
except Exception as e:
|
| 27 |
return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
+
import base64
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
@app.post("/generate")
|
| 10 |
+
async def generate_image(request: Request):
|
| 11 |
try:
|
| 12 |
body = await request.json()
|
| 13 |
base64_image = body.get("image")
|
| 14 |
+
prompt = body.get("prompt", "")
|
| 15 |
+
|
| 16 |
+
# 画像を受け取ってそのまま再エンコードして返す
|
| 17 |
+
image_data = base64.b64decode(base64_image.split(",")[-1])
|
| 18 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 19 |
|
| 20 |
+
buffered = io.BytesIO()
|
| 21 |
+
image.save(buffered, format="PNG")
|
| 22 |
+
encoded_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 23 |
|
|
|
|
| 24 |
return JSONResponse(content={
|
| 25 |
"success": True,
|
| 26 |
+
"echo_prompt": prompt,
|
| 27 |
+
"image_base64": f"data:image/png;base64,{encoded_img}"
|
| 28 |
})
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
|