Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +28 -17
- requirements.txt +4 -3
app.py
CHANGED
|
@@ -1,25 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from torch import autocast
|
| 4 |
-
from diffusers import StableDiffusionPipeline,
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from moviepy.editor import ImageSequenceClip
|
| 8 |
import os
|
| 9 |
import tempfile
|
| 10 |
-
import random
|
| 11 |
from tqdm import tqdm
|
| 12 |
|
| 13 |
def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p", motion_scale=30):
|
| 14 |
try:
|
| 15 |
-
# تهيئة نموذج Stable Diffusion
|
| 16 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 17 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
model_id,
|
| 19 |
torch_dtype=torch.float16,
|
| 20 |
safety_checker=None
|
| 21 |
)
|
| 22 |
-
pipe.scheduler =
|
|
|
|
| 23 |
pipe = pipe.to("cuda")
|
| 24 |
|
| 25 |
# إنشاء مجلد مؤقت لحفظ الصور
|
|
@@ -40,8 +40,11 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
|
|
| 40 |
|
| 41 |
# توليد الإطار الأول
|
| 42 |
with autocast("cuda"):
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
device="cuda",
|
| 46 |
dtype=torch.float16
|
| 47 |
)
|
|
@@ -49,9 +52,10 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
|
|
| 49 |
# توليد الصورة الأولى
|
| 50 |
image = pipe(
|
| 51 |
prompt=text_prompt,
|
| 52 |
-
latents=
|
| 53 |
-
num_inference_steps=
|
| 54 |
-
guidance_scale=7.5
|
|
|
|
| 55 |
).images[0]
|
| 56 |
|
| 57 |
frames.append(np.array(image.resize(size)))
|
|
@@ -59,17 +63,18 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
|
|
| 59 |
# توليد الإطارات المتتالية مع حركة تدريجية
|
| 60 |
for i in tqdm(range(1, num_frames)):
|
| 61 |
with autocast("cuda"):
|
| 62 |
-
# إضافة اضطراب
|
| 63 |
-
|
| 64 |
-
noise = torch.randn_like(
|
| 65 |
-
current_latents =
|
| 66 |
|
| 67 |
# توليد الإطار الجديد
|
| 68 |
image = pipe(
|
| 69 |
prompt=text_prompt,
|
| 70 |
latents=current_latents,
|
| 71 |
-
num_inference_steps=
|
| 72 |
-
guidance_scale=7.5
|
|
|
|
| 73 |
).images[0]
|
| 74 |
|
| 75 |
frames.append(np.array(image.resize(size)))
|
|
@@ -127,11 +132,17 @@ iface = gr.Interface(
|
|
| 127 |
gr.Textbox(label="وصف المشهد", placeholder="اكتب وصفاً للمشهد المتحرك الذي تريد إنشاءه..."),
|
| 128 |
gr.Slider(minimum=5, maximum=30, value=10, step=5, label="مدة الفيديو (بالثواني)"),
|
| 129 |
gr.Radio(["480p", "720p", "1080p"], label="دقة الفيديو", value="480p"),
|
| 130 |
-
gr.Slider(minimum=10, maximum=
|
| 131 |
],
|
| 132 |
outputs=gr.Video(label="الفيديو المتحرك المُنشأ"),
|
| 133 |
title="مولد الفيديو المتحرك بالذكاء الاصطناعي",
|
| 134 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
theme="huggingface",
|
| 136 |
cache_examples=False
|
| 137 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from torch import autocast
|
| 4 |
+
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from moviepy.editor import ImageSequenceClip
|
| 8 |
import os
|
| 9 |
import tempfile
|
|
|
|
| 10 |
from tqdm import tqdm
|
| 11 |
|
| 12 |
def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p", motion_scale=30):
|
| 13 |
try:
|
| 14 |
+
# تهيئة نموذج Stable Diffusion
|
| 15 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 16 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 17 |
model_id,
|
| 18 |
torch_dtype=torch.float16,
|
| 19 |
safety_checker=None
|
| 20 |
)
|
| 21 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 22 |
+
pipe.enable_attention_slicing()
|
| 23 |
pipe = pipe.to("cuda")
|
| 24 |
|
| 25 |
# إنشاء مجلد مؤقت لحفظ الصور
|
|
|
|
| 40 |
|
| 41 |
# توليد الإطار الأول
|
| 42 |
with autocast("cuda"):
|
| 43 |
+
# إنشاء الكمون الأولي
|
| 44 |
+
generator = torch.Generator(device="cuda").manual_seed(42)
|
| 45 |
+
latents = torch.randn(
|
| 46 |
+
(1, pipe.unet.config.in_channels, 64, 64),
|
| 47 |
+
generator=generator,
|
| 48 |
device="cuda",
|
| 49 |
dtype=torch.float16
|
| 50 |
)
|
|
|
|
| 52 |
# توليد الصورة الأولى
|
| 53 |
image = pipe(
|
| 54 |
prompt=text_prompt,
|
| 55 |
+
latents=latents,
|
| 56 |
+
num_inference_steps=30,
|
| 57 |
+
guidance_scale=7.5,
|
| 58 |
+
generator=generator
|
| 59 |
).images[0]
|
| 60 |
|
| 61 |
frames.append(np.array(image.resize(size)))
|
|
|
|
| 63 |
# توليد الإطارات المتتالية مع حركة تدريجية
|
| 64 |
for i in tqdm(range(1, num_frames)):
|
| 65 |
with autocast("cuda"):
|
| 66 |
+
# إضافة اضطراب تدريجي للكمون
|
| 67 |
+
time_embed = torch.tensor([i / num_frames]).to("cuda", dtype=torch.float16)
|
| 68 |
+
noise = torch.randn_like(latents) * (motion_scale / 100)
|
| 69 |
+
current_latents = latents + noise * time_embed.view(-1, 1, 1, 1)
|
| 70 |
|
| 71 |
# توليد الإطار الجديد
|
| 72 |
image = pipe(
|
| 73 |
prompt=text_prompt,
|
| 74 |
latents=current_latents,
|
| 75 |
+
num_inference_steps=30,
|
| 76 |
+
guidance_scale=7.5,
|
| 77 |
+
generator=generator
|
| 78 |
).images[0]
|
| 79 |
|
| 80 |
frames.append(np.array(image.resize(size)))
|
|
|
|
| 132 |
gr.Textbox(label="وصف المشهد", placeholder="اكتب وصفاً للمشهد المتحرك الذي تريد إنشاءه..."),
|
| 133 |
gr.Slider(minimum=5, maximum=30, value=10, step=5, label="مدة الفيديو (بالثواني)"),
|
| 134 |
gr.Radio(["480p", "720p", "1080p"], label="دقة الفيديو", value="480p"),
|
| 135 |
+
gr.Slider(minimum=10, maximum=100, value=30, step=5, label="مقياس الحركة (%)")
|
| 136 |
],
|
| 137 |
outputs=gr.Video(label="الفيديو المتحرك المُنشأ"),
|
| 138 |
title="مولد الفيديو المتحرك بالذكاء الاصطناعي",
|
| 139 |
+
description="""
|
| 140 |
+
قم بإدخال وصف للمشهد وسيقوم النظام بإنشاء فيديو متحرك باستخدام الذكاء الاصطناعي.
|
| 141 |
+
|
| 142 |
+
- استخدم مقياس الحركة للتحكم في مقدار الحركة في الفيديو (10% لحركة بسيطة، 100% لحركة كبيرة)
|
| 143 |
+
- اختر الدقة المناسبة (480p للسرعة، 1080p للجودة العالية)
|
| 144 |
+
- اكتب وصفاً تفصيلياً للمشهد للحصول على أفضل النتائج
|
| 145 |
+
""",
|
| 146 |
theme="huggingface",
|
| 147 |
cache_examples=False
|
| 148 |
)
|
requirements.txt
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
gradio==3.50.2
|
| 2 |
torch==2.0.1
|
| 3 |
-
diffusers==0.
|
| 4 |
-
transformers==4.
|
| 5 |
-
accelerate==0.
|
|
|
|
| 6 |
pillow==9.5.0
|
| 7 |
moviepy==1.0.3
|
| 8 |
numpy==1.24.3
|
|
|
|
| 1 |
gradio==3.50.2
|
| 2 |
torch==2.0.1
|
| 3 |
+
diffusers==0.24.0
|
| 4 |
+
transformers==4.35.2
|
| 5 |
+
accelerate==0.25.0
|
| 6 |
+
huggingface-hub==0.19.4
|
| 7 |
pillow==9.5.0
|
| 8 |
moviepy==1.0.3
|
| 9 |
numpy==1.24.3
|