File size: 3,672 Bytes
dfbf35f
 
7764d0a
2bcd076
113c0ca
 
134c118
dfbf35f
 
6e951e6
dfbf35f
 
 
 
 
 
 
 
 
 
 
 
 
6e951e6
dfbf35f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df22dca
7764d0a
 
df22dca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfbf35f
 
df22dca
dfbf35f
 
 
113c0ca
df22dca
113c0ca
dfbf35f
7764d0a
 
 
 
 
dfbf35f
 
 
 
7764d0a
 
 
 
dfbf35f
df22dca
7764d0a
df22dca
7764d0a
 
dfbf35f
 
 
 
 
 
df22dca
dfbf35f
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
import requests
import shutil,os,re
import datetime
import torch
import soundfile as SF

from components.custom_llm import custom_chain

# Searching for the videos
def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoint='videos', num_pages=50, pages=4):
    
  if orientation not in ['potrait', 'landscape', 'square']:
    raise Exception("Error! orientation must be one of {'square', 'landscape', 'potrait'}")

  if size not in ['medium', 'small', 'large']:
    raise Exception("Error! size must be one of ['medium', 'small', 'large']")

  base_url = 'https://api.pexels.com/'

  headers = {
      'Authorization': f'{api_key}'
  }

  url = f'{base_url}{endpoint}/search?query={keyword}&per_page={num_pages}&orientation={orientation}&page={pages}'


  response = requests.get(url, headers=headers)

  # Check if request was successful (status code 200)
  if response.status_code == 200:
      data = response.json()
      return data
  else:
      print(f'Error: {response.status_code}')


# Video download function
def download_video(data, parent_path, height, width, links, i):
    for x in data['videos'] :
        if x['id'] in links:
            continue
        
        vid = x['video_files']
        for v in vid: 
            if v['height'] == height and v['width'] == width :
                with open(f"{os.path.join(parent_path,str(i) + '_' + str(v['id']))}.mp4", 'bw') as f:
                    f.write(requests.get(v['link']).content)
                    print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(v['id'])) + '.mp4')
                    return x['id']

def generate_voice(text, model, tokenizer, model2, tokenizer2, text_cls):
    speeches = []

    
    for x in text:
        x = x+"."
        if text_cls(x)[0]['label'][:4] == 'Indo':
            inputs = tokenizer(x, return_tensors="pt")
    
            with torch.no_grad():
                output = model(**inputs).waveform
            speeches.append(output)
            
        else :
            inputs = tokenizer2(x, return_tensors="pt")
    
            with torch.no_grad():
                output = model2(**inputs).waveform
            speeches.append(output)
            
    return speeches, [len(x)/16500 for x in speeches]

# Utilizing the LLMs to find the relevant videos 
def generate_videos(text, api_key, orientation, height, width, model, tokenizer, model2, tokenizer2, text_cls):
    links = []
    try :
        # Split the paragraph by sentences
        # sentences = list(filter(None,[x.strip() for x in re.split(r'[^A-Za-z0-9 -]', text)]))
        # print(len(sentences))
        sentences = list(filter(None,[x.strip() for x in re.split(r'[^A-Za-z -]', custom_chain().invoke(p))]))
        
        # Create directory with the name
        di = str(datetime.datetime.now())
        if os.path.exists(di):
            shutil.rmtree(di)
        os.mkdir(di)
        
        # Generate video for every sentence
        print("Keyword :")
        for i,s in enumerate(sentences):
            # keyword = sum_llm_chain.run(s)
            print(i+1, ":", s)
            data = search_pexels(s, api_key, orientation.lower())
            link = download_video(data, di, height, width, links,i)
            links.append(link)

        
        speeches, length_speech = generate_voice(sentences, model, tokenizer, model2, tokenizer2, text_cls)

        sf.write("x.wav", torch.cat(speeches, 1)[0], 16500)
            
        print("Success! videos has been generated")
    except Exception as e :
        print("Error! Failed generating videos")
        print(e)
    
    return di, sentences, length_speech