Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +11 -20
- requirements.txt +1 -8
app.py
CHANGED
|
@@ -1,23 +1,18 @@
|
|
| 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
|
| 7 |
from moviepy.editor import ImageSequenceClip
|
| 8 |
import os
|
| 9 |
import tempfile
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
|
| 12 |
try:
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 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()
|
|
@@ -32,19 +27,19 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
|
|
| 32 |
size = (1920, 1080)
|
| 33 |
|
| 34 |
# توليد عدة صور للحصول على تأثير الحركة
|
| 35 |
-
num_frames = max(int(duration
|
| 36 |
|
| 37 |
for i in range(num_frames):
|
| 38 |
-
# توليد صورة
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
# تحويل حجم الصورة
|
| 43 |
image = image.resize(size, Image.LANCZOS)
|
| 44 |
|
| 45 |
# تكرار الإطار عدة مرات للحصول على فيديو أنعم
|
| 46 |
frame = np.array(image)
|
| 47 |
-
for _ in range(
|
| 48 |
frames.append(frame)
|
| 49 |
|
| 50 |
# إنشاء الفيديو من الإطارات
|
|
@@ -59,10 +54,6 @@ def create_video_from_text(text_prompt, duration=10, fps=30, resolution="480p"):
|
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error: {str(e)}")
|
| 61 |
return str(e)
|
| 62 |
-
finally:
|
| 63 |
-
# تحرير الذاكرة
|
| 64 |
-
if torch.cuda.is_available():
|
| 65 |
-
torch.cuda.empty_cache()
|
| 66 |
|
| 67 |
def video_generator(text_prompt, duration=10, resolution="480p"):
|
| 68 |
if not text_prompt:
|
|
@@ -90,7 +81,7 @@ iface = gr.Interface(
|
|
| 90 |
description="قم بإدخال وصف للفيديو الذي تريد إنشاءه وسيقوم النظام بتوليده باستخدام الذكاء الاصطناعي",
|
| 91 |
theme="huggingface",
|
| 92 |
examples=[
|
| 93 |
-
["شروق الشمس على شاطئ البحر مع أمواج هادئة", 10, "
|
| 94 |
["مدينة مستقبلية في الليل مع أضواء نيون", 15, "480p"],
|
| 95 |
["حديقة جميلة مليئة بالزهور الملونة", 10, "480p"]
|
| 96 |
]
|
|
|
|
| 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()
|
|
|
|
| 27 |
size = (1920, 1080)
|
| 28 |
|
| 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 |
# إنشاء الفيديو من الإطارات
|
|
|
|
| 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:
|
|
|
|
| 81 |
description="قم بإدخال وصف للفيديو الذي تريد إنشاءه وسيقوم النظام بتوليده باستخدام الذكاء الاصطناعي",
|
| 82 |
theme="huggingface",
|
| 83 |
examples=[
|
| 84 |
+
["شروق الشمس على شاطئ البحر مع أمواج هادئة", 10, "480p"],
|
| 85 |
["مدينة مستقبلية في الليل مع أضواء نيون", 15, "480p"],
|
| 86 |
["حديقة جميلة مليئة بالزهور الملونة", 10, "480p"]
|
| 87 |
]
|
requirements.txt
CHANGED
|
@@ -1,13 +1,6 @@
|
|
| 1 |
-
flask==2.0.1
|
| 2 |
-
openai==0.27.0
|
| 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
|
| 13 |
opencv-python==4.7.0.72
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
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
|