-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
125 lines (104 loc) · 4 KB
/
main.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# NewsX
#
# Firstly getting the news title, content and metadata from a news API,
# then summarizing it to have a certain amount of words (in order to reduce video runtime)
# Using elevenlabs text-to-speech to get an audio version of the news
#
# Implementing Wav2Lip lip-sync by the response audio received from tts
#
# Final video generated will be uploaded to YT using Data API v3 and GCP auth
#
# Since the entire pipeline requires each task to be resolved before being completed
# and due to various API monetary restrictions, we'll upload videos
# imports
import requests
import os
from dotenv import load_dotenv
import json
from summarizer import Summarizer
# youtube upload api
from youtube_upload.client import YoutubeUploader
load_dotenv()
CHUNK_SIZE=1024
# Text-to-speech, audio saved to output.mp3 at root level of the directory,, to be used for text-to-avatar
def make_audio(summarizedcontent):
key = os.getenv("ELEVENLABS_API_KEY")
# voice ID of american ground news reporter
voice_id = "5Q0t7uMcjvnagumLfvZi"
url = "https://api.elevenlabs.io/v1/text-to-speech/"+voice_id
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": key
}
# for hindi news switch to multilingual model, check if v1 or v2 works better
data = {
"text": summarizedcontent,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}
response = requests.post(url, json=data, headers=headers)
with open('output.mp3', 'wb') as f:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
f.write(chunk)
# Summarizing text
def summarized_news(text):
# bert model summarizer
model = Summarizer()
# need to make sure ratio is not getting neglected,
# if so, use num_sentences=10 so that lapses in words do not occur
result = model(text, ratio=0.2)
full = ''.join(result)
return full
# calling news API and getting the content of a sample news article (ex: on blockchain)
def get_news():
key = os.getenv("NEWS_API_KEY")
url = "https://newsdata.io/api/1/news?"
# latest news from past 24 hours from US for testing (to get english articles)
# look to get indian news / hindi articles as well
params = {
"country": "us",
"timeframe": "24"
}
headers = {
"X-ACCESS-KEY": key,
"Content-Type": "application/json"
}
# Make a get request with the parameters.
response = requests.get(url, params=params, headers=headers)
data = response.text
convert_json = json.loads(data)
articles = convert_json['results']
# returning the articles dict
return articles
def upload_video(file_path):
uploader = YoutubeUploader(secrets_file_path="./client_secret_610949113764-emvq9t18ac0sg7qr614dou9d9tng000l.apps.googleusercontent.com.json")
uploader.authenticate() # use access token and refresh token here in parameters
# Video options
options = {
"title": "Automated uploading video to YouTube", # Video title
"description": "Sample upload to YT via Python", # Video description
"tags": ["news", "india", "recent"],
"categoryId": "22",
"privacyStatus": "private", # Video privacy. Can either be "public", "private", or "unlisted"
"kids": False, # Specifies if the Video if for kids or not. Defaults to False.
# "thumbnailLink": "https://cdn.havecamerawilltravel.com/photographer/files/2020/01/youtube-logo-new-1068x510.jpg"
# Optional. Specifies video thumbnail.
}
# upload video
uploader.upload(file_path, options)
def main():
# # get the dict of articles
# news_articles = get_news()
# # summarizing topmost article
# summ = summarized_news(news_articles[0]['content'])
# # text to speech audio generated
# make_audio(summ)
file_path = "./sample_vid.mp4"
upload_video(file_path)
if __name__ == '__main__':
main()