File size: 3,968 Bytes
3f176c4
3e2f324
2242171
3e2f324
b110be1
b080a94
19599cb
3f176c4
 
3e2f324
2242171
c55bf8f
 
3e2f324
2242171
3e2f324
 
 
2242171
3e2f324
 
 
2242171
3e2f324
 
2242171
3e2f324
 
 
2242171
3e2f324
 
3f176c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9a06d9
3f176c4
 
 
 
 
 
 
2242171
ec24a25
2242171
1fe1ea5
2242171
1fe1ea5
 
bcbe59e
 
 
 
2242171
1fe1ea5
 
 
2242171
 
1fe1ea5
b080a94
3e2f324
2242171
 
 
 
 
 
a9a06d9
 
 
 
2242171
 
 
3e2f324
f9dc237
 
 
 
 
 
 
 
 
 
 
 
96b10cf
f9dc237
 
 
 
 
 
 
2242171
f9dc237
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from langchain_community.llms import HuggingFacePipeline 
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from components import caption_chain, tag_chain
from components import pexels, utils
import os, gc
import gradio as gr
import os
from moviepy.editor import VideoFileClip, concatenate_videoclips

# ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
model = AutoModelForSeq2SeqLM.from_pretrained("declare-lab/flan-alpaca-large")
tokenizer = AutoTokenizer.from_pretrained("declare-lab/flan-alpaca-large")

# ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
pipe = pipeline(
    'text2text-generation',
    model=model,
    tokenizer=tokenizer,
    max_length=120
)

# HuggingFacePipeline์„ ์‚ฌ์šฉํ•˜์—ฌ LLM ์ดˆ๊ธฐํ™”
local_llm = HuggingFacePipeline(pipeline=pipe)

# ์ฒด์ธ ๊ตฌ์„ฑ
llm_chain = caption_chain.chain(llm=local_llm)
sum_llm_chain = tag_chain.chain(llm=local_llm)

# Pexels API ํ‚ค
pexels_api_key = os.getenv('pexels_api_key')


def pred(product_name, orientation):
    # ๊ธฐ์กด ์ฝ”๋“œ ์ƒ๋žต...
    folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width, llm_chain, sum_llm_chain)
    video_files = [os.path.join(folder_name, f) for f in os.listdir(folder_name) if f.endswith('.mp4')]
    
    if not video_files:  # ๋น„๋””์˜ค ํŒŒ์ผ์ด ์—†์œผ๋ฉด ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
        return ["No videos were generated. Please check the input and try again.", ""]
    
    # ๋น„๋””์˜ค ํŒŒ์ผ ๊ฒฐํ•ฉ
    video_path = combine_videos(video_files, folder_name)
    if not video_path:  # ๋น„๋””์˜ค ๊ฒฐํ•ฉ์— ์‹คํŒจํ•˜๋ฉด ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
        return ["Failed to combine videos.", ""]
    
    return ["\n".join(sentences), video_path]

def combine_videos(video_files, output_folder):
    if not video_files:
        print("No video files to combine.")
        return ""  # ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
    
    clips = [VideoFileClip(vf) for vf in video_files]
    final_clip = concatenate_videoclips(clips)
    output_path = os.path.join(output_folder, "final_video.mp4")
    final_clip.write_videofile(output_path)
    return output_path
    
# ์˜ˆ์ธก ํ•จ์ˆ˜
def pred(product_name, orientation):
    # ๋น„๋””์˜ค ๋ฐฉํ–ฅ๊ณผ ํ•ด์ƒ๋„ ์„ค์ •
    if orientation == "Shorts/Reels/TikTok (1080 x 1920)":
        orientation = "portrait"  # ์˜คํƒ€ ์ˆ˜์ •
        height = 1920
        width = 1080
    elif orientation == "Youtube Videos (1920 x 1080)":
        orientation = "landscape"
        height = 1080
        width = 1920
    else:
        orientation = "square"
        height = 1080
        width = 1080

    # ๋น„๋””์˜ค ์ƒ์„ฑ ๋ฐ ๋ฌธ์žฅ ์ถ”์ถœ
    folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width, llm_chain, sum_llm_chain)
    gc.collect()

    # ๋น„๋””์˜ค ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
    if not sentences:
        return ["No videos generated. Please try again.", ""]

    # ๋น„๋””์˜ค ํŒŒ์ผ ๊ฒฐํ•ฉ

    video_path = combine_videos(video_files, folder_name)
    if not video_path or not os.path.exists(video_path):
        return ["Failed to combine videos.", ""]
    
    return ["\n".join(sentences), video_path]

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ๋ฐ ๋Ÿฐ์นญ
with gr.Blocks() as demo:
    gr.Markdown(
        """
        # Ads Generator
        Create video ads based on your product name using AI
        ### Note: the video generation takes about 2-4 minutes 
        """
    )
    dimension = gr.Dropdown(
        ["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"], 
        label="Video Dimension", info="Choose dimension"
    )
    product_name = gr.Textbox(label="Product name")
    captions = gr.Label(label="Captions")
    video = gr.Video()
    btn = gr.Button("Submit")
    btn.click(pred, inputs=[product_name, dimension], outputs=[captions, video])

    # ์—ฌ๊ธฐ์— ์ถ”๊ฐ€์ ์ธ Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# Gradio ์•ฑ ์‹คํ–‰
demo.launch()