SepDevX commited on
Commit
bc04c37
·
verified ·
1 Parent(s): b8876cc

requirements.txt

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