Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,14 @@ from fastapi.responses import JSONResponse
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
@app.get("/")
|
| 10 |
def root():
|
| 11 |
return {"status": "ok"}
|
| 12 |
-
|
| 13 |
@app.post("/generate")
|
| 14 |
async def generate_image(request: Request):
|
| 15 |
try:
|
|
@@ -17,11 +18,13 @@ async def generate_image(request: Request):
|
|
| 17 |
base64_image = body.get("image")
|
| 18 |
prompt = body.get("prompt", "")
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
image_data = base64.b64decode(base64_image.split(",")[-1])
|
| 22 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 23 |
|
| 24 |
-
# εγ¨γ³γ³γΌγ
|
| 25 |
buffered = io.BytesIO()
|
| 26 |
image.save(buffered, format="PNG")
|
| 27 |
encoded_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
@@ -33,8 +36,7 @@ async def generate_image(request: Request):
|
|
| 33 |
})
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
-
# γγ°εΊεγθΏ½ε
|
| 37 |
print("=== ERROR OCCURRED ===")
|
| 38 |
traceback.print_exc()
|
| 39 |
print("======================")
|
| 40 |
-
return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
|
|
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
from PIL import Image
|
| 6 |
+
import traceback # βγγγθΏ½ε
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
@app.get("/")
|
| 11 |
def root():
|
| 12 |
return {"status": "ok"}
|
| 13 |
+
|
| 14 |
@app.post("/generate")
|
| 15 |
async def generate_image(request: Request):
|
| 16 |
try:
|
|
|
|
| 18 |
base64_image = body.get("image")
|
| 19 |
prompt = body.get("prompt", "")
|
| 20 |
|
| 21 |
+
if not base64_image:
|
| 22 |
+
return JSONResponse(status_code=400, content={"success": False, "error": "image is required"})
|
| 23 |
+
|
| 24 |
+
# η»εγγ³γΌγγ¨ε€ζ
|
| 25 |
image_data = base64.b64decode(base64_image.split(",")[-1])
|
| 26 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 27 |
|
|
|
|
| 28 |
buffered = io.BytesIO()
|
| 29 |
image.save(buffered, format="PNG")
|
| 30 |
encoded_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
|
| 36 |
})
|
| 37 |
|
| 38 |
except Exception as e:
|
|
|
|
| 39 |
print("=== ERROR OCCURRED ===")
|
| 40 |
traceback.print_exc()
|
| 41 |
print("======================")
|
| 42 |
+
return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
|