which part from this code are responsible for losing information in the end results?
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
nlp = spacy.load('en_core_web_trf')
tokenizer = T5Tokenizer.from_pretrained('SJ-Ray/Re-Punctuate')
model = TFT5ForConditionalGeneration.from_pretrained('SJ-Ray/Re-Punctuate')
def export_to_srt(audio_file, segments):
srt_file_en = os.path.splitext(audio_file)[0] + ".en.srt"
results_en = []
with open(srt_file_en, "w", encoding="utf-8") as f_en:
for i, segment in enumerate(segments):
print(f"Processing segment {i+1}/{len(list(segments))}")
results_en.append(f"{i + 1}\n")
results_en.append(f"{segment.start} --> {segment.end}\n")
# Apply the rules for writing and reading to the text
text = segment.text.strip()
# # Remove all punctuation marks, periods, and whitespace
# text = re.sub(r'[^\w\s]|\n', '', text)
# text = text.strip().replace('\n', ' ')
# print(f"Cleaned text: {text}")
# Tokenize the cleaned text using T5Tokenizer
tokenized_text = tokenizer(text, padding="max_length", truncation=True, max_length=512, return_tensors="tf")
print(f"Tokenized text: {tokenized_text}")
# Generate the predicted output using TFT5ForConditionalGeneration
output = model.generate(input_ids=tokenized_text["input_ids"], attention_mask=tokenized_text["attention_mask"])
print(f"Generated output: {output}")
# Decode the predicted output using T5Tokenizer
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(f"Decoded output: {output_text}")
# Split the output text into two lines if needed
if "," in output_text and any(word in output_text.lower() for word in ["because", "so"]):
split_output_text = output_text.split(",")
fixed_text_en = split_output_text[0].strip().capitalize() + ","
fixed_text_en += f" {split_output_text[1].strip().capitalize()}."
else:
# Capitalize the first letter of the output sentence
fixed_text_en = output_text.strip().capitalize()
print(f"Fixed text: {fixed_text_en}")
# Use spacy to split the sentence into two lines if needed
doc = nlp(fixed_text_en)
split_output_text = [sent.text.strip().capitalize() for sent in doc.sents]
fixed_text_en = "\n".join(split_output_text)
print(f"Split text: {fixed_text_en}")
# Add the fixed_text to the results
results_en.append(f"{fixed_text_en}\n\n")
f_en.writelines(results_en)
print(f"Subtitles exported to {srt_file_en}")
# Read the contents of the SRT file
with open(srt_file_en, "r", encoding="utf-8") as f:
contents = f.read()
# Print the contents to the console
print(contents)