diff --git a/iz_helpers/run.py b/iz_helpers/run.py index d224af5..5b2d8c0 100644 --- a/iz_helpers/run.py +++ b/iz_helpers/run.py @@ -3,6 +3,7 @@ from PIL import Image, ImageFilter, ImageDraw from modules.ui import plaintext_to_html import modules.shared as shared +import uuid from modules.paths_internal import script_path from .helpers import ( fix_env_Path_ffprobe, @@ -65,7 +66,17 @@ def crop_fethear_ellipse(image, feather_margin=30, width_offset=0, height_offset res.paste(cropped_image, paste_pos) return res - +progress = { + "current_step": 0, + "total_steps": 0, + "percentage": 0, +} + +def update_progress(current_step, total_steps): + global progress + progress["current_step"] = current_step + progress["total_steps"] = total_steps + progress["percentage"] = (current_step / total_steps) * 100 def outpaint_steps( width, @@ -103,6 +114,7 @@ def outpaint_steps( + str(seed) ) print(print_out) + update_progress(i + 1, outpaint_steps) current_image = main_frames[-1] current_image = shrink_and_paste_on_blank( current_image, mask_width, mask_height @@ -493,9 +505,9 @@ def create_zoom_single( ) frames2Collect(all_frames, out_config) - + video_filename = os.path.join("outputs", "videos", f"{uuid.uuid4()}.mp4") write_video( - out_config["video_filename"], + video_filename, all_frames, video_frame_rate, video_zoom_mode, @@ -504,7 +516,7 @@ def create_zoom_single( ) print("Video saved in: " + os.path.join(script_path, out_config["video_filename"])) return ( - out_config["video_filename"], + video_filename, main_frames, processed.js(), plaintext_to_html(processed.info), diff --git a/scripts/iz_api.py b/scripts/iz_api.py new file mode 100644 index 0000000..e52c617 --- /dev/null +++ b/scripts/iz_api.py @@ -0,0 +1,92 @@ +from fastapi import FastAPI, Body, HTTPException +from fastapi.responses import FileResponse +from iz_helpers.run import create_zoom, progress # Import progress variable +from PIL import Image +import gradio as gr +import os +import logging +import io +import base64 +import asyncio + +def base64_to_image(base64_str): + image_bytes = base64.b64decode(base64_str) + image = Image.open(io.BytesIO(image_bytes)) + return image + +def infinite_zoom_api(_: gr.Blocks, app: FastAPI): + @app.post("/infinite_zoom/create") + async def infinite_zoom( + common_prompt_pre: str = Body(...), + prompts_array: list = Body(...), + common_prompt_suf: str = Body(...), + negative_prompt: str = Body(...), + num_outpainting_steps: int = Body(...), + guidance_scale: float = Body(...), + num_inference_steps: int = Body(...), + custom_init_image: str = Body(...), + custom_exit_image: str = Body(...), + video_frame_rate: int = Body(...), + video_zoom_mode: str = Body(...), + video_start_frame_dupe_amount: int = Body(...), + video_last_frame_dupe_amount: int = Body(...), + inpainting_mask_blur: int = Body(...), + inpainting_fill_mode: str = Body(...), + zoom_speed: float = Body(...), + seed: int = Body(...), + outputsizeW: int = Body(...), + outputsizeH: int = Body(...), + batchcount: int = Body(...), + sampler: str = Body(...), + upscale_do: bool = Body(...), + upscaler_name: str = Body(...), + upscale_by: float = Body(...) + ): + loop = asyncio.get_event_loop() + video_filename, main_frames, js, info, _ = await loop.run_in_executor( + None, + create_zoom, + common_prompt_pre, + prompts_array, + common_prompt_suf, + negative_prompt, + num_outpainting_steps, + guidance_scale, + num_inference_steps, + base64_to_image(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, + batchcount, + sampler, + upscale_do, + upscaler_name, + upscale_by + ) + return {"video_name": video_filename} + + @app.get("/infinite_zoom/download_video/{video_filename}") + async def download_video(video_filename: str): + video_path = os.path.join('outputs', 'videos', video_filename) + if os.path.exists(video_path): + return FileResponse(video_path) + else: + raise HTTPException(status_code=404, detail="Video not found") + + @app.get("/infinite_zoom/progress") + async def get_progress(): + return progress + +try: + import modules.script_callbacks as script_callbacks + script_callbacks.on_app_started(infinite_zoom_api) +except Exception as e: + logging.exception("Failed to call on_app_started in script_callbacks module") \ No newline at end of file