animatedvideo / app.py
KennyDevil1980's picture
Update app.py
7957c02 verified
import torch
import gradio as gr
from diffusers import DiffusionPipeline
# πŸ” Automatically detect GPU or CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
print(f"βœ… Running on device: {device}")
# βš™οΈ Try to load model safely
model_id = "akhaliq/veo3.1-fast" # replace with your own model if needed
try:
with gr.Progress(track_tqdm=True):
pipe = DiffusionPipeline.from_pretrained(
model_id,
torch_dtype=dtype,
).to(device)
print("βœ… Model loaded successfully!")
except Exception as e:
print("❌ Error loading model:", e)
pipe = None
# 🎬 Define your main function
def generate_video(image, prompt):
if pipe is None:
return "⚠️ Model could not be loaded. Please try again later."
if image is None or prompt.strip() == "":
return "⚠️ Please upload an image and enter a description."
try:
with gr.Progress(track_tqdm=True):
result = pipe(image=image, prompt=prompt)
video = result.get("video", None)
if video:
return video
else:
return "⚠️ No video was generated. Try a different prompt."
except Exception as e:
return f"❌ An error occurred while generating video: {str(e)}"
# πŸ–₯️ Gradio interface
demo = gr.Interface(
fn=generate_video,
inputs=[
gr.Image(type="pil", label="πŸ“Έ Upload Image"),
gr.Textbox(label="πŸ“ Prompt", placeholder="Describe the animation you want..."),
],
outputs=gr.Video(label="πŸŽ₯ Generated Video"),
title="✨ AI Image to Video Generator",
description="Upload an image and let AI create an animated video for you. Works on CPU and GPU!",
theme="gradio/soft",
allow_flagging="never",
)
# πŸš€ Launch safely
if __name__ == "__main__":
demo.launch()