Update app.py
Browse files
app.py
CHANGED
|
@@ -23,7 +23,7 @@ from transformers import pipeline as hf_pipeline
|
|
| 23 |
# CONFIGURATION
|
| 24 |
# =========================
|
| 25 |
|
| 26 |
-
NGROK_AUTHTOKEN = "
|
| 27 |
PORT = 7860
|
| 28 |
DEVICE = "cpu"
|
| 29 |
|
|
@@ -327,16 +327,20 @@ def generate_gradio_api(
|
|
| 327 |
# =========================
|
| 328 |
# FASTAPI
|
| 329 |
# =========================
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
api = FastAPI(title="Image Generation API")
|
| 332 |
|
| 333 |
|
| 334 |
-
@api.get("/
|
| 335 |
def health():
|
| 336 |
return {"success": True, "models": MODEL_NAMES, "device": DEVICE}
|
| 337 |
|
| 338 |
|
| 339 |
-
@api.post("/
|
| 340 |
def api_generate(request: GenerateRequest):
|
| 341 |
try:
|
| 342 |
image, is_nsfw = generate_core(
|
|
@@ -366,14 +370,14 @@ def api_generate(request: GenerateRequest):
|
|
| 366 |
"seed": request.seed,
|
| 367 |
"is_nsfw": bool(is_nsfw),
|
| 368 |
"image_format": "png",
|
| 369 |
-
"image_url": f"/
|
| 370 |
}
|
| 371 |
|
| 372 |
except Exception as exc:
|
| 373 |
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
| 374 |
|
| 375 |
|
| 376 |
-
@api.get("/
|
| 377 |
def get_image(image_id: str):
|
| 378 |
if (
|
| 379 |
not image_id.endswith(".png")
|
|
@@ -445,6 +449,7 @@ with gr.Blocks(title="CPU Image Generation API") as demo:
|
|
| 445 |
|
| 446 |
app = gr.mount_gradio_app(api, demo, path="/")
|
| 447 |
|
|
|
|
| 448 |
# =========================
|
| 449 |
# NGROK
|
| 450 |
# =========================
|
|
@@ -457,7 +462,7 @@ def start_ngrok():
|
|
| 457 |
|
| 458 |
print("=" * 50)
|
| 459 |
print("PUBLIC NGROK URL:", tunnel.public_url)
|
| 460 |
-
print("Synchronous API:", tunnel.public_url + "/
|
| 461 |
print("=" * 50)
|
| 462 |
|
| 463 |
except Exception as exc:
|
|
@@ -466,4 +471,4 @@ def start_ngrok():
|
|
| 466 |
|
| 467 |
threading.Thread(target=start_ngrok, daemon=True).start()
|
| 468 |
|
| 469 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 23 |
# CONFIGURATION
|
| 24 |
# =========================
|
| 25 |
|
| 26 |
+
NGROK_AUTHTOKEN = "c2d88cd6410843c8bfa547d37196a665" # замени на свой реальный токен
|
| 27 |
PORT = 7860
|
| 28 |
DEVICE = "cpu"
|
| 29 |
|
|
|
|
| 327 |
# =========================
|
| 328 |
# FASTAPI
|
| 329 |
# =========================
|
| 330 |
+
# ВАЖНО: префикс /custom/ вместо /api/ — Gradio 6.x сам резервирует
|
| 331 |
+
# путь /api/* под свои внутренние роуты (schema, info и т.д.) и
|
| 332 |
+
# перехватывает его раньше, чем запрос доходит до FastAPI-роутов
|
| 333 |
+
# ниже, из-за чего POST отдавал "405 Method Not Allowed, allow: GET".
|
| 334 |
|
| 335 |
api = FastAPI(title="Image Generation API")
|
| 336 |
|
| 337 |
|
| 338 |
+
@api.get("/custom/health")
|
| 339 |
def health():
|
| 340 |
return {"success": True, "models": MODEL_NAMES, "device": DEVICE}
|
| 341 |
|
| 342 |
|
| 343 |
+
@api.post("/custom/generate")
|
| 344 |
def api_generate(request: GenerateRequest):
|
| 345 |
try:
|
| 346 |
image, is_nsfw = generate_core(
|
|
|
|
| 370 |
"seed": request.seed,
|
| 371 |
"is_nsfw": bool(is_nsfw),
|
| 372 |
"image_format": "png",
|
| 373 |
+
"image_url": f"/custom/images/{image_id}",
|
| 374 |
}
|
| 375 |
|
| 376 |
except Exception as exc:
|
| 377 |
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
| 378 |
|
| 379 |
|
| 380 |
+
@api.get("/custom/images/{image_id}")
|
| 381 |
def get_image(image_id: str):
|
| 382 |
if (
|
| 383 |
not image_id.endswith(".png")
|
|
|
|
| 449 |
|
| 450 |
app = gr.mount_gradio_app(api, demo, path="/")
|
| 451 |
|
| 452 |
+
|
| 453 |
# =========================
|
| 454 |
# NGROK
|
| 455 |
# =========================
|
|
|
|
| 462 |
|
| 463 |
print("=" * 50)
|
| 464 |
print("PUBLIC NGROK URL:", tunnel.public_url)
|
| 465 |
+
print("Synchronous API:", tunnel.public_url + "/custom/generate")
|
| 466 |
print("=" * 50)
|
| 467 |
|
| 468 |
except Exception as exc:
|
|
|
|
| 471 |
|
| 472 |
threading.Thread(target=start_ngrok, daemon=True).start()
|
| 473 |
|
| 474 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|