CharVid / demo.py
Rishi Desai
init dump
3d274c1
raw
history blame
1.77 kB
import gradio as gr
import os
import tempfile
import base64
from main import process
def process_image_and_generate_video(image, prompt):
# Create a temporary directory for intermediate files
with tempfile.TemporaryDirectory() as temp_dir:
# Save the uploaded image to a temporary file
temp_image_path = os.path.join(temp_dir, "input_image.png")
image.save(temp_image_path)
# Encode the image as base64 and create a data URL
with open(temp_image_path, "rb") as f:
encoded_image = base64.b64encode(f.read()).decode("utf-8")
data_url = f"data:image/png;base64,{encoded_image}"
# Process the image and generate video
# The process function now handles saving to numbered directories
result, generated_image_path, video_path = process(data_url, prompt, temp_dir)
if result and video_path:
return video_path
else:
return None
# Create the Gradio interface
with gr.Blocks(title="Character Video Generation") as demo:
gr.Markdown("# Character Video Generation")
gr.Markdown("""
* Upload a high-quality image of a person
* Enter a prompt to generate a video
""")
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Upload Reference Image")
prompt = gr.Textbox(label="Enter your prompt")
generate_btn = gr.Button("Generate")
with gr.Column():
output_video = gr.Video(label="Generated Video")
generate_btn.click(
fn=process_image_and_generate_video,
inputs=[input_image, prompt],
outputs=[output_video]
)
if __name__ == "__main__":
demo.launch(share=True)