-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMathReader.py
More file actions
209 lines (137 loc) · 5.7 KB
/
MathReader.py
File metadata and controls
209 lines (137 loc) · 5.7 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import re
import time
from transformers import T5ForConditionalGeneration, T5Tokenizer
import pandas as pd
from nemo.collections.tts.models import VitsModel
import pandas as pd
import soundfile as sf
import torch
from datasets import load_dataset
from pydub import AudioSegment
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
path = "Hyeonsieun/GTtoNT_addmoretoken_ver2"
tokenizer = T5Tokenizer.from_pretrained(path)
model = T5ForConditionalGeneration.from_pretrained(path)
model.to(device)
model.eval()
audio_generator = VitsModel.from_pretrained("tts_en_lj_vits")
latex_pattern = r"(\\\(.+?\\\)|\\\[.+?\\\])"
def T5_inference(text, model, tokenizer):
input_text = f"translate the LaTeX equation to a text pronouncing the formula: {text}"
inputs = tokenizer.encode(
input_text,
return_tensors='pt',
max_length=325,
padding='max_length',
truncation=True
).to(device)
# Get correct sentence ids.
corrected_ids = model.generate(
inputs,
max_length=325,
num_beams=5, # `num_beams=1` indicated temperature sampling.
early_stopping=True
)
# Decode.
corrected_sentence = tokenizer.decode(
corrected_ids[0],
skip_special_tokens=False
)
return corrected_sentence
def postprocessing_T5(GT_raw):
start_index = GT_raw.find("<pad>") + len("<pad>")
end_index = GT_raw.find("</s>")
GT_result = GT_raw[start_index:end_index]
return GT_result
def extract_text_and_latex(mmd_file_path):
with open(mmd_file_path, "r", encoding="utf-8") as file:
content = file.read()
sentence_list = re.split(latex_pattern, content)
return sentence_list
def process_latex_in_list(sentence_list):
replaced_list = []
for sentence in sentence_list:
if re.match(latex_pattern, sentence):
sentence_input = '$'+sentence[2:-2]+'$'
spoken_english = T5_inference(sentence_input, model, tokenizer)
spoken_english = postprocessing_T5(spoken_english)
replaced_list.append(spoken_english)
else:
replaced_list.append(sentence)
return replaced_list
def split_sentence_into_chunks(sentence, chunk_size=10):
words = sentence.split()
chunks = [words[i:i + chunk_size] for i in range(0, len(words), chunk_size)]
chunked_sentences = [' '.join(chunk) for chunk in chunks]
return chunked_sentences
print("-----------OCR start---------")
OCR_start = time.time()
os.system(
'nougat "/home/work/sieun/icassp2025/1.pdf" -o "test" ' # Write the path of the PDF file you want to perform OCR on.
)
OCR_end = time.time()
print("-----------OCR end---------")
OCR_time = OCR_end - OCR_start
print(f"OCR_time : {OCR_time}")
###############################################
print("-----------extract latex start---------")
extract_latex_start = time.time()
LaTeXs = []
mmd_foler_path = "test"
mmd_files = os.listdir(mmd_foler_path)
mmd_files = [os.path.join(mmd_foler_path, file) for file in mmd_files]
LaTeXs = []
for file in mmd_files:
LaTeXs = extract_text_and_latex(file)
extract_latex_end = time.time()
print("-----------extract latex end---------")
extract_latex_time = extract_latex_end - extract_latex_start
print(f"extract_latex_time : {extract_latex_time}")
###############################################
print("-----------LaTeX translate start---------")
LaTeX_translate_start = time.time()
spoken_english_list = process_latex_in_list(LaTeXs)
LaTeX_translate_end = time.time()
LaTeX_translate_time = LaTeX_translate_end - LaTeX_translate_start
print("-----------LaTeX translate end---------")
print(f"LaTeX translate time : {LaTeX_translate_time}")
###############################################
# Load VITS
print("-----------TTS start---------")
TTS_start = time.time()
spoken_english_whole_sentence = ' '.join(spoken_english_list)
spoken_english_whole_sentence = spoken_english_whole_sentence.replace('\n\n', ' ').replace('*', ' ').replace('#', ' ').replace('_', '')
print(spoken_english_whole_sentence)
spoken_english_seperated_list = split_sentence_into_chunks(spoken_english_whole_sentence)
for i, sentence in enumerate(spoken_english_seperated_list):
with torch.no_grad():
parsed = audio_generator.parse(sentence)
audio = audio_generator.convert_text_to_waveform(tokens=parsed)
# Save the audio to disk in a file called speech.wav
if isinstance(audio, torch.Tensor):
audio = audio.to("cpu").numpy()
sf.write(f"./test_audio/{i}.wav", audio.T, 22050, format="WAV")
folder_path = 'test_audio'
combined_audio = AudioSegment.empty()
wav_files = [f for f in os.listdir(folder_path) if f.endswith('.wav')]
def extract_number(file_name):
match = re.search(r'(\d+)', file_name)
return int(match.group()) if match else float('inf')
wav_files.sort(key=extract_number)
for file_name in wav_files:
file_path = os.path.join(folder_path, file_name)
audio = AudioSegment.from_wav(file_path)
combined_audio += audio
output_path = 'result_audio.wav'
combined_audio.export(output_path, format='wav')
TTS_end = time.time()
print("-----------TTS end---------")
TTS_time = TTS_end - TTS_start
print(f"TTS_time : {TTS_time}")
total_time = TTS_time + LaTeX_translate_time + extract_latex_time + OCR_time
print("-----------end-----------")
print(f"OCR_time : {OCR_time}")
print(f"GT2NT_time : {LaTeX_translate_time}")
print(f"TTS_time : {TTS_time}")
print(f"total: {total_time}")