Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import imageio
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# دالة لتول
|
| 9 |
-
def
|
| 10 |
-
if not api_key:
|
| 11 |
-
return None # إذا لم يكن هناك مفتاح API
|
| 12 |
-
|
| 13 |
-
# استخدم API من Hugging Face لتوليد الصور
|
| 14 |
headers = {
|
| 15 |
"Authorization": f"Bearer {api_key}"
|
| 16 |
}
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
images = []
|
| 23 |
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
images.append(np.array(image))
|
| 33 |
-
else:
|
| 34 |
-
print(f"خطأ في الاستجابة من API: {response.status_code}, {response.text}")
|
| 35 |
-
return None # في حال فشل الاتصال بـ API
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
with imageio.get_writer(output_buffer, format='mp4', mode='I', fps=2) as writer:
|
| 40 |
-
for
|
| 41 |
-
writer.append_data(
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
output_buffer.
|
| 45 |
-
return output_buffer
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
|
| 49 |
-
return None
|
| 50 |
|
| 51 |
-
#
|
| 52 |
def main(video_description, api_key):
|
| 53 |
-
if not api_key:
|
| 54 |
-
return "يرجى إدخال مفتاح API الخاص بك.", None
|
| 55 |
-
|
| 56 |
-
# توليد الفيديو باستخدام الوصف
|
| 57 |
video_data = generate_video(video_description, api_key)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
else:
|
| 62 |
-
return "حدث خطأ أثناء إنشاء الفيديو. يرجى التحقق من الوصف أو مفتاح API.", None
|
| 63 |
-
|
| 64 |
-
# واجهة المستخدم باستخدام Gradio
|
| 65 |
-
def create_interface():
|
| 66 |
-
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("### قم بإدخال وصف الفيديو ومفتاح API الخاص بك.")
|
| 68 |
-
|
| 69 |
-
video_description = gr.Textbox(label="وصف الفيديو", placeholder="اكتب وصف الفيديو هنا...")
|
| 70 |
-
api_key = gr.Textbox(label="مفتاح API", placeholder="أدخل مفتاح API الخاص بـ Hugging Face...")
|
| 71 |
-
|
| 72 |
-
output_text = gr.Textbox(label="النتيجة")
|
| 73 |
-
output_video = gr.Video(label="الفيديو الناتج")
|
| 74 |
-
|
| 75 |
-
submit_button = gr.Button("إنشاء الفيديو")
|
| 76 |
-
submit_button.click(main, inputs=[video_description, api_key], outputs=[output_text, output_video])
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# تشغيل التطبيق
|
| 81 |
-
|
| 82 |
-
demo = create_interface()
|
| 83 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import io
|
| 3 |
import imageio
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import requests
|
| 7 |
|
| 8 |
+
# دالة لاستدعاء API والحصول على الصورة بناءً على الوصف
|
| 9 |
+
def query(payload, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {api_key}"
|
| 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 |
try:
|
| 22 |
+
# إعداد القائمة لتخزين الصور
|
| 23 |
+
image_list = []
|
| 24 |
|
| 25 |
+
# الحصول على الصور بناءً على الوصف
|
| 26 |
+
image_bytes = query({
|
| 27 |
+
"inputs": f"Create 9 sequential images with 1:1 aspect ratio for every one of images of a {video_description}",
|
| 28 |
+
}, api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# فتح الصورة باستخدام PIL
|
| 31 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 32 |
+
|
| 33 |
+
# قائمة الإحداثيات لقص الصورة إلى 9 أجزاء (340 بكسل لكل جزء على افتراض أن الصورة 1020x1020)
|
| 34 |
+
coordinates = [
|
| 35 |
+
(0, 340, 0, 340),
|
| 36 |
+
(340, 680, 0, 340),
|
| 37 |
+
(680, 1020, 0, 340),
|
| 38 |
+
(0, 340, 340, 680),
|
| 39 |
+
(340, 680, 340, 680),
|
| 40 |
+
(680, 1020, 340, 680),
|
| 41 |
+
(0, 340, 680, 1020),
|
| 42 |
+
(340, 680, 680, 1020),
|
| 43 |
+
(680, 1020, 680, 1020),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# قص الأجزاء وإضافتها إلى image_list
|
| 47 |
+
for (x1, x2, y1, y2) in coordinates:
|
| 48 |
+
cropped_image = image.crop((x1, y1, x2, y2))
|
| 49 |
+
image_list.append(np.array(cropped_image)) # تحويل الصورة إلى مصفوفة NumPy
|
| 50 |
+
|
| 51 |
+
# تكرار الصور للحصول على عدد أكبر لإنتاج الفيديو
|
| 52 |
+
image_list_extended = image_list * 10 # تكرار 10 مرات للحصول على فيديو أطول
|
| 53 |
+
|
| 54 |
+
# إنشاء الفيديو في الذاكرة باستخدام imageio
|
| 55 |
+
output_buffer = io.BytesIO()
|
| 56 |
with imageio.get_writer(output_buffer, format='mp4', mode='I', fps=2) as writer:
|
| 57 |
+
for img_array in image_list_extended:
|
| 58 |
+
writer.append_data(img_array) # إضافة المصفوفة مباشرة
|
| 59 |
|
| 60 |
+
output_buffer.seek(0) # العودة إلى بداية الملف
|
| 61 |
+
return output_buffer.getvalue() # إرجاع بيانات الفيديو
|
|
|
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
+
return f"حدث خطأ أثناء إنشاء الفيديو: {e}"
|
|
|
|
| 65 |
|
| 66 |
+
# إنشاء واجهة Gradio
|
| 67 |
def main(video_description, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
video_data = generate_video(video_description, api_key)
|
| 69 |
+
if isinstance(video_data, str): # في حال وجود خطأ
|
| 70 |
+
return video_data, None
|
| 71 |
+
return "تم إنشاء الفيديو بنجاح", (video_data, "video/mp4")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# إعداد الواجهة
|
| 74 |
+
interface = gr.Interface(
|
| 75 |
+
fn=main,
|
| 76 |
+
inputs=[
|
| 77 |
+
gr.Textbox(label="وصف الفيديو", placeholder="أدخل وصف الفيديو المطلوب"),
|
| 78 |
+
gr.Textbox(label="مفتاح API", placeholder="أدخل مفتاح API الخاص بك", type="password")
|
| 79 |
+
],
|
| 80 |
+
outputs=[
|
| 81 |
+
gr.Textbox(label="الحالة"),
|
| 82 |
+
gr.Video(label="الفيديو الناتج")
|
| 83 |
+
],
|
| 84 |
+
title="مولد الفيديوهات",
|
| 85 |
+
description="أدخل وصفًا للحصول على فيديو، تأكد من إدخال مفتاح API صالح من Hugging Face."
|
| 86 |
+
)
|
| 87 |
|
| 88 |
# تشغيل التطبيق
|
| 89 |
+
interface.launch()
|
|
|
|
|
|