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()