Created
May 15, 2024 20:01
-
-
Save pabik/39bce6db391414c34982e88e9446d3dd 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 | |
from gtts import gTTS | |
from pathlib import Path | |
import xml.etree.ElementTree as ET | |
# Set your DocsGPT API keys | |
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' | |
tts = gTTS(answer, lang='en', slow=False) # Create the speech | |
tts.save(audio_file_path) # Save the speech to a wav file | |
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