Skip to content

Instantly share code, notes, and snippets.

@liuerfire
Created July 6, 2023 09:10
Show Gist options
  • Save liuerfire/af1f547cd4022cd104bbce07e6eef219 to your computer and use it in GitHub Desktop.
Save liuerfire/af1f547cd4022cd104bbce07e6eef219 to your computer and use it in GitHub Desktop.
infinite-zoom.patch
diff --git a/scripts/infinite-zoom.py b/scripts/infinite-zoom.py
index 60751b0..35cdc78 100644
--- a/scripts/infinite-zoom.py
+++ b/scripts/infinite-zoom.py
@@ -1,5 +1,89 @@
+import base64
+import io
+import random
+
+import gradio as gr
+from fastapi import FastAPI
+from fastapi import Request
from modules import script_callbacks
-from iz_helpers.ui import on_ui_tabs
+
+from iz_helpers.run import create_zoom_single
from iz_helpers.settings import on_ui_settings
+from iz_helpers.ui import on_ui_tabs
+
+
+def encode_pil_to_base64(image):
+ with io.BytesIO() as output_bytes:
+ image.save(output_bytes, format="PNG")
+ bytes_data = output_bytes.getvalue()
+ return base64.b64encode(bytes_data).decode()
+
+
+def infinite_zoom_api(_: gr.Blocks, app: FastAPI):
+ @app.post("/infinite_zoom/txt2video")
+ async def txt2video(req: Request):
+ params = await req.json()
+ prompts_array = params.get('prompts', [])
+ negative_prompt = params.get('negative_prompt')
+ guidance_scale = params.get('cfg_scale', 8)
+ num_outpainting_steps = params.get('video_length', 5)
+ seed = params.get('seed', -1)
+ outputsizeH = params.get('height', 512)
+ outputsizeW = params.get('width', 512)
+ video_zoom_mode = params.get('zoom_mode', 0)
+
+ common_prompt_suf = ''
+ common_prompt_pre = ''
+ sampler = 'DDIM'
+ upscale_do = False
+ upscaler_name = None
+ upscale_by = 2
+ num_inference_steps = 35
+ custom_init_image = None
+ custom_exit_image = None
+ video_frame_rate = 30
+ video_start_frame_dupe_amount = 0
+ video_last_frame_dupe_amount = 0
+ inpainting_mask_blur = 48
+ inpainting_fill_mode = 2 # latent noise
+ zoom_speed = 1
+
+ result = create_zoom_single(
+ common_prompt_pre,
+ prompts_array,
+ common_prompt_suf,
+ negative_prompt,
+ num_outpainting_steps,
+ guidance_scale,
+ num_inference_steps,
+ custom_init_image,
+ custom_exit_image,
+ video_frame_rate,
+ video_zoom_mode,
+ video_start_frame_dupe_amount,
+ video_last_frame_dupe_amount,
+ inpainting_mask_blur,
+ inpainting_fill_mode,
+ zoom_speed,
+ seed,
+ outputsizeW,
+ outputsizeH,
+ sampler,
+ upscale_do,
+ upscaler_name,
+ upscale_by,
+ 1,
+ 0,
+ 0,
+ None,
+ )
+ video_path, main_frames, _, _, _ = result
+ with open(video_path, 'rb') as f:
+ video = base64.b64encode(f.read()).decode()
+ cover_img = random.choice(main_frames)
+ return {'video': video, 'cover_img': encode_pil_to_base64(cover_img)}
+
+
+script_callbacks.on_app_started(infinite_zoom_api)
script_callbacks.on_ui_tabs(on_ui_tabs)
-script_callbacks.on_ui_settings(on_ui_settings)
\ No newline at end of file
+script_callbacks.on_ui_settings(on_ui_settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment