refac
This commit is contained in:
1 parent
01e88c6ac2
commit
446cc0ac60
3 files changed
+75
-17
No files matched your search
@@ -17,6 +17,8 @@ from fastapi.responses import FileResponse
|
|||||||
from open_webui.config import CACHE_DIR
|
from open_webui.config import CACHE_DIR
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
from open_webui.env import ENABLE_FORWARD_USER_INFO_HEADERS
|
from open_webui.env import ENABLE_FORWARD_USER_INFO_HEADERS
|
||||||
|
|
||||||
|
from open_webui.models.chats import Chats
|
||||||
from open_webui.routers.files import upload_file_handler, get_file_content_by_id
|
from open_webui.routers.files import upload_file_handler, get_file_content_by_id
|
||||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||||
from open_webui.utils.headers import include_user_info_headers
|
from open_webui.utils.headers import include_user_info_headers
|
||||||
@@ -510,14 +512,29 @@ def upload_image(request, image_data, content_type, metadata, user):
|
|||||||
process=False,
|
process=False,
|
||||||
user=user,
|
user=user,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if file_item and file_item.id:
|
||||||
|
# If chat_id and message_id are provided in metadata, link the file to the chat message
|
||||||
|
chat_id = metadata.get("chat_id")
|
||||||
|
message_id = metadata.get("message_id")
|
||||||
|
|
||||||
|
if chat_id and message_id:
|
||||||
|
Chats.insert_chat_files(
|
||||||
|
chat_id=chat_id,
|
||||||
|
message_id=message_id,
|
||||||
|
file_ids=[file_item.id],
|
||||||
|
user_id=user.id,
|
||||||
|
)
|
||||||
|
|
||||||
url = request.app.url_path_for("get_file_content_by_id", id=file_item.id)
|
url = request.app.url_path_for("get_file_content_by_id", id=file_item.id)
|
||||||
return url
|
return file_item, url
|
||||||
|
|
||||||
|
|
||||||
@router.post("/generations")
|
@router.post("/generations")
|
||||||
async def image_generations(
|
async def image_generations(
|
||||||
request: Request,
|
request: Request,
|
||||||
form_data: CreateImageForm,
|
form_data: CreateImageForm,
|
||||||
|
metadata: Optional[dict] = None,
|
||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
):
|
):
|
||||||
# if IMAGE_SIZE = 'auto', default WidthxHeight to the 512x512 default
|
# if IMAGE_SIZE = 'auto', default WidthxHeight to the 512x512 default
|
||||||
@@ -535,6 +552,9 @@ async def image_generations(
|
|||||||
size = form_data.size
|
size = form_data.size
|
||||||
|
|
||||||
width, height = tuple(map(int, size.split("x")))
|
width, height = tuple(map(int, size.split("x")))
|
||||||
|
|
||||||
|
metadata = metadata or {}
|
||||||
|
|
||||||
model = get_image_model(request)
|
model = get_image_model(request)
|
||||||
|
|
||||||
r = None
|
r = None
|
||||||
@@ -595,7 +615,9 @@ async def image_generations(
|
|||||||
else:
|
else:
|
||||||
image_data, content_type = get_image_data(image["b64_json"])
|
image_data, content_type = get_image_data(image["b64_json"])
|
||||||
|
|
||||||
url = upload_image(request, image_data, content_type, data, user)
|
_, url = upload_image(
|
||||||
|
request, image_data, content_type, {**data, **metadata}, user
|
||||||
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
return images
|
return images
|
||||||
|
|
||||||
@@ -645,7 +667,9 @@ async def image_generations(
|
|||||||
image_data, content_type = get_image_data(
|
image_data, content_type = get_image_data(
|
||||||
image["bytesBase64Encoded"]
|
image["bytesBase64Encoded"]
|
||||||
)
|
)
|
||||||
url = upload_image(request, image_data, content_type, data, user)
|
_, url = upload_image(
|
||||||
|
request, image_data, content_type, {**data, **metadata}, user
|
||||||
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
elif model.endswith(":generateContent"):
|
elif model.endswith(":generateContent"):
|
||||||
for image in res["candidates"]:
|
for image in res["candidates"]:
|
||||||
@@ -654,8 +678,12 @@ async def image_generations(
|
|||||||
image_data, content_type = get_image_data(
|
image_data, content_type = get_image_data(
|
||||||
part["inlineData"]["data"]
|
part["inlineData"]["data"]
|
||||||
)
|
)
|
||||||
url = upload_image(
|
_, url = upload_image(
|
||||||
request, image_data, content_type, data, user
|
request,
|
||||||
|
image_data,
|
||||||
|
content_type,
|
||||||
|
{**data, **metadata},
|
||||||
|
user,
|
||||||
)
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
|
|
||||||
@@ -705,11 +733,11 @@ async def image_generations(
|
|||||||
}
|
}
|
||||||
|
|
||||||
image_data, content_type = get_image_data(image["url"], headers)
|
image_data, content_type = get_image_data(image["url"], headers)
|
||||||
url = upload_image(
|
_, url = upload_image(
|
||||||
request,
|
request,
|
||||||
image_data,
|
image_data,
|
||||||
content_type,
|
content_type,
|
||||||
form_data.model_dump(exclude_none=True),
|
{**form_data.model_dump(exclude_none=True), **metadata},
|
||||||
user,
|
user,
|
||||||
)
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
@@ -752,11 +780,11 @@ async def image_generations(
|
|||||||
|
|
||||||
for image in res["images"]:
|
for image in res["images"]:
|
||||||
image_data, content_type = get_image_data(image)
|
image_data, content_type = get_image_data(image)
|
||||||
url = upload_image(
|
_, url = upload_image(
|
||||||
request,
|
request,
|
||||||
image_data,
|
image_data,
|
||||||
content_type,
|
content_type,
|
||||||
{**data, "info": res["info"]},
|
{**data, "info": res["info"], **metadata},
|
||||||
user,
|
user,
|
||||||
)
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
@@ -783,10 +811,13 @@ class EditImageForm(BaseModel):
|
|||||||
async def image_edits(
|
async def image_edits(
|
||||||
request: Request,
|
request: Request,
|
||||||
form_data: EditImageForm,
|
form_data: EditImageForm,
|
||||||
|
metadata: Optional[dict] = None,
|
||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
):
|
):
|
||||||
size = None
|
size = None
|
||||||
width, height = None, None
|
width, height = None, None
|
||||||
|
metadata = metadata or {}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
request.app.state.config.IMAGE_EDIT_SIZE
|
request.app.state.config.IMAGE_EDIT_SIZE
|
||||||
and "x" in request.app.state.config.IMAGE_EDIT_SIZE
|
and "x" in request.app.state.config.IMAGE_EDIT_SIZE
|
||||||
@@ -904,7 +935,9 @@ async def image_edits(
|
|||||||
else:
|
else:
|
||||||
image_data, content_type = get_image_data(image["b64_json"])
|
image_data, content_type = get_image_data(image["b64_json"])
|
||||||
|
|
||||||
url = upload_image(request, image_data, content_type, data, user)
|
_, url = upload_image(
|
||||||
|
request, image_data, content_type, {**data, **metadata}, user
|
||||||
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
return images
|
return images
|
||||||
|
|
||||||
@@ -957,8 +990,12 @@ async def image_edits(
|
|||||||
image_data, content_type = get_image_data(
|
image_data, content_type = get_image_data(
|
||||||
part["inlineData"]["data"]
|
part["inlineData"]["data"]
|
||||||
)
|
)
|
||||||
url = upload_image(
|
_, url = upload_image(
|
||||||
request, image_data, content_type, data, user
|
request,
|
||||||
|
image_data,
|
||||||
|
content_type,
|
||||||
|
{**data, **metadata},
|
||||||
|
user,
|
||||||
)
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
|
|
||||||
@@ -1035,11 +1072,11 @@ async def image_edits(
|
|||||||
}
|
}
|
||||||
|
|
||||||
image_data, content_type = get_image_data(image_url, headers)
|
image_data, content_type = get_image_data(image_url, headers)
|
||||||
url = upload_image(
|
_, url = upload_image(
|
||||||
request,
|
request,
|
||||||
image_data,
|
image_data,
|
||||||
content_type,
|
content_type,
|
||||||
form_data.model_dump(exclude_none=True),
|
{**form_data.model_dump(exclude_none=True), **metadata},
|
||||||
user,
|
user,
|
||||||
)
|
)
|
||||||
images.append({"url": url})
|
images.append({"url": url})
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ from typing import Optional
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from open_webui.storage.provider import Storage
|
from open_webui.storage.provider import Storage
|
||||||
|
|
||||||
|
from open_webui.models.chats import Chats
|
||||||
from open_webui.models.files import Files
|
from open_webui.models.files import Files
|
||||||
from open_webui.routers.files import upload_file_handler
|
from open_webui.routers.files import upload_file_handler
|
||||||
|
|
||||||
@@ -65,13 +67,14 @@ def get_image_url_from_base64(request, base64_image_string, metadata, user):
|
|||||||
# Extract base64 image data from the line
|
# Extract base64 image data from the line
|
||||||
image_data, content_type = get_image_data(base64_image_string)
|
image_data, content_type = get_image_data(base64_image_string)
|
||||||
if image_data is not None:
|
if image_data is not None:
|
||||||
image_url = upload_image(
|
_, image_url = upload_image(
|
||||||
request,
|
request,
|
||||||
image_data,
|
image_data,
|
||||||
content_type,
|
content_type,
|
||||||
metadata,
|
metadata,
|
||||||
user,
|
user,
|
||||||
)
|
)
|
||||||
|
|
||||||
return image_url
|
return image_url
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -799,6 +799,10 @@ async def chat_image_generation_handler(
|
|||||||
images = await image_edits(
|
images = await image_edits(
|
||||||
request=request,
|
request=request,
|
||||||
form_data=EditImageForm(**{"prompt": prompt, "image": input_images}),
|
form_data=EditImageForm(**{"prompt": prompt, "image": input_images}),
|
||||||
|
metadata={
|
||||||
|
"chat_id": metadata.get("chat_id", None),
|
||||||
|
"message_id": metadata.get("message_id", None),
|
||||||
|
},
|
||||||
user=user,
|
user=user,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -824,7 +828,7 @@ async def chat_image_generation_handler(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
system_message_content = "<context>The requested image has been created and is now being shown to the user. Let them know that it has been generated.</context>"
|
system_message_content = "<context>The requested image has been edited and created and is now being shown to the user. Let them know that it has been generated.</context>"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.debug(e)
|
log.debug(e)
|
||||||
|
|
||||||
@@ -883,6 +887,10 @@ async def chat_image_generation_handler(
|
|||||||
images = await image_generations(
|
images = await image_generations(
|
||||||
request=request,
|
request=request,
|
||||||
form_data=CreateImageForm(**{"prompt": prompt}),
|
form_data=CreateImageForm(**{"prompt": prompt}),
|
||||||
|
metadata={
|
||||||
|
"chat_id": metadata.get("chat_id", None),
|
||||||
|
"message_id": metadata.get("message_id", None),
|
||||||
|
},
|
||||||
user=user,
|
user=user,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -2737,7 +2745,17 @@ async def process_chat_response(
|
|||||||
|
|
||||||
if ENABLE_CHAT_RESPONSE_BASE64_IMAGE_URL_CONVERSION:
|
if ENABLE_CHAT_RESPONSE_BASE64_IMAGE_URL_CONVERSION:
|
||||||
value = convert_markdown_base64_images(
|
value = convert_markdown_base64_images(
|
||||||
request, value, metadata, user
|
request,
|
||||||
|
value,
|
||||||
|
{
|
||||||
|
"chat_id": metadata.get(
|
||||||
|
"chat_id", None
|
||||||
|
),
|
||||||
|
"message_id": metadata.get(
|
||||||
|
"message_id", None
|
||||||
|
),
|
||||||
|
},
|
||||||
|
user,
|
||||||
)
|
)
|
||||||
|
|
||||||
content = f"{content}{value}"
|
content = f"{content}{value}"
|
||||||
|
|||||||
Reference in new issue
Block a user