SepDevX commited on
Commit
58a9a08
·
verified ·
1 Parent(s): d478a5f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -70
app.py CHANGED
@@ -1,89 +1,84 @@
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()
 
 
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()