open-webui/backend/apps/images/main.py

480 lines
13 KiB
Python
Raw Normal View History

2024-02-23 03:32:36 +00:00
import re
2024-02-22 02:12:01 +00:00
import requests
from fastapi import (
FastAPI,
Request,
Depends,
HTTPException,
status,
UploadFile,
File,
Form,
)
from fastapi.middleware.cors import CORSMiddleware
from faster_whisper import WhisperModel
from constants import ERROR_MESSAGES
from utils.utils import (
get_current_user,
get_admin_user,
)
2024-03-24 00:01:13 +00:00
from apps.images.utils.comfyui import ImageGenerationPayload, comfyui_generate_image
2024-02-22 02:12:01 +00:00
from utils.misc import calculate_sha256
from typing import Optional
from pydantic import BaseModel
2024-03-09 01:38:10 +00:00
from pathlib import Path
import uuid
import base64
import json
import logging
2024-03-09 01:38:10 +00:00
2024-04-11 05:39:11 +00:00
from config import (
SRC_LOG_LEVELS,
CACHE_DIR,
IMAGE_GENERATION_ENGINE,
ENABLE_IMAGE_GENERATION,
2024-04-11 05:39:11 +00:00
AUTOMATIC1111_BASE_URL,
COMFYUI_BASE_URL,
2024-04-23 11:14:31 +00:00
IMAGES_OPENAI_API_BASE_URL,
IMAGES_OPENAI_API_KEY,
IMAGE_GENERATION_MODEL,
IMAGE_SIZE,
IMAGE_STEPS,
2024-04-11 05:39:11 +00:00
)
2024-03-09 01:38:10 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["IMAGES"])
2024-03-09 01:38:10 +00:00
IMAGE_CACHE_DIR = Path(CACHE_DIR).joinpath("./image/generations/")
IMAGE_CACHE_DIR.mkdir(parents=True, exist_ok=True)
2024-02-22 02:12:01 +00:00
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.state.ENGINE = IMAGE_GENERATION_ENGINE
app.state.ENABLED = ENABLE_IMAGE_GENERATION
2024-03-09 01:38:10 +00:00
2024-04-23 11:14:31 +00:00
app.state.OPENAI_API_BASE_URL = IMAGES_OPENAI_API_BASE_URL
app.state.OPENAI_API_KEY = IMAGES_OPENAI_API_KEY
2024-04-20 20:15:59 +00:00
app.state.MODEL = IMAGE_GENERATION_MODEL
2024-03-09 01:38:10 +00:00
2024-02-22 02:12:01 +00:00
app.state.AUTOMATIC1111_BASE_URL = AUTOMATIC1111_BASE_URL
2024-03-23 22:38:59 +00:00
app.state.COMFYUI_BASE_URL = COMFYUI_BASE_URL
2024-03-09 01:38:10 +00:00
app.state.IMAGE_SIZE = IMAGE_SIZE
app.state.IMAGE_STEPS = IMAGE_STEPS
2024-02-22 02:12:01 +00:00
2024-03-09 01:38:10 +00:00
@app.get("/config")
async def get_config(request: Request, user=Depends(get_admin_user)):
return {"engine": app.state.ENGINE, "enabled": app.state.ENABLED}
2024-02-22 02:12:01 +00:00
2024-03-09 01:38:10 +00:00
class ConfigUpdateForm(BaseModel):
engine: str
enabled: bool
@app.post("/config/update")
async def update_config(form_data: ConfigUpdateForm, user=Depends(get_admin_user)):
app.state.ENGINE = form_data.engine
app.state.ENABLED = form_data.enabled
return {"engine": app.state.ENGINE, "enabled": app.state.ENABLED}
2024-02-22 02:12:01 +00:00
2024-03-23 22:38:59 +00:00
class EngineUrlUpdateForm(BaseModel):
AUTOMATIC1111_BASE_URL: Optional[str] = None
COMFYUI_BASE_URL: Optional[str] = None
2024-02-22 02:12:01 +00:00
@app.get("/url")
2024-03-23 22:38:59 +00:00
async def get_engine_url(user=Depends(get_admin_user)):
return {
"AUTOMATIC1111_BASE_URL": app.state.AUTOMATIC1111_BASE_URL,
"COMFYUI_BASE_URL": app.state.COMFYUI_BASE_URL,
}
2024-02-22 02:12:01 +00:00
@app.post("/url/update")
2024-03-23 22:38:59 +00:00
async def update_engine_url(
form_data: EngineUrlUpdateForm, user=Depends(get_admin_user)
2024-03-09 01:38:10 +00:00
):
2024-02-22 03:06:27 +00:00
2024-03-23 22:38:59 +00:00
if form_data.AUTOMATIC1111_BASE_URL == None:
2024-02-22 03:06:27 +00:00
app.state.AUTOMATIC1111_BASE_URL = AUTOMATIC1111_BASE_URL
else:
2024-03-23 22:38:59 +00:00
url = form_data.AUTOMATIC1111_BASE_URL.strip("/")
2024-03-09 01:38:10 +00:00
try:
r = requests.head(url)
app.state.AUTOMATIC1111_BASE_URL = url
except Exception as e:
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
2024-02-22 03:06:27 +00:00
2024-03-23 22:38:59 +00:00
if form_data.COMFYUI_BASE_URL == None:
app.state.COMFYUI_BASE_URL = COMFYUI_BASE_URL
else:
url = form_data.COMFYUI_BASE_URL.strip("/")
2024-03-24 00:01:13 +00:00
try:
r = requests.head(url)
app.state.COMFYUI_BASE_URL = url
except Exception as e:
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
2024-03-23 22:38:59 +00:00
2024-02-22 03:06:27 +00:00
return {
"AUTOMATIC1111_BASE_URL": app.state.AUTOMATIC1111_BASE_URL,
2024-03-23 22:38:59 +00:00
"COMFYUI_BASE_URL": app.state.COMFYUI_BASE_URL,
2024-02-22 03:06:27 +00:00
"status": True,
}
2024-02-22 02:12:01 +00:00
2024-04-23 11:14:31 +00:00
class OpenAIConfigUpdateForm(BaseModel):
url: str
2024-03-09 01:38:10 +00:00
key: str
2024-04-23 11:14:31 +00:00
@app.get("/openai/config")
async def get_openai_config(user=Depends(get_admin_user)):
return {
"OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL,
"OPENAI_API_KEY": app.state.OPENAI_API_KEY,
}
2024-03-09 01:38:10 +00:00
2024-04-23 11:14:31 +00:00
@app.post("/openai/config/update")
async def update_openai_config(
form_data: OpenAIConfigUpdateForm, user=Depends(get_admin_user)
2024-03-09 01:38:10 +00:00
):
if form_data.key == "":
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.API_KEY_NOT_FOUND)
2024-04-23 11:14:31 +00:00
app.state.OPENAI_API_BASE_URL = form_data.url
2024-03-09 01:38:10 +00:00
app.state.OPENAI_API_KEY = form_data.key
2024-04-23 11:14:31 +00:00
2024-03-09 01:38:10 +00:00
return {
"status": True,
2024-04-23 11:14:31 +00:00
"OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL,
"OPENAI_API_KEY": app.state.OPENAI_API_KEY,
2024-03-09 01:38:10 +00:00
}
2024-02-23 03:32:36 +00:00
class ImageSizeUpdateForm(BaseModel):
size: str
@app.get("/size")
async def get_image_size(user=Depends(get_admin_user)):
return {"IMAGE_SIZE": app.state.IMAGE_SIZE}
@app.post("/size/update")
async def update_image_size(
form_data: ImageSizeUpdateForm, user=Depends(get_admin_user)
):
pattern = r"^\d+x\d+$" # Regular expression pattern
if re.match(pattern, form_data.size):
app.state.IMAGE_SIZE = form_data.size
return {
"IMAGE_SIZE": app.state.IMAGE_SIZE,
"status": True,
}
else:
raise HTTPException(
status_code=400,
detail=ERROR_MESSAGES.INCORRECT_FORMAT(" (e.g., 512x512)."),
)
2024-02-25 02:08:35 +00:00
class ImageStepsUpdateForm(BaseModel):
steps: int
@app.get("/steps")
async def get_image_size(user=Depends(get_admin_user)):
return {"IMAGE_STEPS": app.state.IMAGE_STEPS}
@app.post("/steps/update")
async def update_image_size(
form_data: ImageStepsUpdateForm, user=Depends(get_admin_user)
):
if form_data.steps >= 0:
app.state.IMAGE_STEPS = form_data.steps
return {
"IMAGE_STEPS": app.state.IMAGE_STEPS,
"status": True,
}
else:
raise HTTPException(
status_code=400,
detail=ERROR_MESSAGES.INCORRECT_FORMAT(" (e.g., 50)."),
)
2024-02-23 03:32:36 +00:00
2024-02-22 02:12:01 +00:00
@app.get("/models")
def get_models(user=Depends(get_current_user)):
try:
2024-03-09 01:38:10 +00:00
if app.state.ENGINE == "openai":
return [
{"id": "dall-e-2", "name": "DALL·E 2"},
{"id": "dall-e-3", "name": "DALL·E 3"},
]
2024-03-23 22:38:59 +00:00
elif app.state.ENGINE == "comfyui":
r = requests.get(url=f"{app.state.COMFYUI_BASE_URL}/object_info")
info = r.json()
return list(
map(
lambda model: {"id": model, "name": model},
info["CheckpointLoaderSimple"]["input"]["required"]["ckpt_name"][0],
)
)
2024-03-09 01:38:10 +00:00
else:
r = requests.get(
url=f"{app.state.AUTOMATIC1111_BASE_URL}/sdapi/v1/sd-models"
)
models = r.json()
return list(
map(
lambda model: {"id": model["title"], "name": model["model_name"]},
models,
)
)
2024-02-22 02:12:01 +00:00
except Exception as e:
app.state.ENABLED = False
2024-02-25 02:08:35 +00:00
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
2024-02-22 02:12:01 +00:00
@app.get("/models/default")
async def get_default_model(user=Depends(get_admin_user)):
try:
2024-03-09 01:38:10 +00:00
if app.state.ENGINE == "openai":
return {"model": app.state.MODEL if app.state.MODEL else "dall-e-2"}
2024-03-24 00:01:13 +00:00
elif app.state.ENGINE == "comfyui":
return {"model": app.state.MODEL if app.state.MODEL else ""}
2024-03-09 01:38:10 +00:00
else:
r = requests.get(url=f"{app.state.AUTOMATIC1111_BASE_URL}/sdapi/v1/options")
options = r.json()
return {"model": options["sd_model_checkpoint"]}
2024-02-22 02:12:01 +00:00
except Exception as e:
app.state.ENABLED = False
2024-02-25 02:08:35 +00:00
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
2024-02-22 02:12:01 +00:00
class UpdateModelForm(BaseModel):
model: str
def set_model_handler(model: str):
2024-03-09 01:38:10 +00:00
if app.state.ENGINE == "openai":
app.state.MODEL = model
return app.state.MODEL
2024-03-24 00:01:13 +00:00
if app.state.ENGINE == "comfyui":
app.state.MODEL = model
return app.state.MODEL
2024-03-09 01:38:10 +00:00
else:
r = requests.get(url=f"{app.state.AUTOMATIC1111_BASE_URL}/sdapi/v1/options")
options = r.json()
if model != options["sd_model_checkpoint"]:
options["sd_model_checkpoint"] = model
r = requests.post(
url=f"{app.state.AUTOMATIC1111_BASE_URL}/sdapi/v1/options", json=options
)
2024-02-22 02:12:01 +00:00
2024-03-09 01:38:10 +00:00
return options
2024-02-22 02:12:01 +00:00
@app.post("/models/default/update")
def update_default_model(
form_data: UpdateModelForm,
user=Depends(get_current_user),
):
return set_model_handler(form_data.model)
class GenerateImageForm(BaseModel):
model: Optional[str] = None
prompt: str
n: int = 1
2024-03-09 03:54:47 +00:00
size: Optional[str] = None
2024-02-22 02:12:01 +00:00
negative_prompt: Optional[str] = None
2024-03-09 01:38:10 +00:00
def save_b64_image(b64_str):
image_id = str(uuid.uuid4())
file_path = IMAGE_CACHE_DIR.joinpath(f"{image_id}.png")
try:
# Split the base64 string to get the actual image data
img_data = base64.b64decode(b64_str)
# Write the image data to a file
with open(file_path, "wb") as f:
f.write(img_data)
return image_id
except Exception as e:
log.error(f"Error saving image: {e}")
2024-03-09 01:38:10 +00:00
return None
2024-03-24 00:01:13 +00:00
def save_url_image(url):
image_id = str(uuid.uuid4())
file_path = IMAGE_CACHE_DIR.joinpath(f"{image_id}.png")
try:
r = requests.get(url)
r.raise_for_status()
with open(file_path, "wb") as image_file:
image_file.write(r.content)
return image_id
except Exception as e:
log.exception(f"Error saving image: {e}")
2024-03-24 00:01:13 +00:00
return None
2024-02-22 02:12:01 +00:00
@app.post("/generations")
def generate_image(
form_data: GenerateImageForm,
user=Depends(get_current_user),
):
2024-03-24 00:01:13 +00:00
width, height = tuple(map(int, app.state.IMAGE_SIZE.split("x")))
2024-03-09 03:54:47 +00:00
r = None
2024-02-22 02:36:40 +00:00
try:
2024-03-09 01:38:10 +00:00
if app.state.ENGINE == "openai":
2024-02-22 02:36:40 +00:00
2024-03-09 01:38:10 +00:00
headers = {}
headers["Authorization"] = f"Bearer {app.state.OPENAI_API_KEY}"
headers["Content-Type"] = "application/json"
2024-02-22 02:36:40 +00:00
2024-03-09 01:38:10 +00:00
data = {
"model": app.state.MODEL if app.state.MODEL != "" else "dall-e-2",
"prompt": form_data.prompt,
"n": form_data.n,
2024-03-09 03:54:47 +00:00
"size": form_data.size if form_data.size else app.state.IMAGE_SIZE,
2024-03-09 01:38:10 +00:00
"response_format": "b64_json",
}
2024-03-12 20:35:30 +00:00
2024-03-09 01:38:10 +00:00
r = requests.post(
2024-04-20 20:15:59 +00:00
url=f"{app.state.OPENAI_API_BASE_URL}/images/generations",
2024-03-09 01:38:10 +00:00
json=data,
headers=headers,
)
2024-03-09 01:38:10 +00:00
r.raise_for_status()
res = r.json()
2024-02-22 02:12:01 +00:00
2024-03-09 01:38:10 +00:00
images = []
for image in res["data"]:
image_id = save_b64_image(image["b64_json"])
images.append({"url": f"/cache/image/generations/{image_id}.png"})
file_body_path = IMAGE_CACHE_DIR.joinpath(f"{image_id}.json")
with open(file_body_path, "w") as f:
json.dump(data, f)
return images
2024-03-24 00:01:13 +00:00
elif app.state.ENGINE == "comfyui":
data = {
"prompt": form_data.prompt,
"width": width,
"height": height,
"n": form_data.n,
}
if app.state.IMAGE_STEPS != None:
data["steps"] = app.state.IMAGE_STEPS
if form_data.negative_prompt != None:
data["negative_prompt"] = form_data.negative_prompt
data = ImageGenerationPayload(**data)
res = comfyui_generate_image(
app.state.MODEL,
data,
user.id,
app.state.COMFYUI_BASE_URL,
)
log.debug(f"res: {res}")
2024-03-24 00:01:13 +00:00
images = []
for image in res["data"]:
image_id = save_url_image(image["url"])
images.append({"url": f"/cache/image/generations/{image_id}.png"})
file_body_path = IMAGE_CACHE_DIR.joinpath(f"{image_id}.json")
with open(file_body_path, "w") as f:
json.dump(data.model_dump(exclude_none=True), f)
log.debug(f"images: {images}")
2024-03-24 00:01:13 +00:00
return images
2024-03-09 01:38:10 +00:00
else:
if form_data.model:
set_model_handler(form_data.model)
data = {
"prompt": form_data.prompt,
"batch_size": form_data.n,
"width": width,
"height": height,
}
if app.state.IMAGE_STEPS != None:
data["steps"] = app.state.IMAGE_STEPS
if form_data.negative_prompt != None:
data["negative_prompt"] = form_data.negative_prompt
r = requests.post(
url=f"{app.state.AUTOMATIC1111_BASE_URL}/sdapi/v1/txt2img",
json=data,
)
res = r.json()
log.debug(f"res: {res}")
2024-03-09 01:38:10 +00:00
images = []
for image in res["images"]:
image_id = save_b64_image(image)
images.append({"url": f"/cache/image/generations/{image_id}.png"})
file_body_path = IMAGE_CACHE_DIR.joinpath(f"{image_id}.json")
with open(file_body_path, "w") as f:
json.dump({**data, "info": res["info"]}, f)
return images
2024-02-22 02:36:40 +00:00
except Exception as e:
2024-03-12 20:35:30 +00:00
error = e
if r != None:
data = r.json()
if "error" in data:
error = data["error"]["message"]
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(error))