Spaces:
Runtime error
Runtime error
Commit
·
df22dca
1
Parent(s):
6eae4fb
Update components/pexels.py
Browse files- components/pexels.py +24 -10
components/pexels.py
CHANGED
|
@@ -46,22 +46,35 @@ def download_video(data, parent_path, height, width, links, i):
|
|
| 46 |
print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(v['id'])) + '.mp4')
|
| 47 |
return x['id']
|
| 48 |
|
| 49 |
-
def generate_voice(text, model, tokenizer):
|
| 50 |
speeches = []
|
| 51 |
-
for x in text:
|
| 52 |
-
inputs = tokenizer(x, return_tensors="pt")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Utilizing the LLMs to find the relevant videos
|
| 60 |
-
def generate_videos(text, api_key, orientation, height, width, model, tokenizer):
|
| 61 |
links = []
|
| 62 |
try :
|
| 63 |
# Split the paragraph by sentences
|
| 64 |
sentences = list(filter(None,[x.strip() for x in re.split(r'[^A-Za-z0-9 -]', text)]))
|
|
|
|
| 65 |
|
| 66 |
# Create directory with the name
|
| 67 |
di = str(datetime.datetime.now())
|
|
@@ -77,8 +90,9 @@ def generate_videos(text, api_key, orientation, height, width, model, tokenizer)
|
|
| 77 |
data = search_pexels(s, api_key, orientation.lower())
|
| 78 |
link = download_video(data, di, height, width, links,i)
|
| 79 |
links.append(link)
|
|
|
|
| 80 |
|
| 81 |
-
speeches = generate_voice(sentences, model, tokenizer)
|
| 82 |
|
| 83 |
sf.write("x.wav", torch.cat(speeches, 1)[0], 16500)
|
| 84 |
|
|
@@ -87,5 +101,5 @@ def generate_videos(text, api_key, orientation, height, width, model, tokenizer)
|
|
| 87 |
print("Error! Failed generating videos")
|
| 88 |
print(e)
|
| 89 |
|
| 90 |
-
return di, sentences
|
| 91 |
|
|
|
|
| 46 |
print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(v['id'])) + '.mp4')
|
| 47 |
return x['id']
|
| 48 |
|
| 49 |
+
def generate_voice(text, model, tokenizer, model2, tokenizer2, text_cls):
|
| 50 |
speeches = []
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
|
| 53 |
+
for x in text:
|
| 54 |
+
x = x+"."
|
| 55 |
+
if text_cls(x)[0]['label'][:4] == 'Indo':
|
| 56 |
+
inputs = tokenizer(x, return_tensors="pt")
|
| 57 |
+
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
output = model(**inputs).waveform
|
| 60 |
+
speeches.append(output)
|
| 61 |
+
|
| 62 |
+
else :
|
| 63 |
+
inputs = tokenizer2(x, return_tensors="pt")
|
| 64 |
+
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
output = model2(**inputs).waveform
|
| 67 |
+
speeches.append(output)
|
| 68 |
+
|
| 69 |
+
return speeches, [len(x)/16500 for x in speeches]
|
| 70 |
|
| 71 |
# Utilizing the LLMs to find the relevant videos
|
| 72 |
+
def generate_videos(text, api_key, orientation, height, width, model, tokenizer, model2, tokenizer2, text_cls):
|
| 73 |
links = []
|
| 74 |
try :
|
| 75 |
# Split the paragraph by sentences
|
| 76 |
sentences = list(filter(None,[x.strip() for x in re.split(r'[^A-Za-z0-9 -]', text)]))
|
| 77 |
+
# print(len(sentences))
|
| 78 |
|
| 79 |
# Create directory with the name
|
| 80 |
di = str(datetime.datetime.now())
|
|
|
|
| 90 |
data = search_pexels(s, api_key, orientation.lower())
|
| 91 |
link = download_video(data, di, height, width, links,i)
|
| 92 |
links.append(link)
|
| 93 |
+
|
| 94 |
|
| 95 |
+
speeches, length_speech = generate_voice(sentences, model, tokenizer, model2, tokenizer2, text_cls)
|
| 96 |
|
| 97 |
sf.write("x.wav", torch.cat(speeches, 1)[0], 16500)
|
| 98 |
|
|
|
|
| 101 |
print("Error! Failed generating videos")
|
| 102 |
print(e)
|
| 103 |
|
| 104 |
+
return di, sentences, length_speech
|
| 105 |
|