SepDevX commited on
Commit
3ece7ec
·
verified ·
1 Parent(s): bc04c37
Files changed (1) hide show
  1. app.py +83 -5
app.py CHANGED
@@ -1,5 +1,83 @@
1
- gradio
2
- requests
3
- Pillow
4
- imageio
5
- numpy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import gradio as gr
4
+ import requests
5
+ import io
6
+ from PIL import Image
7
+ import imageio
8
+ import numpy as np
9
+ import base64
10
+
11
+ # إعداد API
12
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
13
+
14
+ def query(payload, api_key):
15
+ headers = {"Authorization": f"Bearer {api_key}"}
16
+ response = requests.post(API_URL, headers=headers, json=payload)
17
+ return response.content
18
+
19
+ def generate_video(video_description, api_key):
20
+ # إعداد القائمة لتخزين الصور
21
+ image_list = []
22
+
23
+ # الحصول على الصور بناءً على الوصف
24
+ image_bytes = query({
25
+ "inputs": "Create 9 sequential images with 1:1 aspect ratio for every one of images of a " + video_description,
26
+ }, api_key)
27
+
28
+ # فتح الصورة باستخدام PIL
29
+ image = Image.open(io.BytesIO(image_bytes))
30
+
31
+ # قائمة الإحداثيات لقص الصورة إلى 9 أجزاء
32
+ coordinates = [
33
+ (0, 340, 0, 340),
34
+ (340, 680, 0, 340),
35
+ (680, 1020, 0, 340),
36
+ (0, 340, 340, 680),
37
+ (340, 680, 340, 680),
38
+ (680, 1020, 340, 680),
39
+ (0, 340, 680, 1020),
40
+ (340, 680, 680, 1020),
41
+ (680, 1020, 680, 1020),
42
+ ]
43
+
44
+ # قص الأجزاء وإضافتها إلى image_list
45
+ for (x1, x2, y1, y2) in coordinates:
46
+ cropped_image = image.crop((x1, y1, x2, y2))
47
+ image_list.append(np.array(cropped_image)) # تحويل الصورة إلى مصفوفة NumPy
48
+
49
+ # تكرار الصور للحصول على عدد أكبر لإنتاج الفيديو
50
+ image_list_extended = image_list * 10 # تكرار 10 مرات للحصول على فيديو أطول
51
+
52
+ # إنشاء الفيديو في الذاكرة باستخدام imageio
53
+ output_buffer = io.BytesIO()
54
+ with imageio.get_writer(output_buffer, format='mp4', mode='I', fps=2) as writer:
55
+ for img_array in image_list_extended:
56
+ writer.append_data(img_array) # إضافة المصفوفة مباشرة
57
+
58
+ output_buffer.seek(0) # العودة إلى بداية الملف
59
+ return output_buffer.getvalue() # إرجاع بيانات الفيديو
60
+
61
+ # إعداد واجهة المستخدم باستخدام Gradio
62
+ def main(video_description, api_key):
63
+ # التحقق من وجود مفتاح API
64
+ if not api_key:
65
+ return "يرجى إدخال مفتاح API الخاص بك."
66
+
67
+ # إظهار رسالة التحميل
68
+ message = "Video is being created, please wait 3-4 minutes at most."
69
+
70
+ # إنشاء الفيديو
71
+ video_data = generate_video(video_description, api_key)
72
+
73
+ # تحويل الفيديو إلى Base64
74
+ video_base64 = base64.b64encode(video_data).decode('utf-8')
75
+
76
+ # إرجاع الفيديو
77
+ 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>'
78
+
79
+ return message, video_output
80
+
81
+ # واجهة Gradio
82
+ 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:
83
+ demo.launch()