Last active
September 30, 2024 06:20
-
-
Save satish860/81d814a7b67f5a0c54ea410ba9c700ab to your computer and use it in GitHub Desktop.
Simple transcript for Newsletter
This file contains hidden or 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 | |
from pathlib import Path | |
from typing import List, Literal, Tuple | |
from openai import OpenAI | |
from pydantic import BaseModel | |
from pypdf import PdfReader | |
client = OpenAI() | |
class DialogueItem(BaseModel): | |
text: str | |
speaker: Literal["speaker-1", "speaker-2"] | |
class Dialogue(BaseModel): | |
scratchpad: str | |
dialogue: List[DialogueItem] | |
def extract_text_from_pdf(pdf_path: str) -> str: | |
with Path(pdf_path).open("rb") as f: | |
reader = PdfReader(f) | |
text = "\n\n".join([page.extract_text() for page in reader.pages if page.extract_text()]) | |
return text | |
def generate_dialogue(text: str) -> Dialogue: | |
prompt = f""" | |
Your task is to take the input text provided and turn it into a lively, engaging, informative podcast dialogue for the askJunior Daily Judgments podcast. Use a style similar to NPR. | |
Define all terms used carefully for a broad audience of listeners. | |
Here is the original input text: | |
<input_text> | |
{text} | |
</input_text> | |
First, carefully read through the input text and identify the main topics, key points, and any interesting facts or anecdotes. | |
Then, write a very long, engaging, informative podcast dialogue here, based on the key points and creative ideas. | |
Use a conversational tone and include any necessary context or explanations to make the content accessible to a general audience. | |
The dialogue should be between two speakers, alternating between "speaker-1" and "speaker-2". | |
Start the podcast with an introduction mentioning that this is the askJunior Daily Judgments podcast. | |
The podcast should have around 2000 words. | |
""" | |
response = client.chat.completions.create( | |
model="gpt-4-1106-preview", | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0.0, | |
) | |
dialogue_text = response.choices[0].message.content | |
dialogue_lines = dialogue_text.split('\n') | |
dialogue_items = [] | |
current_speaker = "speaker-1" | |
for line in dialogue_lines: | |
if line.strip(): | |
dialogue_items.append(DialogueItem(text=line.strip(), speaker=current_speaker)) | |
current_speaker = "speaker-2" if current_speaker == "speaker-1" else "speaker-1" | |
return Dialogue(scratchpad="", dialogue=dialogue_items) | |
def generate_audio(text: str, voice: str) -> bytes: | |
response = client.audio.speech.create( | |
model="tts-1", | |
voice=voice, | |
input=text | |
) | |
return response.content | |
def generate_podcast_transcript_and_audio(file_path: str) -> Tuple[str, bytes]: | |
pdf_text = extract_text_from_pdf(file_path) | |
print(f"Extracted text length: {len(pdf_text)}") | |
dialogue = generate_dialogue(pdf_text) | |
transcript = "" | |
audio_segments = [] | |
for item in dialogue.dialogue: | |
transcript += f"{item.speaker}: {item.text}\n\n" | |
voice = "alloy" if item.speaker == "speaker-1" else "echo" | |
audio_segment = generate_audio(item.text, voice) | |
audio_segments.append(audio_segment) | |
combined_audio = b''.join(audio_segments) | |
return transcript, combined_audio | |
def process_templates(file_path: str, template: str) -> Tuple[str, bytes]: | |
if template.lower() == 'podcast': | |
return generate_podcast_transcript_and_audio(file_path) | |
elif template.lower() == 'short summary': | |
return generate_short_summary_transcript_and_audio(file_path) | |
elif template.lower() == 'summary': | |
return generate_summary_transcript_and_audio(file_path) | |
else: | |
raise ValueError("Invalid template format. Please choose 'podcast', 'short summary', or 'summary'.") | |
def main(file_path: str, template: str): | |
transcript, combined_audio = process_templates(file_path, template) | |
# Save audio file | |
audio_file_name = f"output_audio_{template.lower().replace(' ', '_')}.mp3" | |
with open(audio_file_name, "wb") as file: | |
file.write(combined_audio) | |
print(f"Audio saved as {audio_file_name}") | |
# Save transcript | |
transcript_file_name = f"{template.lower().replace(' ', '_')}_transcript.txt" | |
with open(transcript_file_name, "w") as f: | |
f.write(transcript) | |
print(f"Transcript saved as {transcript_file_name}") | |
# Print transcript sections | |
print("\nTranscript sections:") | |
for i, section in enumerate(transcript.split("\n\n"), 1): | |
if section.strip(): | |
print(f"Section {i}:") | |
print(section.strip()) | |
print("---") | |
print(f"\nTotal transcript length: {len(transcript)} characters") | |
if __name__ == "__main__": | |
pdf_file_path = "path/to/your/pdf/file.pdf" # Replace with your PDF file path | |
template_type = "podcast" # Choose from: "podcast", "summary", "short summary" | |
main(pdf_file_path, template_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment