Spaces:
Sleeping
Sleeping
Update generate_transcript.py
Browse files- generate_transcript.py +52 -17
generate_transcript.py
CHANGED
@@ -10,12 +10,12 @@ import warnings
|
|
10 |
warnings.filterwarnings('ignore')
|
11 |
|
12 |
|
13 |
-
class
|
14 |
"""
|
15 |
-
A class to generate
|
16 |
"""
|
17 |
|
18 |
-
def __init__(self, text_file_path, model_name="meta-llama/Llama-3.1-
|
19 |
"""
|
20 |
Initialize with the path to the cleaned text file and the model name.
|
21 |
|
@@ -24,7 +24,8 @@ class TranscriptGenerator:
|
|
24 |
model_name (str): Name of the language model to use.
|
25 |
"""
|
26 |
self.text_file_path = text_file_path
|
27 |
-
self.
|
|
|
28 |
self.model_name = model_name
|
29 |
self.accelerator = Accelerator()
|
30 |
self.model = transformers.pipeline(
|
@@ -33,19 +34,23 @@ class TranscriptGenerator:
|
|
33 |
model_kwargs={"torch_dtype": torch.bfloat16},
|
34 |
device_map="auto"
|
35 |
)
|
36 |
-
self.
|
37 |
-
You are a world-class podcast writer,
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
Speaker 1:
|
44 |
|
45 |
-
|
46 |
-
STRICTLY THE DIALOGUES.
|
47 |
"""
|
48 |
-
|
49 |
def load_text(self):
|
50 |
"""
|
51 |
Reads the cleaned text file and returns its content.
|
@@ -77,7 +82,7 @@ class TranscriptGenerator:
|
|
77 |
return None
|
78 |
|
79 |
messages = [
|
80 |
-
{"role": "system", "content": self.
|
81 |
{"role": "user", "content": input_text}
|
82 |
]
|
83 |
|
@@ -90,7 +95,37 @@ class TranscriptGenerator:
|
|
90 |
transcript = output[0]["generated_text"]
|
91 |
|
92 |
# Save the transcript as a pickle file
|
93 |
-
with open(self.
|
94 |
pickle.dump(transcript, f)
|
95 |
|
96 |
-
return self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
warnings.filterwarnings('ignore')
|
11 |
|
12 |
|
13 |
+
class TranscriptProcessor:
|
14 |
"""
|
15 |
+
A class to generate and rewrite podcast-style transcripts using a specified language model.
|
16 |
"""
|
17 |
|
18 |
+
def __init__(self, text_file_path, model_name="meta-llama/Llama-3.1-8B-Instruct"):
|
19 |
"""
|
20 |
Initialize with the path to the cleaned text file and the model name.
|
21 |
|
|
|
24 |
model_name (str): Name of the language model to use.
|
25 |
"""
|
26 |
self.text_file_path = text_file_path
|
27 |
+
self.transcript_output_path = './resources/data.pkl'
|
28 |
+
self.tts_output_path = './resources/podcast_ready_data.pkl'
|
29 |
self.model_name = model_name
|
30 |
self.accelerator = Accelerator()
|
31 |
self.model = transformers.pipeline(
|
|
|
34 |
model_kwargs={"torch_dtype": torch.bfloat16},
|
35 |
device_map="auto"
|
36 |
)
|
37 |
+
self.transcript_prompt = """
|
38 |
+
You are a world-class podcast writer, working as a ghost writer for top podcast hosts.
|
39 |
+
You will write the dialogue with engaging interruptions, anecdotes, and curiosity-led questions.
|
40 |
+
|
41 |
+
Speaker 1: Leads the conversation. Speaker 2: Asks follow-up questions and reacts with expressions.
|
42 |
+
|
43 |
+
ALWAYS START WITH SPEAKER 1: STRICTLY THE DIALOGUES.
|
44 |
+
"""
|
45 |
+
|
46 |
+
self.rewrite_prompt = """
|
47 |
+
You are an international oscar-winning screenwriter creating a refined script for TTS.
|
48 |
|
49 |
+
Speaker 1: Teaches with anecdotes; Speaker 2: Reacts with expressions like "umm," "hmm," [sigh].
|
50 |
|
51 |
+
Return the response as a list of tuples only, with no extra formatting.
|
|
|
52 |
"""
|
53 |
+
|
54 |
def load_text(self):
|
55 |
"""
|
56 |
Reads the cleaned text file and returns its content.
|
|
|
82 |
return None
|
83 |
|
84 |
messages = [
|
85 |
+
{"role": "system", "content": self.transcript_prompt},
|
86 |
{"role": "user", "content": input_text}
|
87 |
]
|
88 |
|
|
|
95 |
transcript = output[0]["generated_text"]
|
96 |
|
97 |
# Save the transcript as a pickle file
|
98 |
+
with open(self.transcript_output_path, 'wb') as f:
|
99 |
pickle.dump(transcript, f)
|
100 |
|
101 |
+
return self.transcript_output_path
|
102 |
+
|
103 |
+
def rewrite_transcript(self):
|
104 |
+
"""
|
105 |
+
Refines the transcript for TTS, adding expressive elements and saving as a list of tuples.
|
106 |
+
|
107 |
+
Returns:
|
108 |
+
str: Path to the file where the TTS-ready transcript is saved.
|
109 |
+
"""
|
110 |
+
# Load the initial generated transcript
|
111 |
+
with open(self.transcript_output_path, 'rb') as file:
|
112 |
+
input_transcript = pickle.load(file)
|
113 |
+
|
114 |
+
messages = [
|
115 |
+
{"role": "system", "content": self.rewrite_prompt},
|
116 |
+
{"role": "user", "content": input_transcript}
|
117 |
+
]
|
118 |
+
|
119 |
+
output = self.model(
|
120 |
+
messages,
|
121 |
+
max_new_tokens=8126,
|
122 |
+
temperature=1
|
123 |
+
)
|
124 |
+
|
125 |
+
rewritten_transcript = output[0]["generated_text"]
|
126 |
+
|
127 |
+
# Save the rewritten transcript as a pickle file
|
128 |
+
with open(self.tts_output_path, 'wb') as f:
|
129 |
+
pickle.dump(rewritten_transcript, f)
|
130 |
+
|
131 |
+
return self.tts_output_path
|