thomson99 commited on
Commit
a0f0c39
·
verified ·
1 Parent(s): 4c88501

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +58 -24
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,18 +1,24 @@
1
  import gradio as gr
2
  from PIL import Image
3
  import numpy as np
4
- import cv2
5
  from moviepy.editor import ImageSequenceClip
6
  import os
7
  import tempfile
8
- import requests
9
- from io import BytesIO
 
 
 
 
 
10
 
11
  def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
12
  try:
13
- # استخدام API مجاني لتوليد الصور
14
- API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
15
- headers = {"Authorization": "Bearer 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"}
 
 
16
 
17
  # إنشاء مجلد مؤقت لحفظ الصور
18
  temp_dir = tempfile.mkdtemp()
@@ -29,18 +35,40 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
29
  # توليد عدة صور للحصول على تأثير الحركة
30
  num_frames = max(int(duration / 2), 1) # نقلل عدد الإطارات للأداء
31
 
 
 
32
  for i in range(num_frames):
33
- # توليد صورة باستخدام API
34
- response = requests.post(API_URL, headers=headers, json={"inputs": text_prompt})
35
- image = Image.open(BytesIO(response.content))
 
 
 
 
 
 
 
 
 
36
 
37
- # تحويل حجم الصورة
38
- image = image.resize(size, Image.LANCZOS)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # تكرار الإطار عدة مرات للحصول على فيديو أنعم
41
- frame = np.array(image)
42
- for _ in range(int(fps * 2)): # كل صورة تتكرر لمدة 2 ثواني
43
- frames.append(frame)
44
 
45
  # إنشاء الفيديو من الإطارات
46
  clip = ImageSequenceClip(frames, fps=fps)
@@ -49,24 +77,33 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
49
  output_path = os.path.join(temp_dir, "output.mp4")
50
  clip.write_videofile(output_path, codec='libx264', fps=fps, verbose=False, logger=None)
51
 
 
52
  return output_path
53
 
54
  except Exception as e:
55
- print(f"Error: {str(e)}")
56
- return str(e)
 
57
 
58
  def video_generator(text_prompt, duration=10, resolution="480p"):
59
  if not text_prompt:
60
  return "الرجاء إدخال وصف للفيديو"
61
 
 
 
 
 
62
  try:
63
- return create_video_from_text(
64
  text_prompt,
65
  duration=duration,
66
  resolution=resolution
67
  )
 
68
  except Exception as e:
69
- return str(e)
 
 
70
 
71
  # إنشاء واجهة المستخدم باستخدام Gradio
72
  iface = gr.Interface(
@@ -80,13 +117,10 @@ iface = gr.Interface(
80
  title="مولد الفيديو بالذكاء الاصطناعي",
81
  description="قم بإدخال وصف للفيديو الذي تريد إنشاءه وسيقوم النظام بتوليده باستخدام الذكاء الاصطناعي",
82
  theme="huggingface",
83
- examples=[
84
- ["شروق الشمس على شاطئ البحر مع أمواج هادئة", 10, "480p"],
85
- ["مدينة مستقبلية في الليل مع أضواء نيون", 15, "480p"],
86
- ["حديقة جميلة مليئة بالزهور الملونة", 10, "480p"]
87
- ]
88
  )
89
 
90
  # تشغيل التطبيق
91
  if __name__ == "__main__":
 
92
  iface.launch()
 
1
  import gradio as gr
2
  from PIL import Image
3
  import numpy as np
 
4
  from moviepy.editor import ImageSequenceClip
5
  import os
6
  import tempfile
7
+ import time
8
+ import io
9
+ from stability_sdk import client
10
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
11
+
12
+ # تكوين مفتاح API
13
+ STABILITY_KEY = "sk-QG54FiG6g8hd7XUtLIX1n5EvNp8wJbPgeAr9R2YtXKEDVeNV"
14
 
15
  def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
16
  try:
17
+ # تهيئة عميل Stability AI
18
+ stability_api = client.StabilityInference(
19
+ key=STABILITY_KEY,
20
+ verbose=False,
21
+ )
22
 
23
  # إنشاء مجلد مؤقت لحفظ الصور
24
  temp_dir = tempfile.mkdtemp()
 
35
  # توليد عدة صور للحصول على تأثير الحركة
36
  num_frames = max(int(duration / 2), 1) # نقلل عدد الإطارات للأداء
37
 
38
+ print(f"جاري توليد {num_frames} إطار...")
39
+
40
  for i in range(num_frames):
41
+ print(f"توليد الإطار {i+1}/{num_frames}")
42
+ # توليد صورة باستخدام Stability AI
43
+ answers = stability_api.generate(
44
+ prompt=text_prompt,
45
+ seed=int(time.time()), # رقم عشوائي لكل صورة
46
+ steps=30,
47
+ cfg_scale=8.0,
48
+ width=512,
49
+ height=512,
50
+ samples=1,
51
+ sampler=generation.SAMPLER_K_DPMPP_2M
52
+ )
53
 
54
+ for resp in answers:
55
+ for artifact in resp.artifacts:
56
+ if artifact.finish_reason == generation.FILTER:
57
+ raise ValueError("المحتوى غير مناسب، يرجى تعديل الوصف")
58
+ if artifact.type == generation.ARTIFACT_IMAGE:
59
+ img = Image.open(io.BytesIO(artifact.binary))
60
+ # تحويل حجم الصورة
61
+ img = img.resize(size, Image.LANCZOS)
62
+ # تحويل الصورة إلى مصفوفة numpy
63
+ frame = np.array(img)
64
+ # تكرار الإطار عدة مرات للحصول على فيديو أنعم
65
+ for _ in range(int(fps * 2)): # كل صورة تتكرر لمدة 2 ثواني
66
+ frames.append(frame)
67
+
68
+ if not frames:
69
+ raise ValueError("لم يتم توليد أي إطارات للفيديو")
70
 
71
+ print("جاري إنشاء الفيديو...")
 
 
 
72
 
73
  # إنشاء الفيديو من الإطارات
74
  clip = ImageSequenceClip(frames, fps=fps)
 
77
  output_path = os.path.join(temp_dir, "output.mp4")
78
  clip.write_videofile(output_path, codec='libx264', fps=fps, verbose=False, logger=None)
79
 
80
+ print("تم إنشاء الفيديو بنجاح!")
81
  return output_path
82
 
83
  except Exception as e:
84
+ error_msg = str(e)
85
+ print(f"حدث خطأ: {error_msg}")
86
+ return f"حدث خطأ أثناء إنشاء الفيديو: {error_msg}"
87
 
88
  def video_generator(text_prompt, duration=10, resolution="480p"):
89
  if not text_prompt:
90
  return "الرجاء إدخال وصف للفيديو"
91
 
92
+ print(f"بدء توليد فيديو بناءً على الوصف: {text_prompt}")
93
+ print(f"المدة: {duration} ثواني")
94
+ print(f"الدقة: {resolution}")
95
+
96
  try:
97
+ result = create_video_from_text(
98
  text_prompt,
99
  duration=duration,
100
  resolution=resolution
101
  )
102
+ return result
103
  except Exception as e:
104
+ error_msg = str(e)
105
+ print(f"حدث خطأ في المولد: {error_msg}")
106
+ return f"حدث خ��أ: {error_msg}"
107
 
108
  # إنشاء واجهة المستخدم باستخدام Gradio
109
  iface = gr.Interface(
 
117
  title="مولد الفيديو بالذكاء الاصطناعي",
118
  description="قم بإدخال وصف للفيديو الذي تريد إنشاءه وسيقوم النظام بتوليده باستخدام الذكاء الاصطناعي",
119
  theme="huggingface",
120
+ cache_examples=False
 
 
 
 
121
  )
122
 
123
  # تشغيل التطبيق
124
  if __name__ == "__main__":
125
+ print("بدء تشغيل التطبيق...")
126
  iface.launch()
requirements.txt CHANGED
@@ -2,5 +2,5 @@ gradio==3.50.2
2
  pillow==9.5.0
3
  moviepy==1.0.3
4
  numpy==1.24.3
5
- opencv-python==4.7.0.72
6
  requests==2.31.0
 
2
  pillow==9.5.0
3
  moviepy==1.0.3
4
  numpy==1.24.3
5
+ stability-sdk==0.8.5
6
  requests==2.31.0