-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevelopment.py
More file actions
173 lines (136 loc) · 6.52 KB
/
Copy pathdevelopment.py
File metadata and controls
173 lines (136 loc) · 6.52 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import re
from pathlib import Path
from newspaper import Article
from openai import OpenAI
def get_article(url):
article = Article(url)
article.download()
article.parse()
return article.text
def init_openai():
# Read the API key
api_key_path = 'files/api/api_key.txt'
api_org_path = 'files/api/api_org.txt'
with open(api_key_path, 'r') as file:
api_key = file.read().strip()
# Read the API organization key
with open(api_org_path, 'r') as file:
api_key2 = file.read().strip()
myclient = OpenAI(api_key=api_key, organization=api_key2)
return myclient
def create_summary(myclient, article):
response = myclient.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system",
"content": "You are a summary tool and you need to create a detail summary between 50 to 100 words. "},
{"role": "user", "content": f"Create a summary of this text{article}"}
]
)
return response.choices[0].message.content
def generate_summary_dict(text):
"""
Generates a dictionary of summaries from a given text.
Parameters:
text (str): A string containing summaries, each starting with 'summary_'.
Returns:
dict: A dictionary with each summary under a unique key.
"""
# Splitting the text into separate summaries using 'summary_' as a delimiter
split_summaries = re.split(r'\* summary_\d+: ', text)[1:]
# Initializing an empty dictionary
summaries = {}
# Assigning summaries to the dictionary
for i, summary in enumerate(split_summaries):
key = f'summary_{i + 1}'
summaries[key] = summary.strip() # Remove leading/trailing whitespace
return summaries
def get_transcript(file_path, myclient):
# speech to text
# this will take that voice file and turn it back to text
audio_file = open(file_path, "rb")
transcript = myclient.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print(transcript.text)
def generate_script(transcript_text, article_text, video_seconds, myclient):
prompt = (
f"Generate 1 video script based on the provided inputs:\n\n"
f"Transcript Text: {transcript_text}\n"
f"Article Summary: {article_text}\n"
f"Video Duration: {video_seconds} seconds\n\n"
"Guidelines:\n"
"1. Style Replication: Mimic language and sentence structure from the transcript.\n"
"2. Tone Emulation: Match the emotional tone of the transcript.\n"
"3. Creative Expansion: Incorporate themes from the article summary in a creative way.\n"
"4. Time Consideration: Ensure the scripts are suitable for the specified video duration.\n"
"5. Personality Reflection: Capture the speaker's personality traits.\n\n"
"Output Format:\n"
"[Script content for the first video based on the guidelines]\n"
)
prompt_2 = (f"Create a text ready to say that combines the following news article with the style and personality of the provided transcript. The text should be engaging, lively, and informative.\n\nNews Article:\n{article_text}\n\nTranscript Style:\n{transcript_text}\n\nGenerated Script:"
f"The text must have between 80 to 100 words.")
# Replace {transcript_text}, {article_text}, and {video_seconds} with your specific inputs.
response = myclient.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": prompt_2},
{"role": "user", "content": "Please generate the scripts."}
]
)
return response.choices[0].message.content
import re
def generate_script_dict(text):
"""
Generates a dictionary of scripts from a given text, removing newlines immediately following each script identifier.
Parameters:
text (str): A string containing scripts, each starting with '* script_' followed by a number.
Returns:
dict: A dictionary with each script under a unique key, with initial newlines removed.
"""
# Splitting the text into separate scripts using the original pattern
split_scripts = re.split(r'\* script_\d+: ', text)[1:]
# Initializing an empty dictionary
scripts = {}
# Assigning scripts to the dictionary, removing initial newlines
for i, script in enumerate(split_scripts):
key = f'script_{i + 1}'
# Remove leading newlines and whitespace
scripts[key] = script.lstrip('\n').strip()
return scripts
def generate_audio(transcript_text, article_text, myclient, video_seconds=100):
prompt = (
f"Generate 1 video script based on the provided inputs:\n\n"
f"Transcript Text: {transcript_text}\n"
f"Article Summary: {article_text}\n"
f"Video number of words: {video_seconds} words\n\n"
"Guidelines:\n"
"1. Style Replication: Mimic language and sentence structure from the transcript.\n"
"2. Tone Emulation: Match the emotional tone of the transcript.\n"
"3. Creative Expansion: Incorporate themes from the article summary in a creative way.\n"
"4. Words Consideration: Ensure the scripts are suitable for the specified number of words.\n"
"5. Personality Reflection: Capture the speaker's personality traits.\n\n"
"You must create a single line as it were a person talking. Do not add anything but the words of the person talking for the video"
"Output Format:\n"
"[Script content for the first video based on the guidelines, excluding any labels like 'Host:' or 'Speaker:']\n"
)
prompt_2 = (f"Create a text ready to say that combines the following news article with the style and personality of the provided transcript. The text should be engaging, lively, and informative.\n\nNews Article:\n{article_text}\n\nTranscript Style:\n{transcript_text}\n\nGenerated Script:"
f"The text must have between 80 to 100 words.")
# Replace {transcript_text}, {article_text}, and {video_seconds} with your specific inputs.
response = myclient.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": prompt_2},
{"role": "user", "content": "Please generate the text ready to say."}
]
)
return response.choices[0].message.content
def text_to_audio(myclient, speech):
speech_file_path = Path(__file__).parent / "speech.mp3"
response = myclient.audio.speech.create(
model="tts-1",
voice="shimmer",
input=speech
)
return response.stream_to_file(speech_file_path)