TDN-M commited on
Commit
965d9f2
·
verified ·
1 Parent(s): fdebe4c

Update components/pexels.py

Browse files
Files changed (1) hide show
  1. components/pexels.py +16 -12
components/pexels.py CHANGED
@@ -32,10 +32,12 @@ def download_video(data, parent_path, height, width, links, i):
32
  vid = x['video_files']
33
  for v in vid:
34
  if v['height'] == height and v['width'] == width:
35
- with open(f"{os.path.join(parent_path, str(i) + '_' + str(v['id']))}.mp4", 'wb') as f:
 
 
36
  f.write(requests.get(v['link']).content)
37
- print("Successfully saved video in", os.path.join(parent_path, str(i) + '_' + str(v['id'])) + '.mp4')
38
- return x['id']
39
 
40
  # Utilizing the LLMs to find the relevant videos
41
  def generate_videos(product, api_key, orientation, height, width, llm_chain=None, sum_llm_chain=None):
@@ -46,13 +48,14 @@ def generate_videos(product, api_key, orientation, height, width, llm_chain=None
46
  sentences = llm_chain.run(product.strip())
47
  print('Sentences:', sentences)
48
 
49
- # sentences = sentences.split(".")[:-1]
50
- sentences = [x.strip() for x in re.split(r'\d+\.', sentences) if len(x) > 6]
51
 
52
- # Create directory with the product's name
53
- if os.path.exists(prod):
54
- shutil.rmtree(prod)
55
- os.mkdir(prod)
 
56
 
57
  # Generate video for every sentence
58
  print("Keywords:")
@@ -60,12 +63,13 @@ def generate_videos(product, api_key, orientation, height, width, llm_chain=None
60
  keyword = sum_llm_chain.run(s)
61
  print(i + 1, ":", keyword)
62
  data = search_pexels(keyword, api_key, orientation.lower())
63
- link = download_video(data, prod, height, width, links, i)
64
- links.append(link)
 
65
 
66
  print("Success! Videos have been generated")
67
  except Exception as e:
68
  print("Error! Failed generating videos")
69
  print(e)
70
 
71
- return prod, sentences
 
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):
 
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:")
 
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