-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneration.py
More file actions
111 lines (90 loc) · 4.31 KB
/
generation.py
File metadata and controls
111 lines (90 loc) · 4.31 KB
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
import io
from openai import OpenAI
from pydub import AudioSegment
from pydantic import BaseModel
from elevenlabs import play
client = OpenAI(
api_key="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"
)
class Line(BaseModel):
text: str
voice: str
instruction: str
class Script(BaseModel):
script: list[Line]
def generate_podcast_script(articles, interest, model="gpt-4o-mini", temperature=0.7):
# news_articles = ""
# for idx, interest in enumerate(articles, 1):
# news_articles += f"{idx}. Topics: {interest[0]}\n"
# for idx, article in enumerate(interest[1], 1):
# news_articles += f" {idx}. Title{article.title}. Summary: {article.summary}\n"
prompt = """
You are a senior podcast writer.
I will provide a list of specific user interests, and each each interests is followed by a ranked list of news articles
related to that interest, ordered by editorial importance.
Task:
Generate a natural-sounding scripted two-host dialogue suitable for audio AI generation, tailored specifically
to the provided ranked articles and user interests.
### Output format
{
"script": [
{
"text": "<what the speaker will say>",
"voice": "<one of: alloy,ash,ballad,coral,echo,fable,nova,onyx,sage,shimmer>",
"instruction": "<stage direction for TTS engine - briefly describe:
- The speaker's tone or emotion
- Any relevant context (e.g. replying to previous speaker, introducing a guest)>"
},
…
]
}
### Writing rules
1. Begin with one host delivering a concise, engaging one-sentence show introduction.
2. Alternate dialogue evenly between two hosts—one male, one female—for variety and listener engagement.
3. Clearly introduce each user-interest segment with a conversational transition
(e.g., "Let's dive into today's AI headlines.").
4. Limit the text field to ≤ 60 words per dialogue entry for clarity and conversational flow.
5. Seamlessly connect different news segments with brief conversational transitions or responses
(e.g., "Interesting take, what's next?").
6. Maintain a friendly, informative, and conversational tone throughout.
7. Conclude the podcast script with a warm, joint sign-off from both hosts, encouraging listener
interaction or anticipation for the next episode.
"""
response = client.responses.parse(
model="gpt-4o",
instructions= prompt,
input=f"User prompt: {interest}\n Recommended article:\nstr(articles)",
text_format=Script
)
# print([turn for turn in response.output_parsed.script])
# exit()
return [turn for turn in response.output_parsed.script]
def synthesize_audio(article, interest):
script = generate_podcast_script(article, interest)
full_episode = AudioSegment.empty()
for line in script:
with client.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts",
voice=line.voice,
input=line.text,
instructions=line.instruction,
) as response:
audio_bytes = b"".join(response.iter_bytes(chunk_size=1024)) # raw MP3 bytes
segment = AudioSegment.from_file( # decode once into AudioSegment
io.BytesIO(audio_bytes), format="mp3"
)
full_episode += segment
full_episode.export("podcast_episode.mp3", format="mp3")
print("Podcast saved → podcast_episode.mp3")
if __name__ == "__main__":
# Example usage
sample_articles = {
"Artificial Intelligence": [
{"title": "OpenAI Introduces GPT-5", "summary": "The latest GPT-5 model boasts enhanced reasoning capabilities."},
{"title": "EU Proposes New AI Regulations", "summary": "EU drafts stricter rules governing AI deployment and transparency."}
],
"Renewable Energy": [
{"title": "Global Solar Capacity Hits Record High", "summary": "Solar energy sees unprecedented growth in global adoption."}
]
}
synthesize_audio(sample_articles)