thomson99 commited on
Commit
4f79f0c
·
verified ·
1 Parent(s): aca9ad6

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +38 -29
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import StableDiffusionPipeline
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
@@ -9,48 +9,55 @@ import os
9
  import tempfile
10
 
11
  def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
12
- # تهيئة نموذج Stable Diffusion
13
- model_id = "runwayml/stable-diffusion-v1-5"
14
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
15
- if torch.cuda.is_available():
16
- pipe = pipe.to("cuda")
17
-
18
- # إنشاء مجلد مؤقت لحفظ الصور
19
- temp_dir = tempfile.mkdtemp()
20
- frames = []
21
-
22
- # تحديد حجم الصورة حسب الدقة المطلوبة
23
- if resolution == "480p":
24
- size = (854, 480)
25
- elif resolution == "720p":
26
- size = (1280, 720)
27
- else: # 1080p
28
- size = (1920, 1080)
29
-
30
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # توليد عدة صور للحصول على تأثير الحركة
32
- num_frames = int(duration * fps / 10) # نقلل عدد الإطارات للأداء
 
33
  for i in range(num_frames):
34
  # توليد صورة
35
- image = pipe(text_prompt).images[0]
 
36
 
37
  # تحويل حجم الصورة
38
- image = image.resize(size)
39
 
40
  # تكرار الإطار عدة مرات للحصول على فيديو أنعم
 
41
  for _ in range(10): # كل صورة تتكرر 10 مرات
42
- frames.append(np.array(image))
43
 
44
  # إنشاء الفيديو من الإطارات
45
  clip = ImageSequenceClip(frames, fps=fps)
46
 
47
  # حفظ الفيديو في ملف مؤقت
48
  output_path = os.path.join(temp_dir, "output.mp4")
49
- clip.write_videofile(output_path, codec='libx264', fps=fps)
50
 
51
  return output_path
52
 
53
  except Exception as e:
 
54
  return str(e)
55
  finally:
56
  # تحرير الذاكرة
@@ -58,13 +65,15 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
58
  torch.cuda.empty_cache()
59
 
60
  def video_generator(text_prompt, duration=10, resolution="480p"):
 
 
 
61
  try:
62
- video_path = create_video_from_text(
63
  text_prompt,
64
  duration=duration,
65
  resolution=resolution
66
  )
67
- return video_path
68
  except Exception as e:
69
  return str(e)
70
 
@@ -81,9 +90,9 @@ iface = gr.Interface(
81
  description="قم بإدخال وصف للفيديو الذي تريد إنشاءه وسيقوم النظام بتوليده باستخدام الذكاء الاصطناعي",
82
  theme="huggingface",
83
  examples=[
84
- ["شروق الشمس على شاطئ البحر", 10, "720p"],
85
- ["مدينة مستقبلية في الليل", 15, "1080p"],
86
- ["حديقة جميلة مليئة بالزهور", 20, "480p"]
87
  ]
88
  )
89
 
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import DiffusionPipeline
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
 
9
  import tempfile
10
 
11
  def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
+ # تهيئة نموذج لتوليد الصور
14
+ pipe = DiffusionPipeline.from_pretrained(
15
+ "runwayml/stable-diffusion-v1-5",
16
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
17
+ )
18
+
19
+ if torch.cuda.is_available():
20
+ pipe = pipe.to("cuda")
21
+
22
+ # إنشاء مجلد مؤقت لحفظ الصور
23
+ temp_dir = tempfile.mkdtemp()
24
+ frames = []
25
+
26
+ # تحديد حجم الصورة حسب الدقة المطلوبة
27
+ if resolution == "480p":
28
+ size = (854, 480)
29
+ elif resolution == "720p":
30
+ size = (1280, 720)
31
+ else: # 1080p
32
+ size = (1920, 1080)
33
+
34
  # توليد عدة صور للحصول على تأثير الحركة
35
+ num_frames = max(int(duration * fps / 10), 1) # على الأقل إطار واحد
36
+
37
  for i in range(num_frames):
38
  # توليد صورة
39
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
40
+ image = pipe(text_prompt, num_inference_steps=20).images[0]
41
 
42
  # تحويل حجم الصورة
43
+ image = image.resize(size, Image.LANCZOS)
44
 
45
  # تكرار الإطار عدة مرات للحصول على فيديو أنعم
46
+ frame = np.array(image)
47
  for _ in range(10): # كل صورة تتكرر 10 مرات
48
+ frames.append(frame)
49
 
50
  # إنشاء الفيديو من الإطارات
51
  clip = ImageSequenceClip(frames, fps=fps)
52
 
53
  # حفظ الفيديو في ملف مؤقت
54
  output_path = os.path.join(temp_dir, "output.mp4")
55
+ clip.write_videofile(output_path, codec='libx264', fps=fps, verbose=False, logger=None)
56
 
57
  return output_path
58
 
59
  except Exception as e:
60
+ print(f"Error: {str(e)}")
61
  return str(e)
62
  finally:
63
  # تحرير الذاكرة
 
65
  torch.cuda.empty_cache()
66
 
67
  def video_generator(text_prompt, duration=10, resolution="480p"):
68
+ if not text_prompt:
69
+ return "الرجاء إدخال وصف للفيديو"
70
+
71
  try:
72
+ return create_video_from_text(
73
  text_prompt,
74
  duration=duration,
75
  resolution=resolution
76
  )
 
77
  except Exception as e:
78
  return str(e)
79
 
 
90
  description="قم بإدخال وصف للفيديو الذي تريد إنشاءه وسيقوم النظام بتوليده باستخدام الذكاء الاصطناعي",
91
  theme="huggingface",
92
  examples=[
93
+ ["شروق الشمس على شاطئ البحر مع أمواج هادئة", 10, "720p"],
94
+ ["مدينة مستقبلية في الليل مع أضواء نيون", 15, "480p"],
95
+ ["حديقة جميلة مليئة بالزهور الملونة", 10, "480p"]
96
  ]
97
  )
98
 
requirements.txt CHANGED
@@ -3,9 +3,10 @@ openai==0.27.0
3
  python-dotenv==0.19.0
4
  gradio==3.50.2
5
  torch==2.0.1
6
- diffusers==0.21.4
7
  transformers==4.30.2
8
  accelerate==0.21.0
 
9
  pillow==9.5.0
10
  moviepy==1.0.3
11
  numpy==1.24.3
 
3
  python-dotenv==0.19.0
4
  gradio==3.50.2
5
  torch==2.0.1
6
+ diffusers==0.19.3
7
  transformers==4.30.2
8
  accelerate==0.21.0
9
+ huggingface-hub==0.16.4
10
  pillow==9.5.0
11
  moviepy==1.0.3
12
  numpy==1.24.3