Spaces:
Runtime error
Runtime error
jonathanjordan21
commited on
Commit
•
7764d0a
1
Parent(s):
dfbf35f
Create Audio Video combnation
Browse files- app.py +14 -20
- components/caption_chain.py +0 -12
- components/pexels.py +25 -16
- components/tag_chain.py +0 -16
- components/utils.py +10 -1
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,26 +1,15 @@
|
|
1 |
from langchain.llms import HuggingFacePipeline
|
2 |
import torch
|
3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, AutoModelForSeq2SeqLM
|
4 |
|
5 |
-
from components import caption_chain, tag_chain
|
6 |
from components import pexels, utils
|
7 |
import os, gc
|
8 |
import gradio as gr
|
|
|
|
|
9 |
|
10 |
-
model =
|
11 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
12 |
-
|
13 |
-
pipe = pipeline(
|
14 |
-
'text2text-generation',
|
15 |
-
model=model,
|
16 |
-
tokenizer= tokenizer,
|
17 |
-
max_length=120
|
18 |
-
)
|
19 |
-
|
20 |
-
local_llm = HuggingFacePipeline(pipeline=pipe)
|
21 |
|
22 |
-
llm_chain = caption_chain.chain(llm=local_llm)
|
23 |
-
sum_llm_chain = tag_chain.chain(llm=local_llm)
|
24 |
|
25 |
pexels_api_key = os.getenv('pexels_api_key')
|
26 |
|
@@ -37,10 +26,15 @@ def pred(product_name, orientation):
|
|
37 |
orientation = "square"
|
38 |
height = 1080
|
39 |
width = 1080
|
40 |
-
folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width,
|
41 |
gc.collect()
|
42 |
utils.combine_videos(folder_name)
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
#{'video':os.path.join(folder_name, "Final_Ad_Video.mp4"),
|
45 |
# 'captions':"\n".join(sentences)}
|
46 |
|
@@ -48,8 +42,8 @@ def pred(product_name, orientation):
|
|
48 |
with gr.Blocks() as demo:
|
49 |
gr.Markdown(
|
50 |
"""
|
51 |
-
#
|
52 |
-
Create video
|
53 |
### Note : the video generation takes about 2-4 minutes
|
54 |
"""
|
55 |
)
|
@@ -57,7 +51,7 @@ with gr.Blocks() as demo:
|
|
57 |
["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"],
|
58 |
label="Video Dimension", info="Choose dimension"
|
59 |
)
|
60 |
-
product_name = gr.Textbox(label="
|
61 |
captions = gr.Textbox(label="captions")
|
62 |
video = gr.Video()
|
63 |
btn = gr.Button("Submit")
|
|
|
1 |
from langchain.llms import HuggingFacePipeline
|
2 |
import torch
|
|
|
3 |
|
|
|
4 |
from components import pexels, utils
|
5 |
import os, gc
|
6 |
import gradio as gr
|
7 |
+
from transformers import VitsModel, AutoTokenizer, pipeline
|
8 |
+
import torch
|
9 |
|
10 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-ind")
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-ind")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
|
|
13 |
|
14 |
pexels_api_key = os.getenv('pexels_api_key')
|
15 |
|
|
|
26 |
orientation = "square"
|
27 |
height = 1080
|
28 |
width = 1080
|
29 |
+
folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width, model, tokenizer)
|
30 |
gc.collect()
|
31 |
utils.combine_videos(folder_name)
|
32 |
+
|
33 |
+
vid = os.path.join(folder_name,"Final_Ad_Video.mp4")
|
34 |
+
spe = "x.wav"
|
35 |
+
|
36 |
+
utils.combine_audio_video(folder_name, vid, spe)
|
37 |
+
return ["\n".join(sentences), os.path.join(folder_name, "new_filename.mp4")]
|
38 |
#{'video':os.path.join(folder_name, "Final_Ad_Video.mp4"),
|
39 |
# 'captions':"\n".join(sentences)}
|
40 |
|
|
|
42 |
with gr.Blocks() as demo:
|
43 |
gr.Markdown(
|
44 |
"""
|
45 |
+
# Content [Video] Generator
|
46 |
+
Create a short video based on your text input using AI
|
47 |
### Note : the video generation takes about 2-4 minutes
|
48 |
"""
|
49 |
)
|
|
|
51 |
["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"],
|
52 |
label="Video Dimension", info="Choose dimension"
|
53 |
)
|
54 |
+
product_name = gr.Textbox(label="text story", lines=15, max_lines=100)
|
55 |
captions = gr.Textbox(label="captions")
|
56 |
video = gr.Video()
|
57 |
btn = gr.Button("Submit")
|
components/caption_chain.py
DELETED
@@ -1,12 +0,0 @@
|
|
1 |
-
from langchain import PromptTemplate
|
2 |
-
from langchain.chains import LLMChain
|
3 |
-
|
4 |
-
def chain(llm):
|
5 |
-
template = """Make 5 different advertisement captions about this product.
|
6 |
-
{product}
|
7 |
-
"""
|
8 |
-
|
9 |
-
prompt = PromptTemplate(template=template, input_variables=["product"])
|
10 |
-
|
11 |
-
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
12 |
-
return llm_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
components/pexels.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import requests
|
2 |
import shutil,os,re
|
|
|
3 |
|
4 |
# Searching for the videos
|
5 |
def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoint='videos', num_pages=50):
|
@@ -43,34 +44,42 @@ def download_video(data, parent_path, height, width, links, i):
|
|
43 |
print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(v['id'])) + '.mp4')
|
44 |
return x['id']
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Utilizing the LLMs to find the relevant videos
|
48 |
-
def generate_videos(
|
49 |
prod = product.strip().replace(" ", "_")
|
50 |
links = []
|
51 |
try :
|
52 |
# Split the paragraph by sentences
|
53 |
-
|
54 |
-
sentences = llm_chain.run(product.strip())
|
55 |
-
print('Sentence :', sentences)
|
56 |
-
|
57 |
-
# sentences = sentences.split(".")[:-1]
|
58 |
-
sentences = [x.strip() for x in re.split(r'\d+\.', sentences) if len(x) > 6]
|
59 |
-
|
60 |
|
61 |
-
# Create directory with the
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
# Generate video for every sentence
|
67 |
print("Keyword :")
|
68 |
for i,s in enumerate(sentences):
|
69 |
-
keyword = sum_llm_chain.run(s)
|
70 |
-
print(i+1, ":",
|
71 |
-
data = search_pexels(
|
72 |
-
link = download_video(data,
|
73 |
links.append(link)
|
|
|
|
|
|
|
|
|
74 |
|
75 |
print("Success! videos has been generated")
|
76 |
except Exception as e :
|
|
|
1 |
import requests
|
2 |
import shutil,os,re
|
3 |
+
import datetime
|
4 |
|
5 |
# Searching for the videos
|
6 |
def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoint='videos', num_pages=50):
|
|
|
44 |
print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(v['id'])) + '.mp4')
|
45 |
return x['id']
|
46 |
|
47 |
+
def generate_voice(text, model, tokenizer):
|
48 |
+
speeches = []
|
49 |
+
for x in text:
|
50 |
+
inputs = tokenizer(x, return_tensors="pt")
|
51 |
+
|
52 |
+
with torch.no_grad():
|
53 |
+
output = model(**inputs).waveform
|
54 |
+
speeches.append(output)
|
55 |
+
return speeches
|
56 |
|
57 |
# Utilizing the LLMs to find the relevant videos
|
58 |
+
def generate_videos(text, api_key, orientation, height, width, model, tokenizer):
|
59 |
prod = product.strip().replace(" ", "_")
|
60 |
links = []
|
61 |
try :
|
62 |
# Split the paragraph by sentences
|
63 |
+
sentences = list(filter(None,[x.strip() for x in re.split(r'[^A-Za-z0-9 -]', text)]))
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
# Create directory with the name
|
66 |
+
di = str(datetime.datetime.now())
|
67 |
+
if os.path.exists(di):
|
68 |
+
shutil.rmtree(di)
|
69 |
+
os.mkdir(di)
|
70 |
|
71 |
# Generate video for every sentence
|
72 |
print("Keyword :")
|
73 |
for i,s in enumerate(sentences):
|
74 |
+
# keyword = sum_llm_chain.run(s)
|
75 |
+
print(i+1, ":", s)
|
76 |
+
data = search_pexels(s, api_key, orientation.lower())
|
77 |
+
link = download_video(data, di, height, width, links,i)
|
78 |
links.append(link)
|
79 |
+
|
80 |
+
speeches = generate_voice(sentences, model, tokenizer)
|
81 |
+
|
82 |
+
sf.write("x.wav", torch.cat(speeches, 1)[0], 16500)
|
83 |
|
84 |
print("Success! videos has been generated")
|
85 |
except Exception as e :
|
components/tag_chain.py
DELETED
@@ -1,16 +0,0 @@
|
|
1 |
-
from langchain.prompts import PromptTemplate
|
2 |
-
from langchain.chains import LLMChain
|
3 |
-
|
4 |
-
def chain(llm):
|
5 |
-
sum_template = """what is the most significant actions or places or things, say it in at most 5 words :
|
6 |
-
|
7 |
-
{sentence}
|
8 |
-
|
9 |
-
|
10 |
-
"""
|
11 |
-
|
12 |
-
sum_prompt = PromptTemplate(template=sum_template, input_variables=["sentence"])
|
13 |
-
|
14 |
-
sum_llm_chain = LLMChain(prompt=sum_prompt, llm=llm)
|
15 |
-
|
16 |
-
return sum_llm_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
components/utils.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from moviepy.editor import VideoFileClip, concatenate_videoclips,vfx
|
|
|
2 |
import os
|
3 |
|
4 |
def combine_videos(folder_name):
|
@@ -20,4 +21,12 @@ def combine_videos(folder_name):
|
|
20 |
# else :
|
21 |
# combined = concatenate_videoclips([VideoFileClip(os.path.join(folder_name,x)).subclip(0,60//length) for x in os.listdir(folder_name)])
|
22 |
# combined.write_videofile(os.path.join(folder_name, "Ad_Video.mp4"))
|
23 |
-
print("Done! Your ads video has been created")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from moviepy.editor import VideoFileClip, concatenate_videoclips,vfx
|
2 |
+
from moviepy.editor import AudioFileClip, CompositeAudioClip
|
3 |
import os
|
4 |
|
5 |
def combine_videos(folder_name):
|
|
|
21 |
# else :
|
22 |
# combined = concatenate_videoclips([VideoFileClip(os.path.join(folder_name,x)).subclip(0,60//length) for x in os.listdir(folder_name)])
|
23 |
# combined.write_videofile(os.path.join(folder_name, "Ad_Video.mp4"))
|
24 |
+
print("Done! Your ads video has been created")
|
25 |
+
|
26 |
+
def combine_audio_video(folder_name, vid, speech):
|
27 |
+
videoclip = VideoFileClip(vid)
|
28 |
+
audioclip = AudioFileClip(speech)
|
29 |
+
|
30 |
+
new_audioclip = CompositeAudioClip([audioclip])
|
31 |
+
videoclip.audio = new_audioclip
|
32 |
+
videoclip.write_videofile(os.path.join(folder_name,"new_filename.mp4"))
|
requirements.txt
CHANGED
@@ -2,3 +2,6 @@ langchain
|
|
2 |
moviepy
|
3 |
transformers
|
4 |
torch
|
|
|
|
|
|
|
|
2 |
moviepy
|
3 |
transformers
|
4 |
torch
|
5 |
+
sentencepiece
|
6 |
+
accelerate
|
7 |
+
sacremoses
|