im2vid / app.py
jblast94's picture
Update app.py
2b25d3c verified
import gradio as gr
import time
import os
# This is a placeholder function to simulate the backend processing.
# In a real application, this function would contain the logic for a deep learning model
# to perform character replacement. For this UI demo, we simulate a processing delay
# and simply return the original video.
def replace_character(source_image, target_video, progress=gr.Progress(track_tqdm=True)):
"""
Simulates the character replacement process in a video.
Args:
source_image (str): File path to the source character image.
target_video (str): File path to the target video.
progress (gr.Progress): Gradio progress tracker to show processing status.
Returns:
str: File path of the processed video. For this demo, it's the original video.
"""
if source_image is None:
raise gr.Error("Please upload a character reference image.")
if target_video is None:
raise gr.Error("Please upload a target video.")
# Simulate a multi-stage AI process
stages = [
"Initializing model...",
"Detecting faces and bodies in video...",
"Tracking primary character...",
"Generating character mask...",
"Swapping frames with reference image...",
"Rendering final video..."
]
for stage in progress.tqdm(stages, desc="Starting process"):
# Simulate work for each stage
time.sleep(2.5)
# In a real implementation, you would return the path to the newly created video file.
return target_video
# Define the Gradio interface using gr.Blocks for a custom layout
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# Header and description
gr.HTML(
"""
<div style="text-align: center; max-width: 800px; margin: 0 auto;">
<h1 style="font-weight: 900; font-size: 2.5rem;">🎭 AI Video Character Replacement</h1>
<p style="margin-top: 1rem; font-size: 1.1rem; color: #4B5563;">
Provide a reference image of a character and a target video. The AI will replace a character in the video with the one from your image.
<br>
<em>(Note: This is a UI demonstration. The backend simulates the process and returns the original video.)</em>
</p>
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #3B82F6; font-weight: 500;">
Built with anycoder
</a>
</div>
"""
)
# Main UI layout with inputs and outputs
with gr.Row(variant="panel", equal_height=True):
# Input column
with gr.Column(scale=1, min_width=300):
gr.Markdown("### 1. Inputs")
source_image = gr.Image(
type="filepath",
label="Character Reference Image",
info="Upload a clear image of the character you want to insert."
)
target_video = gr.Video(
label="Target Video",
info="Upload the video where you want to replace a character."
)
submit_btn = gr.Button("✨ Replace Character", variant="primary")
# Output column
with gr.Column(scale=1, min_width=300):
gr.Markdown("### 2. Result")
output_video = gr.Video(
label="Result Video",
interactive=False,
info="The processed video will appear here."
)
# Link the button to the processing function
submit_btn.click(
fn=replace_character,
inputs=[source_image, target_video],
outputs=output_video
)
# Launch the application
if __name__ == "__main__":
demo.launch()
```