Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
80413a76
Commit
80413a76
authored
Apr 30, 2024
by
Timothy J. Baek
Browse files
refac
parent
3321a1b9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
12 deletions
+11
-12
backend/apps/images/main.py
backend/apps/images/main.py
+11
-12
No files found.
backend/apps/images/main.py
View file @
80413a76
...
...
@@ -320,16 +320,19 @@ def save_b64_image(b64_str):
header
,
encoded
=
b64_str
.
split
(
","
,
1
)
mime_type
=
header
.
split
(
";"
)[
0
]
image_format
=
mimetypes
.
guess_extension
(
mime_type
)
img_data
=
base64
.
b64decode
(
encoded
)
image_id
=
str
(
uuid
.
uuid4
())
file_path
=
IMAGE_CACHE_DIR
/
f
"
{
image_id
}{
image_format
}
"
image_format
=
mimetypes
.
guess_extension
(
mime_type
)
image_filename
=
f
"
{
image_id
}{
image_format
}
"
file_path
=
IMAGE_CACHE_DIR
/
f
"
{
image_filename
}
"
with
open
(
file_path
,
"wb"
)
as
f
:
f
.
write
(
img_data
)
return
image_
id
,
image_format
return
image_
filename
except
Exception
as
e
:
log
.
exception
(
f
"Error saving image:
{
e
}
"
)
return
None
,
None
return
None
def
save_url_image
(
url
):
...
...
@@ -395,10 +398,8 @@ def generate_image(
images
=
[]
for
image
in
res
[
"data"
]:
image_id
,
image_format
=
save_b64_image
(
image
[
"b64_json"
])
images
.
append
(
{
"url"
:
f
"/cache/image/generations/
{
image_id
}{
image_format
}
"
}
)
image_filename
=
save_b64_image
(
image
[
"b64_json"
])
images
.
append
({
"url"
:
f
"/cache/image/generations/
{
image_filename
}
"
})
file_body_path
=
IMAGE_CACHE_DIR
.
joinpath
(
f
"
{
image_id
}
.json"
)
with
open
(
file_body_path
,
"w"
)
as
f
:
...
...
@@ -474,10 +475,8 @@ def generate_image(
images
=
[]
for
image
in
res
[
"images"
]:
image_id
,
image_format
=
save_b64_image
(
image
)
images
.
append
(
{
"url"
:
f
"/cache/image/generations/
{
image_id
}{
image_format
}
"
}
)
image_filename
=
save_b64_image
(
image
)
images
.
append
({
"url"
:
f
"/cache/image/generations/
{
image_filename
}
"
})
file_body_path
=
IMAGE_CACHE_DIR
.
joinpath
(
f
"
{
image_id
}
.json"
)
with
open
(
file_body_path
,
"w"
)
as
f
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment