thomson99 commited on
Commit
2b39201
·
verified ·
1 Parent(s): a7061b5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -40
app.py CHANGED
@@ -11,47 +11,40 @@ from tqdm import tqdm
11
  def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p", motion_scale=30):
12
  try:
13
  # تهيئة نموذج Stable Diffusion
14
- model_id = "CompVis/stable-diffusion-v1-4" # نموذج أخف
15
- device = "cpu"
16
 
17
  pipe = StableDiffusionPipeline.from_pretrained(
18
  model_id,
19
- torch_dtype=torch.float32
20
- )
21
- pipe = pipe.to(device)
22
 
23
  # تحسين الأداء
24
  pipe.enable_attention_slicing()
25
- pipe.enable_model_cpu_offload()
26
-
27
- # إنشاء مجلد مؤقت
28
- temp_dir = tempfile.mkdtemp()
29
- frames = []
30
 
31
  # تحديد الحجم
32
  if resolution == "480p":
33
- size = (640, 480) # حجم أصغر للأداء
34
  elif resolution == "720p":
35
- size = (1280, 720)
36
  else:
37
- size = (1920, 1080)
38
 
39
- # توليد 3 صور فقط
40
- num_images = 3
41
  print(f"جاري توليد {num_images} صور...")
42
 
 
43
  for i in tqdm(range(num_images)):
44
  # تعديل البرومبت لكل صورة
45
  current_prompt = text_prompt
46
  if i == 1:
47
- current_prompt += " , slight movement"
48
- elif i == 2:
49
- current_prompt += " , more movement"
50
 
51
  # توليد الصورة
52
  image = pipe(
53
  prompt=current_prompt,
54
- num_inference_steps=20, # تقليل خطوات الاستدلال
55
  guidance_scale=7.0
56
  ).images[0]
57
 
@@ -66,19 +59,19 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
66
 
67
  # إنشاء الإطارات الوسيطة
68
  final_frames = []
69
- for i in range(len(frames)-1):
70
- # إضافة الإطار الحالي
71
- final_frames.append(frames[i])
72
-
73
- # إنشاء إطارات وسيطة
74
- num_transitions = 10 # عدد الإطارات الوسيطة
75
- for t in range(num_transitions):
76
- alpha = t / num_transitions
77
- transition_frame = (1 - alpha) * frames[i] + alpha * frames[i+1]
78
- final_frames.append(transition_frame.astype(np.uint8))
79
 
80
  # إضافة الإطار الأخير
81
- final_frames.append(frames[-1])
82
 
83
  # تكرار الإطارات للوصول للمدة المطلوبة
84
  target_frames = int(duration * fps)
@@ -87,8 +80,10 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
87
  final_frames = final_frames[:target_frames]
88
 
89
  # إنشاء الفيديو
90
- clip = ImageSequenceClip(final_frames, fps=fps)
91
  output_path = os.path.join(temp_dir, "output.mp4")
 
 
92
  clip.write_videofile(output_path, codec='libx264', fps=fps)
93
 
94
  # تنظيف الذاكرة
@@ -101,11 +96,11 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p",
101
  except Exception as e:
102
  error_msg = str(e)
103
  print(f"حدث خطأ: {error_msg}")
104
- return f"حدث خطأ أثناء إنشاء الفيديو: {error_msg}"
105
 
106
  def video_generator(text_prompt, duration=10, resolution="480p", motion_scale=30):
107
  if not text_prompt:
108
- return "الرجاء إدخال وصف للفيديو"
109
 
110
  print(f"بدء توليد فيديو متحرك بناءً على الوصف: {text_prompt}")
111
  print(f"المدة: {duration} ثواني")
@@ -120,30 +115,29 @@ def video_generator(text_prompt, duration=10, resolution="480p", motion_scale=30
120
  )
121
  return result
122
  except Exception as e:
123
- error_msg = str(e)
124
- print(f"حدث خطأ في المولد: {error_msg}")
125
- return f"حدث خطأ: {error_msg}"
126
 
127
  # إنشاء واجهة المستخدم
128
  iface = gr.Interface(
129
  fn=video_generator,
130
  inputs=[
131
  gr.Textbox(label="وصف المشهد", placeholder="اكتب وصفاً للمشهد المتحرك الذي تريد إنشاءه..."),
132
- gr.Slider(minimum=3, maximum=15, value=5, step=1, label="مدة الفيديو (بالثواني)"),
133
  gr.Radio(["480p", "720p", "1080p"], label="دقة الفيديو", value="480p"),
134
  gr.Slider(minimum=10, maximum=100, value=30, step=5, label="مقياس الحركة (%)")
135
  ],
136
  outputs=gr.Video(label="الفيديو المتحرك المُنشأ"),
137
- title="مولد الفيديو المتحرك بالذكاء الاصطناعي (نسخة سريعة)",
138
  description="""
139
  قم بإدخال وصف للمشهد وسيقوم النظام بإنشاء فيديو متحرك باستخدام الذكاء الاصطناعي.
140
 
141
  نصائح للأداء الأفضل:
142
  - استخدم دقة 480p للحصول على أسرع أداء
143
- - اختر مدة قصيرة (3-5 ثواني) للتجربة الأولى
144
  - اكتب وصفاً واضحاً وبسيطاً
145
 
146
- ملاحظة: هذه نسخة مُحسنة للأداء على CPU. تم تبسيط عملية توليد الفيديو للحصول على نتائج أسرع.
147
  """,
148
  theme="huggingface",
149
  cache_examples=False
 
11
  def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p", motion_scale=30):
12
  try:
13
  # تهيئة نموذج Stable Diffusion
14
+ model_id = "CompVis/stable-diffusion-v1-4"
 
15
 
16
  pipe = StableDiffusionPipeline.from_pretrained(
17
  model_id,
18
+ torch_dtype=torch.float32,
19
+ low_memory=True
20
+ ).to("cpu")
21
 
22
  # تحسين الأداء
23
  pipe.enable_attention_slicing()
 
 
 
 
 
24
 
25
  # تحديد الحجم
26
  if resolution == "480p":
27
+ size = (480, 320) # حجم أصغر للأداء
28
  elif resolution == "720p":
29
+ size = (640, 480)
30
  else:
31
+ size = (854, 480)
32
 
33
+ # توليد صورتين فقط
34
+ num_images = 2
35
  print(f"جاري توليد {num_images} صور...")
36
 
37
+ frames = []
38
  for i in tqdm(range(num_images)):
39
  # تعديل البرومبت لكل صورة
40
  current_prompt = text_prompt
41
  if i == 1:
42
+ current_prompt += " , with movement and motion"
 
 
43
 
44
  # توليد الصورة
45
  image = pipe(
46
  prompt=current_prompt,
47
+ num_inference_steps=15, # تقليل خطوات الاستدلال
48
  guidance_scale=7.0
49
  ).images[0]
50
 
 
59
 
60
  # إنشاء الإطارات الوسيطة
61
  final_frames = []
62
+
63
+ # إضافة الإطار الأول
64
+ final_frames.append(frames[0])
65
+
66
+ # إنشاء إطارات وسيطة بين الصورتين
67
+ num_transitions = 8
68
+ for t in range(num_transitions):
69
+ alpha = t / num_transitions
70
+ transition_frame = (1 - alpha) * frames[0] + alpha * frames[1]
71
+ final_frames.append(transition_frame.astype(np.uint8))
72
 
73
  # إضافة الإطار الأخير
74
+ final_frames.append(frames[1])
75
 
76
  # تكرار الإطارات للوصول للمدة المطلوبة
77
  target_frames = int(duration * fps)
 
80
  final_frames = final_frames[:target_frames]
81
 
82
  # إنشاء الفيديو
83
+ temp_dir = tempfile.mkdtemp()
84
  output_path = os.path.join(temp_dir, "output.mp4")
85
+
86
+ clip = ImageSequenceClip(final_frames, fps=fps)
87
  clip.write_videofile(output_path, codec='libx264', fps=fps)
88
 
89
  # تنظيف الذاكرة
 
96
  except Exception as e:
97
  error_msg = str(e)
98
  print(f"حدث خطأ: {error_msg}")
99
+ return None # إرجاع None بدلاً من رسالة الخطأ
100
 
101
  def video_generator(text_prompt, duration=10, resolution="480p", motion_scale=30):
102
  if not text_prompt:
103
+ return None
104
 
105
  print(f"بدء توليد فيديو متحرك بناءً على الوصف: {text_prompt}")
106
  print(f"المدة: {duration} ثواني")
 
115
  )
116
  return result
117
  except Exception as e:
118
+ print(f"حدث خطأ في المولد: {str(e)}")
119
+ return None
 
120
 
121
  # إنشاء واجهة المستخدم
122
  iface = gr.Interface(
123
  fn=video_generator,
124
  inputs=[
125
  gr.Textbox(label="وصف المشهد", placeholder="اكتب وصفاً للمشهد المتحرك الذي تريد إنشاءه..."),
126
+ gr.Slider(minimum=3, maximum=10, value=3, step=1, label="مدة الفيديو (بالثواني)"),
127
  gr.Radio(["480p", "720p", "1080p"], label="دقة الفيديو", value="480p"),
128
  gr.Slider(minimum=10, maximum=100, value=30, step=5, label="مقياس الحركة (%)")
129
  ],
130
  outputs=gr.Video(label="الفيديو المتحرك المُنشأ"),
131
+ title="مولد الفيديو المتحرك بالذكاء الاصطناعي (نسخة خفيفة)",
132
  description="""
133
  قم بإدخال وصف للمشهد وسيقوم النظام بإنشاء فيديو متحرك باستخدام الذكاء الاصطناعي.
134
 
135
  نصائح للأداء الأفضل:
136
  - استخدم دقة 480p للحصول على أسرع أداء
137
+ - اختر مدة قصيرة (3 ثواني) للتجربة الأولى
138
  - اكتب وصفاً واضحاً وبسيطاً
139
 
140
+ ملاحظة: هذه نسخة خفيفة جداً تستخدم CPU فقط. قد تستغرق العملية بضع دقائق.
141
  """,
142
  theme="huggingface",
143
  cache_examples=False