File size: 2,405 Bytes
508288d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
from diffusers import DiffusionPipeline
import gradio as gr
from PIL import Image as PILImage
import numpy as np
from diffusers.utils import export_to_gif

# Replace this with your new Hugging Face repository ID
huggingface_repo_id = "Nehal721/my-smart-kitchen-video-model"

# Model ko load karein (ek baar jab app shuru ho)
try:
    pipe = DiffusionPipeline.from_pretrained(huggingface_repo_id, torch_dtype=torch.float16)
    if torch.cuda.is_available():
        pipe.to("cuda")
        print("Model loaded on GPU.")
    else:
        pipe.to("cpu")
        print("Model loaded on CPU.")
    
    # xformers memory optimization (optional)
    try:
        if torch.cuda.is_available():
            pipe.enable_xformers_memory_efficient_attention()
            print("xFormers memory efficient attention enabled.")
    except Exception as e:
        print(f"xFormers not enabled: {e}")

except Exception as e:
    print(f"Error loading model: {e}")
    pipe = None

def generate_video_from_prompt(prompt):
    if pipe is None:
        return "Error: Model could not be loaded."
    
    try:
        # Video frames generate karein
        video_frames = pipe(prompt, num_inference_steps=40, height=256, width=448, num_frames=24).frames
        
        # Frames ko GIF mein convert karein
        gif_path = "output.gif"
        
        frames_for_gif = []
        if isinstance(video_frames, np.ndarray) and video_frames.ndim == 5:
            video_frames = video_frames.squeeze(0)
        
        if isinstance(video_frames, np.ndarray):
            for frame_np in video_frames:
                if frame_np.dtype != np.uint8:
                    frame_np = (frame_np * 255).astype(np.uint8)
                frames_for_gif.append(PILImage.fromarray(frame_np, 'RGB'))
        
        if not frames_for_gif:
            raise ValueError("No valid frames for GIF conversion.")
        
        export_to_gif(frames_for_gif, gif_path)
        
        return gif_path # GIF file ka path return karein
    
    except Exception as e:
        return f"An error occurred during video generation: {e}"

# Gradio app ka interface banayein
iface = gr.Interface(
    fn=generate_video_from_prompt,
    inputs="text",
    outputs="image",
    title="Smart Kitchen Video Generator",
    description="Text prompts se cooking videos generate karein."
)

# App ko run karein
iface.launch(share=False)