Last active
July 3, 2024 07:00
-
-
Save pkreissel/6ad6043936489f83880b462e71e45ac4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openai import OpenAI | |
import requests | |
import re | |
import os | |
from pydub import AudioSegment | |
from tqdm import tqdm | |
def getLatest(): | |
data = requests.get("https://volksverpetzer.de/wp-json/wp/v2/posts/", headers={'User-Agent': 'Voice'}) | |
#replace all html tags | |
content = data.json() | |
article = content[0]["content"]["rendered"] | |
slug = content[0]["slug"] | |
title = content[0]["title"]["rendered"] | |
article = re.sub(r'<[^>]*>', '', article) | |
return article, slug, title | |
def voiceArticle(article): | |
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
text_splitter = RecursiveCharacterTextSplitter( | |
chunk_size=4000, | |
chunk_overlap=0, | |
length_function=len | |
) | |
chunks = text_splitter.create_documents([article]) | |
for i, chunk in tqdm(enumerate(chunks)): | |
with client.audio.speech.with_streaming_response.create( | |
model="tts-1-hd", | |
voice="shimmer", | |
input=chunk.page_content | |
) as response: | |
response.stream_to_file("output{}.mp3".format(i)) | |
def combineVoices(): | |
files = [f for f in os.listdir() if f.endswith(".mp3")] | |
print(files) | |
#exclude output.mp3 | |
files = [f for f in files if f != "output.mp3"] | |
# sort files by name | |
files.sort(key=lambda x: int(x[6:-4])) | |
combined = AudioSegment.empty() | |
for file in files: | |
combined += AudioSegment.from_mp3(file) | |
combined.export("output.mp3", format="mp3") | |
for file in files: | |
os.remove(file) | |
def uploadVoice(slug, title): | |
# File path and access token | |
file_path = 'output.mp3' | |
access_token = os.environ["SOUNDCLOUD_ACCESS_TOKEN"] | |
headers = { | |
'accept': 'application/json; charset=utf-8', | |
'Authorization': 'OAuth {}'.format(access_token), | |
} | |
print(title) | |
files = { | |
'track[title]': (None, title), | |
'track[asset_data]': open(file_path, 'rb'), | |
"track[sharing]": (None, "public"), | |
"track[permalink]": (None, slug), | |
} | |
print(files) | |
response = requests.post('https://api.soundcloud.com/tracks', headers=headers, files=files) | |
track_info = response.json() | |
if response.status_code == 201: | |
print("Track uploaded successfully!") | |
print("Track URL:", track_info['permalink_url']) | |
else: | |
print("Failed to upload track:", response.content) | |
def main(): | |
article, slug, title = getLatest() | |
voiceArticle(article) | |
combineVoices() | |
uploadVoice(slug, title) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment