2023-05-27 00:48:32 +00:00
|
|
|
import logging
|
2023-05-22 09:02:00 +00:00
|
|
|
import os.path
|
2023-05-27 00:48:32 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
2023-05-20 22:32:40 +00:00
|
|
|
from asyncio import Lock
|
|
|
|
|
2023-05-27 00:48:32 +00:00
|
|
|
from fastapi import FastAPI, Query, Request
|
2023-05-20 22:32:40 +00:00
|
|
|
from fastapi.concurrency import run_in_threadpool
|
2023-05-22 07:17:59 +00:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2023-05-27 04:42:06 +00:00
|
|
|
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
|
2023-05-22 09:02:00 +00:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2023-05-20 22:32:40 +00:00
|
|
|
|
2023-10-11 05:33:24 +00:00
|
|
|
from imaginairy.http_app.stablestudio import routes
|
|
|
|
from imaginairy.http_app.utils import generate_image
|
2023-05-29 17:54:07 +00:00
|
|
|
from imaginairy.schema import ImaginePrompt
|
2023-05-20 22:32:40 +00:00
|
|
|
|
2023-05-27 00:48:32 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-05-27 04:42:06 +00:00
|
|
|
static_folder = os.path.dirname(os.path.abspath(__file__)) + "/stablestudio/dist"
|
|
|
|
|
|
|
|
|
2023-05-22 07:17:59 +00:00
|
|
|
gpu_lock = Lock()
|
2023-05-20 22:32:40 +00:00
|
|
|
|
2023-05-27 00:48:32 +00:00
|
|
|
|
2023-05-22 07:17:59 +00:00
|
|
|
app = FastAPI()
|
2023-05-20 22:32:40 +00:00
|
|
|
|
2023-05-22 07:17:59 +00:00
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=[
|
|
|
|
"http://localhost:3000",
|
|
|
|
"http://localhost:3001",
|
|
|
|
"http://localhost:3002",
|
|
|
|
],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
2023-05-20 22:32:40 +00:00
|
|
|
|
2023-05-22 07:17:59 +00:00
|
|
|
app.include_router(routes.router, prefix="/api/stablestudio")
|
2023-05-20 22:32:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/imagine")
|
2023-05-29 17:54:07 +00:00
|
|
|
async def imagine_endpoint(prompt: ImaginePrompt):
|
2023-05-22 07:17:59 +00:00
|
|
|
async with gpu_lock:
|
|
|
|
img_io = await run_in_threadpool(generate_image, prompt)
|
2023-05-20 22:32:40 +00:00
|
|
|
return StreamingResponse(img_io, media_type="image/jpg")
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/imagine")
|
|
|
|
async def imagine_get_endpoint(text: str = Query(...)):
|
2023-05-22 07:17:59 +00:00
|
|
|
async with gpu_lock:
|
2023-05-29 17:54:07 +00:00
|
|
|
img_io = await run_in_threadpool(generate_image, ImaginePrompt(prompt=text))
|
2023-05-20 22:32:40 +00:00
|
|
|
return StreamingResponse(img_io, media_type="image/jpg")
|
2023-05-22 09:02:00 +00:00
|
|
|
|
|
|
|
|
2023-05-27 04:42:06 +00:00
|
|
|
@app.get("/edit")
|
|
|
|
async def edit_redir():
|
|
|
|
return FileResponse(f"{static_folder}/index.html")
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/generate")
|
|
|
|
async def generate_redir():
|
|
|
|
return FileResponse(f"{static_folder}/index.html")
|
|
|
|
|
2023-05-22 09:02:00 +00:00
|
|
|
|
|
|
|
app.mount("/", StaticFiles(directory=static_folder, html=True), name="static")
|
2023-05-27 00:48:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(Exception)
|
|
|
|
async def exception_handler(request: Request, exc: Exception):
|
|
|
|
print(f"Unhandled error: {exc}", file=sys.stderr)
|
|
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=500,
|
|
|
|
content={"message": "Internal Server Error"},
|
|
|
|
)
|