Spaces:
Runtime error
Runtime error
File size: 5,569 Bytes
f5376d1 d94e7cf a7061b5 d481918 aca9ad6 d481918 d94e7cf a0f0c39 d94e7cf aca9ad6 513387b 2b39201 ed8662b d94e7cf 2b39201 4f79f0c a7061b5 4f79f0c a7061b5 4f79f0c 2b39201 4f79f0c 2b39201 a7061b5 2b39201 4f79f0c 2b39201 a7061b5 ed8662b 2b39201 a7061b5 2b39201 d94e7cf a7061b5 d94e7cf a7061b5 2b39201 a7061b5 d94e7cf a7061b5 a0f0c39 a7061b5 a0f0c39 d481918 a7061b5 2b39201 a7061b5 2b39201 ed8662b a7061b5 aca9ad6 a7061b5 2b39201 aca9ad6 2b39201 d94e7cf a7061b5 d94e7cf aca9ad6 a0f0c39 aca9ad6 a0f0c39 2b39201 f8eeb04 d94e7cf 4f79f0c 2b39201 4f79f0c d94e7cf a0f0c39 d481918 a0f0c39 d481918 d94e7cf d481918 a0f0c39 d481918 2b39201 f8eeb04 a7061b5 d481918 d94e7cf 2b39201 d94e7cf 513387b d481918 d94e7cf 2b39201 513387b a7061b5 2b39201 a7061b5 ed8662b 2b39201 513387b d481918 a0f0c39 d481918 1eec37c d481918 1eec37c a0f0c39 d481918 |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image
import numpy as np
from moviepy.editor import ImageSequenceClip
import os
import tempfile
from tqdm import tqdm
def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p", motion_scale=30):
try:
# تهيئة نموذج Stable Diffusion
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float32,
low_memory=True
).to("cpu")
# تحسين الأداء
pipe.enable_attention_slicing()
# تحديد الحجم
if resolution == "480p":
size = (480, 320) # حجم أصغر للأداء
elif resolution == "720p":
size = (640, 480)
else:
size = (854, 480)
# توليد صورتين فقط
num_images = 2
print(f"جاري توليد {num_images} صور...")
frames = []
for i in tqdm(range(num_images)):
# تعديل البرومبت لكل صورة
current_prompt = text_prompt
if i == 1:
current_prompt += " , with movement and motion"
# توليد الصورة
image = pipe(
prompt=current_prompt,
num_inference_steps=15, # تقليل خطوات الاستدلال
guidance_scale=7.0
).images[0]
# تغيير الحجم
image = image.resize(size, Image.LANCZOS)
frames.append(np.array(image))
if not frames:
raise ValueError("فشل في توليد الصور")
print("جاري إنشاء الفيديو...")
# إنشاء الإطارات الوسيطة
final_frames = []
# إضافة الإطار الأول
final_frames.append(frames[0])
# إنشاء إطارات وسيطة بين الصورتين
num_transitions = 8
for t in range(num_transitions):
alpha = t / num_transitions
transition_frame = (1 - alpha) * frames[0] + alpha * frames[1]
final_frames.append(transition_frame.astype(np.uint8))
# إضافة الإطار الأخير
final_frames.append(frames[1])
# تكرار الإطارات للوصول للمدة المطلوبة
target_frames = int(duration * fps)
if len(final_frames) < target_frames:
final_frames = final_frames * (target_frames // len(final_frames) + 1)
final_frames = final_frames[:target_frames]
# إنشاء الفيديو
temp_dir = tempfile.mkdtemp()
output_path = os.path.join(temp_dir, "output.mp4")
clip = ImageSequenceClip(final_frames, fps=fps)
clip.write_videofile(output_path, codec='libx264', fps=fps)
# تنظيف الذاكرة
pipe = None
torch.cuda.empty_cache()
print("تم إنشاء الفيديو بنجاح!")
return output_path
except Exception as e:
error_msg = str(e)
print(f"حدث خطأ: {error_msg}")
return None # إرجاع None بدلاً من رسالة الخطأ
def video_generator(text_prompt, duration=10, resolution="480p", motion_scale=30):
if not text_prompt:
return None
print(f"بدء توليد فيديو متحرك بناءً على الوصف: {text_prompt}")
print(f"المدة: {duration} ثواني")
print(f"الدقة: {resolution}")
try:
result = create_video_from_text(
text_prompt,
duration=duration,
resolution=resolution,
motion_scale=motion_scale
)
return result
except Exception as e:
print(f"حدث خطأ في المولد: {str(e)}")
return None
# إنشاء واجهة المستخدم
iface = gr.Interface(
fn=video_generator,
inputs=[
gr.Textbox(label="وصف المشهد", placeholder="اكتب وصفاً للمشهد المتحرك الذي تريد إنشاءه..."),
gr.Slider(minimum=3, maximum=10, value=3, step=1, label="مدة الفيديو (بالثواني)"),
gr.Radio(["480p", "720p", "1080p"], label="دقة الفيديو", value="480p"),
gr.Slider(minimum=10, maximum=100, value=30, step=5, label="مقياس الحركة (%)")
],
outputs=gr.Video(label="الفيديو المتحرك المُنشأ"),
title="مولد الفيديو المتحرك بالذكاء الاصطناعي (نسخة خفيفة)",
description="""
قم بإدخال وصف للمشهد وسيقوم النظام بإنشاء فيديو متحرك باستخدام الذكاء الاصطناعي.
نصائح للأداء الأفضل:
- استخدم دقة 480p للحصول على أسرع أداء
- اختر مدة قصيرة (3 ثواني) للتجربة الأولى
- اكتب وصفاً واضحاً وبسيطاً
ملاحظة: هذه نسخة خفيفة جداً تستخدم CPU فقط. قد تستغرق العملية بضع دقائق.
""",
theme="huggingface",
cache_examples=False
)
# تشغيل التطبيق
if __name__ == "__main__":
print("بدء تشغيل التطبيق...")
iface.launch()
|