TDN-M commited on
Commit
5c7e162
·
verified ·
1 Parent(s): 04ff90f

Delete components

Browse files
Files changed (3) hide show
  1. components/1.txt +0 -1
  2. components/pexels.py +0 -75
  3. components/utils.py +0 -19
components/1.txt DELETED
@@ -1 +0,0 @@
1
- e
 
 
components/pexels.py DELETED
@@ -1,75 +0,0 @@
1
- import requests
2
- import shutil
3
- import os
4
- import re
5
-
6
- # Searching for the videos
7
- def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoint='videos', num_pages=50):
8
- if orientation not in ['potrait', 'landscape', 'square']:
9
- raise Exception("Error! orientation must be one of {'square', 'landscape', 'potrait'}")
10
- if size not in ['medium', 'small', 'large']:
11
- raise Exception("Error! size must be one of ['medium', 'small', 'large']")
12
- base_url = 'https://api.pexels.com/'
13
- headers = {
14
- 'Authorization': f'{api_key}'
15
- }
16
- url = f'{base_url}{endpoint}/search?query={keyword}&per_page={num_pages}&orientation={orientation}&size={size}'
17
-
18
- response = requests.get(url, headers=headers)
19
- # Check if request was successful (status code 200)
20
- if response.status_code == 200:
21
- data = response.json()
22
- return data
23
- else:
24
- print(f'Error: {response.status_code}')
25
-
26
- # Video download function
27
- def download_video(data, parent_path, height, width, links, i):
28
- for x in data['videos']:
29
- if x['id'] in links:
30
- continue
31
-
32
- vid = x['video_files']
33
- for v in vid:
34
- if v['height'] == height and v['width'] == width:
35
- video_id = str(x['id'])
36
- video_path = os.path.join(parent_path, f"{i}_{video_id}.mp4")
37
- with open(video_path, 'wb') as f:
38
- f.write(requests.get(v['link']).content)
39
- print("Successfully saved video in", video_path)
40
- return video_id
41
-
42
- # Utilizing the LLMs to find the relevant videos
43
- def generate_videos(product, api_key, orientation, height, width, llm_chain=None, sum_llm_chain=None):
44
- prod = product.strip().replace(" ", "_")
45
- links = []
46
- try:
47
- # Split the paragraph by sentences
48
- sentences = llm_chain.run(product.strip())
49
- print('Sentences:', sentences)
50
-
51
- # Tách các câu từ văn bản
52
- sentences = [x.strip() for x in re.split(r'[.!?]', sentences) if len(x.strip()) > 6]
53
-
54
- # Sử dụng UUID để tạo tên thư mục ngắn gọn
55
- folder_name = f"video_{uuid.uuid4().hex}"
56
- os.makedirs(folder_name, exist_ok=True)
57
- folder_path = os.path.join(folder_name, "images")
58
- os.makedirs(folder_path, exist_ok=True)
59
-
60
- # Generate video for every sentence
61
- print("Keywords:")
62
- for i, s in enumerate(sentences):
63
- keyword = sum_llm_chain.run(s)
64
- print(i + 1, ":", keyword)
65
- data = search_pexels(keyword, api_key, orientation.lower())
66
- link = download_video(data, folder_path, height, width, links, i)
67
- if link:
68
- links.append(link)
69
-
70
- print("Success! Videos have been generated")
71
- except Exception as e:
72
- print("Error! Failed generating videos")
73
- print(e)
74
-
75
- return folder_name, sentences
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
components/utils.py DELETED
@@ -1,19 +0,0 @@
1
- import os
2
- import moviepy.editor as mp
3
-
4
- def combine_videos(folder_name):
5
- video_clips = []
6
- for file in sorted(os.listdir(folder_name)):
7
- if file.endswith(".mp4"):
8
- video_clips.append(mp.VideoFileClip(os.path.join(folder_name, file)))
9
-
10
- if not video_clips:
11
- print("No video clips found to combine.")
12
- return
13
-
14
- final_clip = mp.concatenate_videoclips(video_clips, method="compose")
15
- final_clip.write_videofile(os.path.join(folder_name, "Final_Ad_Video.mp4"), codec='libx264', audio_codec='aac')
16
- for clip in video_clips:
17
- clip.close()
18
- final_clip.close()
19
- print("Final video combined and saved as Final_Ad_Video.mp4")