-
Notifications
You must be signed in to change notification settings - Fork 5
/
podcast.py
188 lines (172 loc) · 6.88 KB
/
podcast.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import logging
import speech
from os import path
import stringUtils as s
from pydub import *
import translateNews as trans
import textgeneration as tg
def generatePodcastDialogMP3Files(dialogName):
ff = open(dialogName, "r")
listOfFiles = []
talksIasmim = 0
talksGPT2 = 0
for line in ff:
if(line[0] == '1'):
mp3File = dialogName + str(talksIasmim) + "_iasmim.mp3"
listOfFiles.append(mp3File)
line = line[2:]
logging.info("IAsmim: " + line)
fSub = open("subtitles", "a")
fSub.write("\nIAsmim: " + line + "\n")
fSub.close()
logging.info("Salvando arquivo " + mp3File)
if path.exists(mp3File) == False:
speech.speechNews(line, mp3File, "iasmim")
talksIasmim += 1
else:
mp3File = dialogName + str(talksGPT2) + "_gpt2.mp3"
listOfFiles.append(mp3File)
line = line[2:]
if "%comentariogpt2" in line:
line = line.replace("%comentariogpt2%", "")
if path.exists("traduzidos/" + dialogName + "_gpt2_" + str(talksGPT2) + "ingles") == False:
gpt2Talk = trans.translateConsideringAPILimit(line, "pt_to_en")
logging.info("Tradução da GPT-2: " + gpt2Talk)
gpt2Talk += tg.generateText(gpt2Talk)
f = open("traduzidos/" + dialogName + "_gpt2_" + str(talksGPT2) + "ingles", "a")
f.write(gpt2Talk)
f.close()
else:
f = open("traduzidos/" + dialogName + "_gpt2_" + str(talksGPT2) + "ingles", "r")
gpt2Talk = f.readlines()
if(isinstance(gpt2Talk, list)):
gpt2Talk = gpt2Talk[0]
f.close()
if path.exists("traduzidos/" + dialogName + "_gpt2_" + str(talksGPT2) + "portugues") == False:
logging.info(gpt2Talk)
gpt2Talk = trans.translateConsideringAPILimit(gpt2Talk, "en_to_pt")
f = open("traduzidos/" + dialogName + "_gpt2_" + str(talksGPT2) + "portugues", "a")
f.write(gpt2Talk)
f.close()
else:
f = open("traduzidos/" + dialogName + "_gpt2_" + str(talksGPT2) + "portugues", "r")
gpt2Talk = f.readlines()
if(isinstance(gpt2Talk, list)):
gpt2Talk = gpt2Talk[0]
f.close()
talksGPT2 += 1
else:
gpt2Talk = line[2:]
logging.info("GPT-2: " + gpt2Talk)
fSub = open("subtitles", "a")
fSub.write("\nGPT-2: " + gpt2Talk + "\n")
fSub.close()
logging.info("Salvando arquivo " + mp3File)
if path.exists(mp3File) == False:
speech.speechNews(gpt2Talk, mp3File, "GPT2")
ff.close()
return listOfFiles
def generateHeadlines(dFinal):
headlines = ""
mp3File = "headlines_iasmim.mp3"
for i in range(3):
if "manchete" in dFinal[i]:
if(isinstance(dFinal[i]["manchete"], list)):
dFinal[i]["manchete"] = dFinal[i]["manchete"][0]
if(i < 2):
headlines += dFinal[i]["manchete"] + " , "
else:
headlines += dFinal[i]["manchete"]
logging.info("IAsmim: " + headlines)
fSub = open("subtitles", "a")
fSub.write("\nIAsmim: " + headlines)
fSub.write("\n")
fSub.close()
if path.exists(mp3File) == False:
speech.speechNews(headlines, mp3File, "iasmim")
return mp3File
import codecs
def generateNewsByID(dFinal, idNews):
talk = ""
mp3File = "news" + str(idNews) + "_iasmim.mp3"
logging.debug(dFinal[idNews]["texto original"])
with codecs.open("noticia", 'r', encoding='utf8') as fp:
line = fp.readline()
if '%texto traduzido%' in line:
if(isinstance(dFinal[idNews]["texto traduzido"], list)):
dFinal[idNews]["texto traduzido"] = dFinal[idNews]["texto traduzido"][0]
line = line.replace("%texto traduzido%", dFinal[idNews]["texto traduzido"])
talk += line
while line:
line = fp.readline()
if '%autor%' in line:
line = line.replace("%autor%", dFinal[idNews]["autor"])
if '%dia%' in line:
line = line.replace("%dia%", str(dFinal[idNews]["data"]["dia"]))
if '%mes%' in line:
line = line.replace("%mes%", str(dFinal[idNews]["data"]["mes"]))
if '%ano%' in line:
line = line.replace("%ano%", str(dFinal[idNews]["data"]["ano"]))
talk += line
logging.info("IAsmim: " + talk)
fSub = open("subtitles", "a")
fSub.write("\nIAsmim: " + talk)
fSub.write("\n")
fSub.close()
if path.exists(mp3File) == False:
speech.speechNews(talk, mp3File, "iasmim")
return mp3File
def generateGPT2Comment(prefix, idNews):
prefixToGPT2 = ""
mp3File = "comment" + str(idNews) + "_gpt2.mp3"
prefix = s.cleanSentence(prefix)
if path.exists("traduzidos/comentarios_gpt2_" + str(idNews) + "ingles") == False:
prefixToGPT2 = trans.translateConsideringAPILimit(prefix, "pt_to_en")
logging.info(prefixToGPT2)
text = tg.generateText(prefixToGPT2)
logging.info(text)
f = open("traduzidos/comentarios_gpt2_" + str(idNews) + "ingles", "a")
f.write(text)
f.close()
else:
f = open("traduzidos/comentarios_gpt2_" + str(idNews) + "ingles", "r")
text = f.readlines()
if(isinstance(text, list)):
text = text[0]
f.close()
if path.exists("traduzidos/comentarios_gpt2_" + str(idNews) + "portugues") == False:
text = trans.translateConsideringAPILimit(text, "en_to_pt")
logging.info(text)
f = open("traduzidos/comentarios_gpt2_" + str(idNews) + "portugues", "a")
f.write(text)
f.close()
else:
f = open("traduzidos/comentarios_gpt2_" + str(idNews) + "portugues", "r")
text = f.readlines()
if(isinstance(text, list)):
text = text[0]
f.close()
logging.info("GPT-2: " + text)
fSub = open("subtitles", "a")
fSub.write("\nGPT-2: " + text)
fSub.write("\n")
fSub.close()
if path.exists(mp3File) == False:
speech.speechNews(text, mp3File, "GPT2")
return mp3File
def concatenateMP3s(mp3List):
if path.exists("final.mp3"):
return
i = 0
for mp3 in mp3List:
if(i == 0):
finalmp3 = AudioSegment.from_mp3(mp3)
else:
if(mp3 != 'intro_music.mp3'):
finalmp3 += AudioSegment.from_mp3(mp3)
else:
finalmp3 += AudioSegment.from_mp3(mp3) - 3
i += 1
finalmp3.export("final.mp3", format="mp3")
#for mp3 in mp3List:
# os.remove(mp3)