-
Notifications
You must be signed in to change notification settings - Fork 0
/
FableForge.py
98 lines (78 loc) · 3.38 KB
/
FableForge.py
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
import os
import re
import time
import openai
openai.api_key = os.environ.get('OPENAI_API_KEY')
def ask_question(question):
response = input(question + " ")
return response
def generate_text_with_gpt3(prompt):
while True:
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
return response['choices'][0]['message']['content']
except openai.error.RateLimitError:
print("Rate limit exceeded, waiting for 60 seconds before retrying.")
time.sleep(60)
def generate_outline(prompt):
return generate_text_with_gpt3(prompt)
def generate_narration(prompt):
return generate_text_with_gpt3(prompt)
def get_input(question, example):
while True:
response = ask_question(question)
print(f'You answered: {response}')
user_satisfied = input('Is this correct? (yes/no) ')
if user_satisfied.lower() == 'yes':
return response
def is_roman_numeral(s):
s_split = s.split()
if not s_split:
return False
pattern = r'^[IVX]+'
match = re.match(pattern, s_split[0])
return match is not None
def generate_and_confirm(prompt, generator):
feedback_prompt = prompt
while True:
generated_text = generator(feedback_prompt)
print(f"Generated Text:\n{generated_text}\n")
user_feedback = input("If you want to revise this, please provide your feedback. Otherwise, type 'approve': ")
if user_feedback.lower() == 'approve':
return generated_text
else:
feedback_prompt += f"\nBased on the feedback, here's a revision: {user_feedback}."
print("Let's create your podcast script!")
subject = get_input("What's the subject of your podcast?", "e.g., the speakeasies of prohibtion era Chicago")
style = get_input("What style should the podcast be?", "e.g., formal, conversational, humorous")
tone = get_input("What tone do you prefer?", "e.g., serious, light-hearted, sarcastic")
while True:
length = get_input("How long should the podcast be in minutes?", "e.g., 30")
if length.isdigit():
length = int(length)
break
else:
print("Please enter a valid number for the length of the podcast.")
outline_prompt = f"We're creating a podcast on {subject}. The style is {style} and the tone is {tone}. The podcast should be {length} minutes long. Please provide a detailed outline for the episode."
outline = generate_and_confirm(outline_prompt, generate_outline)
sections = outline.split('\n')
narrations = []
for i, section in enumerate(sections):
if is_roman_numeral(section):
continue
print(f"\nProcessing section {i+1} of {len(sections)}: {section}")
section_prompt = f"Based on the following outline: '{section}', generate a {style} and {tone} narration for this podcast section."
section_narration = generate_and_confirm(section_prompt, generate_narration)
narrations.append(section_narration)
with open('outline.txt', 'w') as file:
for section in sections:
file.write(f"{section}\n")
with open('narration.txt', 'w') as file:
for i, narration in enumerate(narrations):
file.write(f"Section {i+1}:\n{narration}\n\n")