Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 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
|
|
@@ -13,14 +12,15 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
|
|
| 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.
|
| 19 |
-
safety_checker=None
|
| 20 |
)
|
| 21 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 22 |
pipe.enable_attention_slicing()
|
| 23 |
-
pipe = pipe.to(
|
| 24 |
|
| 25 |
# إنشاء مجلد مؤقت لحفظ الصور
|
| 26 |
temp_dir = tempfile.mkdtemp()
|
|
@@ -34,58 +34,62 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
|
|
| 34 |
else: # 1080p
|
| 35 |
size = (1920, 1080)
|
| 36 |
|
| 37 |
-
# عدد الإطارات
|
| 38 |
-
|
|
|
|
| 39 |
print(f"جاري توليد {num_frames} إطار...")
|
| 40 |
|
| 41 |
# توليد الإطار الأول
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# توليد ال
|
| 53 |
image = pipe(
|
| 54 |
prompt=text_prompt,
|
| 55 |
-
latents=
|
| 56 |
-
num_inference_steps=
|
| 57 |
guidance_scale=7.5,
|
| 58 |
generator=generator
|
| 59 |
).images[0]
|
| 60 |
|
| 61 |
frames.append(np.array(image.resize(size)))
|
| 62 |
|
| 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)))
|
| 81 |
-
|
| 82 |
if not frames:
|
| 83 |
raise ValueError("فشل في توليد الإطارات")
|
| 84 |
|
| 85 |
print("جاري إنشاء الفيديو...")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
# إنشاء الفيديو من الإطارات
|
| 88 |
-
clip = ImageSequenceClip(
|
| 89 |
|
| 90 |
# حفظ الفيديو في ملف مؤقت
|
| 91 |
output_path = os.path.join(temp_dir, "output.mp4")
|
|
@@ -142,6 +146,8 @@ iface = gr.Interface(
|
|
| 142 |
- استخدم مقياس الحركة للتحكم في مقدار الحركة في الفيديو (10% لحركة بسيطة، 100% لحركة كبيرة)
|
| 143 |
- اختر الدقة المناسبة (480p للسرعة، 1080p للجودة العالية)
|
| 144 |
- اكتب وصفاً تفصيلياً للمشهد للحصول على أفضل النتائج
|
|
|
|
|
|
|
| 145 |
""",
|
| 146 |
theme="huggingface",
|
| 147 |
cache_examples=False
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 12 |
try:
|
| 13 |
# تهيئة نموذج Stable Diffusion
|
| 14 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 15 |
+
device = "cpu" # استخدام CPU
|
| 16 |
+
|
| 17 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
model_id,
|
| 19 |
+
torch_dtype=torch.float32, # استخدام float32 للـ CPU
|
|
|
|
| 20 |
)
|
| 21 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 22 |
pipe.enable_attention_slicing()
|
| 23 |
+
pipe = pipe.to(device)
|
| 24 |
|
| 25 |
# إنشاء مجلد مؤقت لحفظ الصور
|
| 26 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
| 34 |
else: # 1080p
|
| 35 |
size = (1920, 1080)
|
| 36 |
|
| 37 |
+
# تقليل عدد الإطارات للأداء على CPU
|
| 38 |
+
fps_reduced = 15 # تقليل معدل الإطارات
|
| 39 |
+
num_frames = int(duration * fps_reduced / 2) # تقليل عدد الإطارات
|
| 40 |
print(f"جاري توليد {num_frames} إطار...")
|
| 41 |
|
| 42 |
# توليد الإطار الأول
|
| 43 |
+
generator = torch.Generator(device=device).manual_seed(42)
|
| 44 |
+
latents = torch.randn(
|
| 45 |
+
(1, pipe.unet.config.in_channels, 64, 64),
|
| 46 |
+
generator=generator,
|
| 47 |
+
device=device,
|
| 48 |
+
dtype=torch.float32
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# توليد الصورة الأولى
|
| 52 |
+
image = pipe(
|
| 53 |
+
prompt=text_prompt,
|
| 54 |
+
latents=latents,
|
| 55 |
+
num_inference_steps=20, # تقليل خطوات الاستدلال
|
| 56 |
+
guidance_scale=7.5,
|
| 57 |
+
generator=generator
|
| 58 |
+
).images[0]
|
| 59 |
+
|
| 60 |
+
frames.append(np.array(image.resize(size)))
|
| 61 |
+
|
| 62 |
+
# توليد الإطارات المتتالية مع حركة تدريجية
|
| 63 |
+
for i in tqdm(range(1, num_frames)):
|
| 64 |
+
# إضافة اضطراب تدريجي للكمون
|
| 65 |
+
time_embed = torch.tensor([i / num_frames], device=device, dtype=torch.float32)
|
| 66 |
+
noise = torch.randn_like(latents) * (motion_scale / 100)
|
| 67 |
+
current_latents = latents + noise * time_embed.view(-1, 1, 1, 1)
|
| 68 |
|
| 69 |
+
# توليد الإطار الجديد
|
| 70 |
image = pipe(
|
| 71 |
prompt=text_prompt,
|
| 72 |
+
latents=current_latents,
|
| 73 |
+
num_inference_steps=20,
|
| 74 |
guidance_scale=7.5,
|
| 75 |
generator=generator
|
| 76 |
).images[0]
|
| 77 |
|
| 78 |
frames.append(np.array(image.resize(size)))
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if not frames:
|
| 81 |
raise ValueError("فشل في توليد الإطارات")
|
| 82 |
|
| 83 |
print("جاري إنشاء الفيديو...")
|
| 84 |
|
| 85 |
+
# تكرار الإطارات للوصول إلى FPS المطلوب
|
| 86 |
+
frames_extended = []
|
| 87 |
+
for frame in frames:
|
| 88 |
+
repeat_times = int(fps / fps_reduced)
|
| 89 |
+
frames_extended.extend([frame] * repeat_times)
|
| 90 |
+
|
| 91 |
# إنشاء الفيديو من الإطارات
|
| 92 |
+
clip = ImageSequenceClip(frames_extended, fps=fps)
|
| 93 |
|
| 94 |
# حفظ الفيديو في ملف مؤقت
|
| 95 |
output_path = os.path.join(temp_dir, "output.mp4")
|
|
|
|
| 146 |
- استخدم مقياس الحركة للتحكم في مقدار الحركة في الفيديو (10% لحركة بسيطة، 100% لحركة كبيرة)
|
| 147 |
- اختر الدقة المناسبة (480p للسرعة، 1080p للجودة العالية)
|
| 148 |
- اكتب وصفاً تفصيلياً للمشهد للحصول على أفضل النتائج
|
| 149 |
+
|
| 150 |
+
ملاحظة: يتم تشغيل النموذج على CPU مما قد يؤدي إلى بطء في الأداء. للحصول على أداء أفضل، يُنصح باستخدام GPU.
|
| 151 |
""",
|
| 152 |
theme="huggingface",
|
| 153 |
cache_examples=False
|