mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-23 15:38:55 +01:00
Use Django management command to update images URL in DB to WebP
This provides Khoj server admins more control on migrating their S3 images to WebP format from PNG
This commit is contained in:
parent
7d8e8eb0cf
commit
a352940dfd
4 changed files with 40 additions and 10 deletions
0
src/khoj/database/management/__init__.py
Normal file
0
src/khoj/database/management/__init__.py
Normal file
0
src/khoj/database/management/commands/__init__.py
Normal file
0
src/khoj/database/management/commands/__init__.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
|
||||
from khoj.database.models import Conversation
|
||||
from khoj.utils.helpers import ImageIntentType
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Convert all images to WebP format or reverse."
|
||||
|
||||
def add_arguments(self, parser):
|
||||
# Add a new argument 'reverse' to the command
|
||||
parser.add_argument(
|
||||
"--reverse",
|
||||
action="store_true",
|
||||
help="Convert from WebP to PNG instead of PNG to WebP",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
updated_count = 0
|
||||
for conversation in Conversation.objects.all():
|
||||
conversation_updated = False
|
||||
for chat in conversation.conversation_log["chat"]:
|
||||
if chat["by"] == "khoj" and chat["intent"]["type"] == ImageIntentType.TEXT_TO_IMAGE2.value:
|
||||
if options["reverse"] and chat["message"].endswith(".webp"):
|
||||
# Convert WebP url to PNG url
|
||||
chat["message"] = chat["message"].replace(".webp", ".png")
|
||||
conversation_updated = True
|
||||
updated_count += 1
|
||||
elif chat["message"].endswith(".png"):
|
||||
# Convert PNG url to WebP url
|
||||
chat["message"] = chat["message"].replace(".png", ".webp")
|
||||
conversation_updated = True
|
||||
updated_count += 1
|
||||
if conversation_updated:
|
||||
conversation.save()
|
||||
|
||||
if updated_count > 0 and options["reverse"]:
|
||||
self.stdout.write(self.style.SUCCESS(f"Successfully converted {updated_count} WebP images to PNG format."))
|
||||
elif updated_count > 0:
|
||||
self.stdout.write(self.style.SUCCESS(f"Successfully converted {updated_count} PNG images to WebP format."))
|
|
@ -30,11 +30,6 @@ def convert_png_images_to_webp(apps, schema_editor):
|
|||
chat["intent"]["type"] = ImageIntentType.TEXT_TO_IMAGE_V3.value
|
||||
webp_image_io.close()
|
||||
|
||||
if chat["by"] == "khoj" and chat["intent"]["type"] == ImageIntentType.TEXT_TO_IMAGE2.value:
|
||||
print("❗️ Please MANUALLY update PNG images created by Khoj in your AWS S3 bucket to WebP format.")
|
||||
# Convert PNG url to WebP url
|
||||
chat["message"] = chat["message"].replace(".png", ".webp")
|
||||
|
||||
# Save the updated conversation history
|
||||
conversation.save()
|
||||
|
||||
|
@ -60,11 +55,6 @@ def convert_webp_images_to_png(apps, schema_editor):
|
|||
chat["intent"]["type"] = ImageIntentType.TEXT_TO_IMAGE.value
|
||||
webp_image_io.close()
|
||||
|
||||
if chat["by"] == "khoj" and chat["intent"]["type"] == ImageIntentType.TEXT_TO_IMAGE2.value:
|
||||
# Convert WebP url to PNG url
|
||||
print("❗️ Please MANUALLY update WebP images created by Khoj in your AWS S3 bucket to PNG format.")
|
||||
chat["message"] = chat["message"].replace(".webp", ".png")
|
||||
|
||||
# Save the updated conversation history
|
||||
conversation.save()
|
||||
|
||||
|
|
Loading…
Reference in a new issue