Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| from diffusers.utils import export_to_video | |
| import os | |
| # تنزيل النموذج المخصص لتحويل النص إلى فيديو (بجودة ممتازة متوافقة مع كولاب) | |
| print("جاري تحميل النموذج... قد يستغرق هذا بضع دقائق في المرة الأولى.") | |
| pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16) | |
| # تفريغ الذاكرة تلقائياً لتجنب توقف كولاب بسبب نفاد الرامات | |
| pipe.enable_model_cpu_offload() | |
| def generate_video(prompt, negative_prompt, steps): | |
| try: | |
| # إعدادات توليد الفيديو | |
| video_frames = pipe( | |
| prompt, | |
| negative_prompt=negative_prompt, | |
| num_inference_steps=int(steps), | |
| height=320, | |
| width=576, | |
| num_frames=24 # عدد الإطارات (يحدد طول الفيديو) | |
| ).frames[0] | |
| # حفظ الفيديو | |
| output_path = "output_video.mp4" | |
| export_to_video(video_frames, output_path) | |
| return output_path | |
| except Exception as e: | |
| return f"حدث خطأ: {str(e)}" | |
| # تصميم واجهة المستخدم (مظهر احترافي وجميل) | |
| custom_theme = gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="indigo", | |
| ).set( | |
| button_primary_background_fill="*primary_500", | |
| button_primary_background_fill_hover="*primary_600", | |
| ) | |
| with gr.Blocks(theme=custom_theme, title="أداة محمد الصرماني") as demo: | |
| gr.HTML( | |
| """ | |
| <div style="text-align: center; padding: 20px; background-color: #1e1e2f; color: white; border-radius: 10px; margin-bottom: 20px;"> | |
| <h1 style="color: #4da6ff; margin: 0;">🎥 أداة محمد الصرماني من ليبيا 🇱🇾</h1> | |
| <p style="font-size: 16px; margin-top: 5px;">لتحويل النصوص إلى فيديوهات عالية الدقة باستخدام الذكاء الاصطناعي</p> | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| prompt = gr.Textbox(label="وصف الفيديو (يفضل باللغة الإنجليزية لأفضل نتيجة)", placeholder="مثال: A cinematic shot of a futuristic city at night, neon lights...", lines=3) | |
| negative_prompt = gr.Textbox(label="الأشياء التي لا تريدها في الفيديو (اختياري)", placeholder="مثال: blurry, low quality, distorted", value="blurry, distorted, low quality, bad anatomy") | |
| steps = gr.Slider(minimum=20, maximum=50, value=30, step=1, label="جودة المعالجة (رقم أكبر = جودة أفضل ووقت أطول)") | |
| generate_btn = gr.Button("🚀 بدء توليد الفيديو", variant="primary", size="lg") | |
| with gr.Column(scale=1): | |
| video_output = gr.Video(label="الفيديو النهائي (بصيغة MP4)") | |
| # ربط الزر بدالة التوليد | |
| generate_btn.click( | |
| fn=generate_video, | |
| inputs=[prompt, negative_prompt, steps], | |
| outputs=video_output | |
| ) | |
| # تشغيل الواجهة وتوليد رابط Gradio.live | |
| demo.launch(share=True, debug=True) | |