File size: 3,970 Bytes
9551276
3be135a
5e72808
5e68066
e66305d
8b87f76
162d15d
 
 
 
 
8b87f76
 
162d15d
8b87f76
5e72808
572ef72
9b5051b
 
 
 
 
 
8b87f76
8eef721
8b87f76
9b5051b
70e2653
9b5051b
572ef72
 
 
9b5051b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e68066
9b5051b
 
 
 
 
5e68066
70e2653
95edb23
9949ff6
c93e6a7
5d135b7
 
 
 
 
 
 
 
c93e6a7
c3a6074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c93e6a7
5d135b7
ddde924
5d135b7
 
 
 
c3a6074
5d135b7
 
bc69648
5d135b7
 
5b1af87
b016235
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import random
import gradio as gr
from moviepy.editor import VideoFileClip, CompositeVideoClip, ImageClip
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import subprocess

# Ensure ImageMagick is installed
def install_imagemagick():
    if not os.path.exists('/usr/bin/convert'):
        subprocess.run(['apt-get', 'update'])
        subprocess.run(['apt-get', 'install', '-y', 'imagemagick'])

install_imagemagick()

import os
from moviepy.editor import VideoFileClip, CompositeVideoClip, ImageClip
from PIL import Image, ImageDraw, ImageFont

def create_text_clip(text, fontsize=70, color='white', size=(1080, 1920)):
    font = ImageFont.truetype("arial.ttf", fontsize)
    img = Image.new('RGBA', size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    w, h = draw.textbbox((0, 0), text, font=font)[2:]
    draw.text(((size[0] - w) / 2, (size[1] - h) / 2), text, font=font, fill=color)
    return img

def process_video(video_path, text):
    if not os.path.isfile(video_path):
        raise FileNotFoundError(f"The file {video_path} does not exist.")
    
    video = VideoFileClip(video_path)
    
    # Calculate new width to maintain the 9:16 aspect ratio
    new_width = int(video.h * 9 / 16)
    
    # Resize video to maintain 9:16 aspect ratio
    video = video.resize(width=new_width)
    
    # Create a text image
    text_img = create_text_clip(text, fontsize=70, color='white', size=(new_width, video.h))
    
    # Convert the text image to an ImageClip
    text_clip = ImageClip(text_img).set_duration(video.duration)
    
    # Composite the video with the text clip
    final_clip = CompositeVideoClip([video, text_clip])
    
    # Save the final video
    final_clip.write_videofile("output_video.mp4", codec="libx264", fps=24)
    
    return "output_video.mp4"
    final_clip.write_videofile(output_path, codec="libx264")

    return output_path
    
# Additional inputs for the chat interface
additional_inputs = [
    gr.Dropdown(choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"], value="llama3-70b-8192", label="Model"),
    gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Temperature", info="Controls diversity of the generated text. Lower is more deterministic, higher is more creative."),
    gr.Slider(minimum=1, maximum=32192, step=1, value=4096, label="Max Tokens", info="The maximum number of tokens that the model can process in a single response.<br>Maximums: 8k for gemma 7b, llama 7b & 70b, 32k for mixtral 8x7b."),
    gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Top P", info="A method of text generation where a model will only consider the most probable next tokens that make up the probability p."),
    gr.Number(precision=0, value=42, label="Seed", info="A starting point to initiate generation, use 0 for random")
]

# Gradio interface with blocks and tabs
# Chat Interface
def create_chat_interface():
    return gr.ChatInterface(
        fn=generate_response,
        chatbot=gr.Chatbot(
            show_label=False,
            show_share_button=False,
            show_copy_button=True,
            likeable=True,
            layout="panel"
        ),
        additional_inputs=additional_inputs,
        title="YTSHorts Maker",
        description="Powered by GROQ, MoviePy, and other tools."
    )

# Main app definition
with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as demo:
    with gr.Tabs():
        # Chat Ta
        with gr.TabItem("Video Processing"):
            text_input = gr.Textbox(lines=5, label="Text (8 words max per line)")
            process_button = gr.Button("Process Video")
            video_output = gr.Video(label="Processed Video")

            process_button.click(
                fn=process_video,
                inputs=text_input,
                outputs=video_output,
            )

# Launch the Gradio interface
if __name__ == "__main__":
    demo.launch()