mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-02 02:09:17 -05:00
enh/pref: convert markdown base64 images to urls
Co-Authored-By: Shirasawa <kaguyashirasawa@gmail.com>
This commit is contained in:
@@ -16,10 +16,15 @@ from open_webui.routers.files import upload_file_handler
|
||||
import mimetypes
|
||||
import base64
|
||||
import io
|
||||
import re
|
||||
|
||||
|
||||
BASE64_IMAGE_URL_PREFIX = re.compile(r"data:image/\w+;base64,", re.IGNORECASE)
|
||||
MARKDOWN_IMAGE_URL_PATTERN = re.compile(r"!\[(.*?)\]\((.+?)\)", re.IGNORECASE)
|
||||
|
||||
|
||||
def get_image_url_from_base64(request, base64_image_string, metadata, user):
|
||||
if "data:image/png;base64" in base64_image_string:
|
||||
if BASE64_IMAGE_URL_PREFIX.match(base64_image_string):
|
||||
image_url = ""
|
||||
# Extract base64 image data from the line
|
||||
image_data, content_type = get_image_data(base64_image_string)
|
||||
@@ -35,6 +40,19 @@ def get_image_url_from_base64(request, base64_image_string, metadata, user):
|
||||
return None
|
||||
|
||||
|
||||
def convert_markdown_base64_images(request, content: str, metadata, user):
|
||||
def replace(match):
|
||||
base64_string = match.group(2)
|
||||
MIN_REPLACEMENT_URL_LENGTH = 1024
|
||||
if len(base64_string) > MIN_REPLACEMENT_URL_LENGTH:
|
||||
url = get_image_url_from_base64(request, base64_string, metadata, user)
|
||||
if url:
|
||||
return f""
|
||||
return match.group(0)
|
||||
|
||||
return MARKDOWN_IMAGE_URL_PATTERN.sub(replace, content)
|
||||
|
||||
|
||||
def load_b64_audio_data(b64_str):
|
||||
try:
|
||||
if "," in b64_str:
|
||||
|
||||
Reference in New Issue
Block a user