Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,84 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import io
|
|
|
|
| 3 |
import imageio
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# دالة لاستدعاء API والحصول على الصورة بناءً على الوصف
|
| 9 |
def query(payload, api_key):
|
| 10 |
-
headers = {
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
response = requests.post("https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4", headers=headers, json=payload)
|
| 14 |
-
if response.status_code == 200:
|
| 15 |
-
return response.content
|
| 16 |
-
else:
|
| 17 |
-
raise ValueError(f"API Error {response.status_code}: {response.text}")
|
| 18 |
|
| 19 |
-
# دالة لتوليد الفيديو
|
| 20 |
def generate_video(video_description, api_key):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
image_list = []
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
return f"حدث خطأ أثناء إنشاء الفيديو: {e}"
|
| 65 |
-
|
| 66 |
-
# إنشاء واجهة Gradio
|
| 67 |
def main(video_description, api_key):
|
| 68 |
-
|
| 69 |
-
if
|
| 70 |
-
return
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
import imageio
|
| 6 |
import numpy as np
|
| 7 |
+
import base64
|
| 8 |
+
|
| 9 |
+
# إعداد API
|
| 10 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
| 11 |
|
|
|
|
| 12 |
def query(payload, api_key):
|
| 13 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
| 14 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 15 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
def generate_video(video_description, api_key):
|
| 18 |
+
# إعداد القائمة لتخزين الصور
|
| 19 |
+
image_list = []
|
|
|
|
| 20 |
|
| 21 |
+
# الحصول على الصور بناءً على الوصف
|
| 22 |
+
image_bytes = query({
|
| 23 |
+
"inputs": "Create 9 sequential images with 1:1 aspect ratio for every one of images of a " + video_description,
|
| 24 |
+
}, api_key)
|
| 25 |
|
| 26 |
+
# فتح الصورة باستخدام PIL
|
| 27 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 28 |
|
| 29 |
+
# قائمة الإحداثيات لقص الصورة إلى 9 أجزاء
|
| 30 |
+
coordinates = [
|
| 31 |
+
(0, 340, 0, 340),
|
| 32 |
+
(340, 680, 0, 340),
|
| 33 |
+
(680, 1020, 0, 340),
|
| 34 |
+
(0, 340, 340, 680),
|
| 35 |
+
(340, 680, 340, 680),
|
| 36 |
+
(680, 1020, 340, 680),
|
| 37 |
+
(0, 340, 680, 1020),
|
| 38 |
+
(340, 680, 680, 1020),
|
| 39 |
+
(680, 1020, 680, 1020),
|
| 40 |
+
]
|
| 41 |
|
| 42 |
+
# قص الأجزاء وإضافتها إلى image_list
|
| 43 |
+
for (x1, x2, y1, y2) in coordinates:
|
| 44 |
+
cropped_image = image.crop((x1, y1, x2, y2))
|
| 45 |
+
image_list.append(np.array(cropped_image)) # تحويل الصورة إلى مصفوفة NumPy
|
| 46 |
|
| 47 |
+
# تكرار الصور للحصول على عدد أكبر لإنتاج الفيديو
|
| 48 |
+
image_list_extended = image_list * 10 # تكرار 10 مرات للحصول على فيديو أطول
|
| 49 |
|
| 50 |
+
# إنشاء الفيديو في الذاكرة باستخدام imageio
|
| 51 |
+
output_buffer = io.BytesIO()
|
| 52 |
+
with imageio.get_writer(output_buffer, format='mp4', mode='I', fps=2) as writer:
|
| 53 |
+
for img_array in image_list_extended:
|
| 54 |
+
writer.append_data(img_array) # إضافة المصفوفة مباشرة
|
| 55 |
|
| 56 |
+
output_buffer.seek(0) # العودة إلى بداية الملف
|
| 57 |
+
return output_buffer.getvalue() # إرجاع بيانات الفيديو
|
| 58 |
|
| 59 |
+
# إعداد واجهة المستخدم باستخدام Gradio
|
|
|
|
|
|
|
|
|
|
| 60 |
def main(video_description, api_key):
|
| 61 |
+
# التحقق من وجود مفتاح API
|
| 62 |
+
if not api_key:
|
| 63 |
+
return "يرجى إدخال مفتاح API الخاص بك."
|
| 64 |
+
|
| 65 |
+
# إظهار رسالة التحميل
|
| 66 |
+
message = "يتم الآن إنشاء الفيديو، يرجى الانتظار لبضع دقائق."
|
| 67 |
|
| 68 |
+
try:
|
| 69 |
+
# إنشاء الفيديو
|
| 70 |
+
video_data = generate_video(video_description, api_key)
|
| 71 |
+
|
| 72 |
+
# تحويل الفيديو إلى Base64
|
| 73 |
+
video_base64 = base64.b64encode(video_data).decode('utf-8')
|
| 74 |
+
|
| 75 |
+
# إرجاع الفيديو
|
| 76 |
+
video_output = f'<video width="320" height="240" controls><source src="data:video/mp4;base64,{video_base64}" type="video/mp4">Your browser does not support the video tag.</video>'
|
| 77 |
+
|
| 78 |
+
return message, video_output
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"حدث خطأ أثناء إنشاء الفيديو: {str(e)}"
|
|
|
|
| 81 |
|
| 82 |
+
# واجهة Gradio
|
| 83 |
+
with gr.Interface(fn=main, inputs=[gr.Textbox(label="Enter Video Description"), gr.Textbox(label="Enter your Hugging Face API Key")], outputs=[gr.Textbox(), gr.HTML()]) as demo:
|
| 84 |
+
demo.launch()
|