Created
May 15, 2024 19:57
-
-
Save pabik/f8ded60d546e618157da0325369eab2e 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
import os | |
import requests | |
import openai | |
from pathlib import Path | |
import xml.etree.ElementTree as ET | |
# Set your OpenAI and DocsGPT API keys | |
OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY' | |
DOCSGPT_API_URL = 'https://gptcloud.arc53.com/api/answer' #Will differ for local deployment | |
DOCSGPT_API_KEY = 'YOUR_DOCSGPT_API_KEY' | |
# Input path to the summary plan .txt | |
input_path = 'sum.txt' | |
# Capture the name of the file (without extension) to create folders | |
file_name = Path(input_path).stem | |
text_folder = Path(f'{file_name}_text') | |
audio_folder = Path(f'{file_name}_audio') | |
# Create the folders | |
text_folder.mkdir(parents=True, exist_ok=True) | |
audio_folder.mkdir(parents=True, exist_ok=True) | |
# Read the content of the input file | |
with open(input_path, 'r', encoding='utf-8') as file: | |
plan_content = file.read() | |
# Parse the XML content | |
root = ET.fromstring(plan_content) | |
# Initialize the OpenAI client | |
openai.api_key = OPENAI_API_KEY | |
# Function to request DocsGPT API | |
def request_docsgpt(question): | |
headers = {'Content-Type': 'application/json; charset=utf-8'} | |
data = { | |
"question": question, | |
"api_key": DOCSGPT_API_KEY, | |
} | |
response = requests.post(DOCSGPT_API_URL, json=data, headers=headers) | |
response_json = response.json() | |
return response_json['answer'] | |
# Process each part in the plan | |
for part in root.findall('part'): | |
number = part.get('number') | |
title = part.find('title').text | |
content = part.find('content').text.strip() | |
# Create the question for DocsGPT API | |
question = f"{title} {content}" | |
# Request DocsGPT API | |
answer = request_docsgpt(question) | |
# Write the answer to a text file | |
text_file_path = text_folder / f'{number}.{title.replace(" ", "_")}.txt' | |
with open(text_file_path, 'w', encoding='utf-8') as text_file: | |
text_file.write(answer) | |
print(f"Generated text: {text_file_path}") | |
audio_file_path = audio_folder / f'{number}.{title.replace(" ", "_")}.mp3' | |
# Initialize the OpenAI client | |
client = openai.Client(api_key=OPENAI_API_KEY) | |
# Request text-to-speech conversion | |
response = client.audio.speech.create( | |
model="tts-1", | |
voice="alloy", | |
input=answer | |
) | |
# Stream the response to the output file | |
response.stream_to_file(audio_file_path) | |
print(f"Generated audio: {audio_file_path}") | |
print("Processing completed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment