Spaces:
Sleeping
Sleeping
Update app.py
Browse files1232345678910111213141516171819202122232425266782829303132910111213141516171819202122232425262728293031323334353637383940414243444546363738394748495041424344454651525354555657585960615062import gradio as grfrom moviepy.editor import ImageClipimport tempfilefrom huggingface_hub import InferenceClientfrom PIL import Imageimport os
def image_to_video(image, duration, fps): if image is None: return None
# Create a temporary file for the output video tmp_dir = tempfile.mkdtemp() output_path = os.path.join(tmp_dir, "output.mp4")
# Create a video clip from the image clip = ImageClip(image).set_duration(duration) clip = clip.set_fps(fps)
# Write the video file clip.write_videofile( output_path, codec="libx264", audio=False, fps=fps, verbose=False, logger=None )# Connecting to the high-performance Stable Video Diffusion clusterclient = InferenceClient()
return output_path
with gr.Blocks() as demo: gr.Markdown("# RustyAI Image β Video π₯") gr.Markdown("Upload an image and RustyAI will turn it into a video.")def transform_image_to_video(uploaded_image): # Check if the user actually uploaded a file if uploaded_image is None: return None print("RustyAI Engine: Converting image array to PIL format...") # Convert the raw image data into a format Python can process input_img = Image.fromarray(uploaded_image) print("RustyAI Engine: Sending file to external GPU cluster for animation...") try: # Call the high-speed serverless endpoint using Stable Video Diffusion video_bytes = client.image_to_video( image=input_img, model="stabilityai/stable-video-diffusion-img2vid-xt" ) # Define the path where the finished video clip will save output_file_path = "rusty_ai_animation.mp4" # Write the video bytes into an actual playable MP4 file with open(output_file_path, "wb") as f: f.write(video_bytes) print("RustyAI Engine: Video successfully baked!") return output_file_path except Exception as e: print(f"Server Error caught during animation: {e}") return None
# π οΈ Designing the Clean RustyAI Studio Interfacewith gr.Blocks(title="RustyAI Video Studio") as demo: gr.Markdown("# π¬ RustyAI Image-to-Video Machine") gr.Markdown("Drop any image format (`.webp`, `.png`, `.jpg`) into the slot and turn it into an animated video clip instantly.") with gr.Row(): with gr.Column(): img_input = gr.Image(type="numpy", label="Input image") duration_input = gr.Slider(1, 15, value=5, step=1, label="Duration (seconds)") fps_input = gr.Slider(5, 60, value=24, step=1, label="FPS") btn = gr.Button("Generate Video") image_slot = gr.Image(label="Upload Your Static Image Here") generate_button = gr.Button("Bake Video File π", variant="primary") with gr.Column(): video_output = gr.Video(label="Output video")
btn.click( fn=image_to_video, inputs=[img_input, duration_input, fps_input], outputs=video_output video_slot = gr.Video(label="Generated MP4 Output") # Connect the button directly to our new Python processing function generate_button.click( fn=transform_image_to_video, inputs=image_slot, outputs=video_slot )
# Launch the local server web appif __name__ == "__main__": demo.launch() demo.launch()app.py+51-39