Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -266,31 +266,62 @@ def generate(
|
|
266 |
##############################################################################
|
267 |
# 5. Gradio UI
|
268 |
##############################################################################
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
##############################################################################
|
267 |
# 5. Gradio UI
|
268 |
##############################################################################
|
269 |
+
|
270 |
+
# FastAPI app
|
271 |
+
app = FastAPI()
|
272 |
+
|
273 |
+
@app.post("/generate")
|
274 |
+
async def generate_image_api(request: Request):
|
275 |
+
try:
|
276 |
+
body = await request.json()
|
277 |
+
|
278 |
+
image_base64 = body.get("image")
|
279 |
+
subject = body.get("subject", "beautiful woman")
|
280 |
+
add_prompt = body.get("add_prompt", "")
|
281 |
+
add_neg = body.get("add_neg", "")
|
282 |
+
cfg = float(body.get("cfg", 6.0))
|
283 |
+
ip_scale = float(body.get("ip_scale", 0.65))
|
284 |
+
steps = int(body.get("steps", 20))
|
285 |
+
width = int(body.get("width", 512))
|
286 |
+
height = int(body.get("height", 768))
|
287 |
+
upscale = bool(body.get("upscale", False))
|
288 |
+
up_factor = int(body.get("up_factor", 2))
|
289 |
+
|
290 |
+
# decode image
|
291 |
+
if not image_base64:
|
292 |
+
raise ValueError("Base64画像がありません")
|
293 |
+
|
294 |
+
img_bytes = base64.b64decode(image_base64.split(",")[-1])
|
295 |
+
pil_image = Image.open(BytesIO(img_bytes)).convert("RGB")
|
296 |
+
np_image = np.array(pil_image)
|
297 |
+
|
298 |
+
result_image = generate(
|
299 |
+
face_np=np_image,
|
300 |
+
subject=subject,
|
301 |
+
add_prompt=add_prompt,
|
302 |
+
add_neg=add_neg,
|
303 |
+
cfg=cfg,
|
304 |
+
ip_scale=ip_scale,
|
305 |
+
steps=steps,
|
306 |
+
w=width,
|
307 |
+
h=height,
|
308 |
+
upscale=upscale,
|
309 |
+
up_factor=up_factor,
|
310 |
+
)
|
311 |
+
|
312 |
+
buffer = BytesIO()
|
313 |
+
result_image.save(buffer, format="PNG")
|
314 |
+
result_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
315 |
+
|
316 |
+
return {"data": ["data:image/png;base64," + result_base64]}
|
317 |
+
|
318 |
+
except Exception as e:
|
319 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
320 |
+
|
321 |
+
# Gradio UIをマウント
|
322 |
+
from app import demo # もともとの Gradio UI
|
323 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
324 |
+
|
325 |
+
if __name__ == "__main__":
|
326 |
+
import uvicorn
|
327 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|