File size: 2,875 Bytes
3ece7ec
58a9a08
931cc18
abc2889
e1a31f1
3ece7ec
58a9a08
fc222b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f9ed29
58a9a08
3ece7ec
931cc18
58a9a08
 
 
da73391
931cc18
4f9ed29
5ebd377
 
4f9ed29
5ebd377
 
 
 
4f9ed29
abc2889
5ebd377
4f9ed29
5ebd377
 
 
 
 
 
 
 
 
 
 
 
4f9ed29
abc2889
5ebd377
4f9ed29
abc2889
5ebd377
4f9ed29
 
5ebd377
4f9ed29
abc2889
 
4f9ed29
abc2889
 
5ebd377
abc2889
da73391
3ece7ec
58a9a08
69801fc
58a9a08
4f9ed29
abc2889
5ebd377
4f9ed29
 
da73391
4f9ed29
fc222b1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
import requests
import io
from PIL import Image as img
from moviepy.editor import ImageSequenceClip
import numpy as np

google_html = '''

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-CDY5VGM0FL"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-CDY5VGM0FL');
</script>

'''


# Setting up the API URL and headers
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"

def query(payload, api_key):
    headers = {"Authorization": f"Bearer {api_key}"}
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.content

def generate_video(video_description, api_key):
    # Setting up the list to store images
    image_list = []

    # Fetching the images based on the description
    image_bytes = query({
        "inputs": "Create 9 sequential images with 1:1 aspect ratio for every one of images of a " + video_description,
    }, api_key)

    # Opening the image using PIL
    image = img.open(io.BytesIO(image_bytes))

    # List of coordinates to crop the image into 9 parts
    coordinates = [
        (0, 340, 0, 340),  
        (340, 680, 0, 340), 
        (680, 1020, 0, 340),
        (0, 340, 340, 680),  
        (340, 680, 340, 680), 
        (680, 1020, 340, 680),
        (0, 340, 680, 1020),  
        (340, 680, 680, 1020), 
        (680, 1020, 680, 1020),
    ]

    # Cropping the parts and converting them to numpy arrays and adding them to image_list
    for i, (x1, x2, y1, y2) in enumerate(coordinates):
        cropped_image = image.crop((x1, y1, x2, y2))
        image_array = np.array(cropped_image)  # Converting the image to a numpy array
        image_list.append(image_array)

    # Repeating the images to get more frames for the video
    image_list_extended = image_list * 10  # Repeating 10 times to get a longer video

    # Creating the video from the list using moviepy
    clip = ImageSequenceClip(image_list_extended, fps=2)
    
    # Saving the video to a file
    output_path = "/tmp/output_video.mp4"
    clip.write_videofile(output_path, codec="libx264")

    return output_path

def main(video_description, api_key):
    if not api_key:
        return "Please Enter your Hugging Face API key."
    
    # Creating the video based on the input description
    video_path = generate_video(video_description, api_key)

    # Returning the video download link to the user
    return "The video has been created, you can download it now:", gr.File(video_path)

# Gradio interface
with gr.Interface(fn=main, inputs=[gr.HTML(google_html),gr.Textbox(label="Enter video description"), gr.Textbox(label="Enter your Hugging Face API key")], outputs=[gr.Textbox(), gr.File()]) as demo:
    demo.launch()