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|
+
from glob import glob
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
output_file = "annotation.csv"
|
12 |
+
current_audio="Sample_verse.wav"
|
13 |
+
current_text = " ".join(str("طَيِّ السِّجِلِّ لِلْكُتُبِ").replace(" ", "_"))
|
14 |
+
|
15 |
+
audio_root = "./recordings_OI"
|
16 |
+
|
17 |
+
wav_files= list(glob(f"{audio_root}/*/*.wav"))
|
18 |
+
wav_files.sort()
|
19 |
+
txt_files=[x.replace(".wav",".txt") for x in wav_files]
|
20 |
+
|
21 |
+
aud_text = list(zip(wav_files, txt_files))
|
22 |
+
|
23 |
+
global index
|
24 |
+
with open(output_file, 'r') as f:
|
25 |
+
index = sum(1 for line in f)
|
26 |
+
|
27 |
+
def get_audio_path(aud_text):
|
28 |
+
for i in aud_text:
|
29 |
+
yield i
|
30 |
+
|
31 |
+
pattern = r'>(.*?)<span style=".*?">(.*?)</span>(.*?)</p>'
|
32 |
+
def read_txt(path):
|
33 |
+
with open(path,"r") as f:
|
34 |
+
x = f.readline().strip()
|
35 |
+
match = re.search(pattern, x)
|
36 |
+
if match is not None:
|
37 |
+
x_ = f"{match.group(1)}{match.group(2)}{match.group(3)}"
|
38 |
+
else:
|
39 |
+
x_ = x.replace("<p>", "").replace("</p>", "")\
|
40 |
+
.replace('<span style="color:red;">', "").replace("</span>", "")\
|
41 |
+
.replace('<h4>', "").replace("</h4>", "")
|
42 |
+
return x, x_
|
43 |
+
|
44 |
+
# file_path = "./Record Prompts - Extra.csv"
|
45 |
+
file_path = "./Record Prompts - Sheet3.csv"
|
46 |
+
|
47 |
+
texts_instructions = {}
|
48 |
+
|
49 |
+
texts_plain_instruction = {}
|
50 |
+
def get_separated(text):
|
51 |
+
return " ".join([x.replace(" "," ").replace(" ", "_") for x in text])\
|
52 |
+
.replace(" @ ","@")
|
53 |
+
|
54 |
+
return " ".join([x.replace(" ", " ").replace(" ", "_") if "@" not in x else x for x in text])
|
55 |
+
|
56 |
+
with open(file_path, "r") as f:
|
57 |
+
header = f.readline()
|
58 |
+
for line in f:
|
59 |
+
data = line.strip().split(",")
|
60 |
+
data = [d.replace('\n','').replace(" "," ") for d in data]
|
61 |
+
# text = f"{data[0]}{data[1]}{data[2]}"
|
62 |
+
text = f"{data[1]}{data[2]}{data[3]}"
|
63 |
+
# texts_instructions[text] = get_separated(data[0])
|
64 |
+
text = text.replace(" ", "")
|
65 |
+
texts_instructions[text] = get_separated(data[0])
|
66 |
+
|
67 |
+
|
68 |
+
with open(file_path, "r") as f:
|
69 |
+
header = f.readline()
|
70 |
+
for line in f:
|
71 |
+
data = line.strip().split(",")
|
72 |
+
data = [d.replace('\n','').replace(" "," ") for d in data]
|
73 |
+
# text = f"{data[0]}{data[1]}{data[2]}"
|
74 |
+
text = f"{data[1]}{data[2]}{data[3]}"
|
75 |
+
text = text.replace(" ", "")
|
76 |
+
texts_plain_instruction[text] = '<p style="font-family:"Traditional Arabic",font-size:150px;"><mark>{0}</mark></p>'.format(data[4])
|
77 |
+
|
78 |
+
def give_audio_text(gen=None):
|
79 |
+
global current_audio
|
80 |
+
global current_text
|
81 |
+
global aud_text_generator
|
82 |
+
global index, ai_annotation, plain_instruction
|
83 |
+
# Use the passed generator or default to the global one
|
84 |
+
if gen is None:
|
85 |
+
gen = aud_text_generator
|
86 |
+
try:
|
87 |
+
current_audio, current_text = next(gen)
|
88 |
+
current_text, x_ = read_txt(current_text)
|
89 |
+
ai_annotation = texts_instructions.get(x_.strip().replace(" ", "").replace("ۚ",""), get_separated(x_.strip()))
|
90 |
+
plain_instruction = texts_plain_instruction.get(x_.strip().replace(" ", "").replace("ۚ",""), "")
|
91 |
+
# x_ = " ".join(str(x_).replace(" ", "_"))
|
92 |
+
index +=1
|
93 |
+
return current_audio, current_audio, ai_annotation ,current_text, plain_instruction
|
94 |
+
|
95 |
+
except StopIteration:
|
96 |
+
aud_text_generator = get_audio_path(aud_text) # Reset the generator
|
97 |
+
current_audio = next(aud_text_generator) # Start from the beginning
|
98 |
+
print("Current Audio after reset: ", current_audio)
|
99 |
+
gr.Info("Annotation Completed! Thank you for your time. Please close the tab!", duration=0)
|
100 |
+
return current_audio, current_audio, current_text,current_text, current_text
|
101 |
+
|
102 |
+
def save_transcription(path, text, additional_errors=""):
|
103 |
+
with open(f"{output_file}", "a") as f:
|
104 |
+
f.write(f"{path}\t{text}\t{additional_errors}\n")
|
105 |
+
return gr.Button("Get New Audio/Save updated transcription",interactive=False)
|
106 |
+
|
107 |
+
def activate_button():
|
108 |
+
return gr.Button("Get New Audio/Save updated transcription",interactive=True)
|
109 |
+
|
110 |
+
def increase(num):
|
111 |
+
if num == len(aud_text):
|
112 |
+
return 1
|
113 |
+
return num + 1
|
114 |
+
|
115 |
+
start_index = 1
|
116 |
+
|
117 |
+
def begin_annotation(annotator):
|
118 |
+
os.makedirs(f"./annotations/{annotator}", exist_ok=True)
|
119 |
+
global output_file, index, current_audio, current_text, ai_annotation, plain_instruction, start_index
|
120 |
+
output_file = f"./annotations/{annotator}/annotation.csv"
|
121 |
+
with open(output_file, 'r') as f:
|
122 |
+
index = sum(1 for line in f)
|
123 |
+
start_index = index
|
124 |
+
aud_text_generator = get_audio_path(aud_text[index:])
|
125 |
+
current_audio, current_text = next(aud_text_generator)
|
126 |
+
current_text, x_ = read_txt(current_text)
|
127 |
+
ai_annotation = texts_instructions.get(x_.strip().replace(" ", "").replace("ۚ",""), get_separated(x_.strip()))
|
128 |
+
plain_instruction = texts_plain_instruction.get(x_.strip().replace(" ", "").replace("ۚ",""), "")
|
129 |
+
return gr.Column(visible=True), gr.Column(visible=False), \
|
130 |
+
start_index, current_audio,current_audio, current_text,plain_instruction, ai_annotation
|
131 |
+
|
132 |
+
aud_text_generator = get_audio_path(aud_text[index:])
|
133 |
+
current_audio, current_text = next(aud_text_generator)
|
134 |
+
current_text, x_ = read_txt(current_text)
|
135 |
+
ai_annotation = texts_instructions.get(x_.strip().replace(" ", "").replace("ۚ",""), get_separated(x_.strip()))
|
136 |
+
plain_instruction = texts_plain_instruction.get(x_.strip().replace(" ", "").replace("ۚ",""), "")
|
137 |
+
|
138 |
+
with gr.Blocks(title = "Transcription Instruction") as instruction:
|
139 |
+
gr.Markdown("""
|
140 |
+
<div align="center">
|
141 |
+
<h2>Quran Pronunciation Annotation</h2>
|
142 |
+
</div>
|
143 |
+
|
144 |
+
# **الغرض من العمل**
|
145 |
+
|
146 |
+
ستقوم بتحديد الأخطاء النطقية التي يقوم بها المتكلم أثناء نطقه للجمل، وفقًا للإرشادات التالية
|
147 |
+
|
148 |
+
# **خطوات العمل**
|
149 |
+
|
150 |
+
1. استمع بعناية للتسجيل الصوتي المرفق. تأكد من متابعة كل كلمة بدقة.
|
151 |
+
2. قبل البدء، اقرأ وصف الخطأ المطلوب (مثال: استبدال الفتحة بالكسرة، أو نطق الحرف بطريقة غير صحيحة).
|
152 |
+
3. افهم الجملة المتوقعة مع تمييز الخطأ المطلوب فيها باللون الأحمر.
|
153 |
+
4. استمع مجددًا للتسجيل الصوتي للتحقق مما إذا كان المتكلم قد قام بالخطأ النطقي المطلوب.
|
154 |
+
5. إذا لاحظت وجود أي أخطاء إضافية قام بها المتكلم ولم يتم تحديدها مسبقًا، قم بتوثيقها أيضًا.
|
155 |
+
6. راجع كل كلمة منطوقة لتحديد أي انحراف إضافي عن النطق الصحيح.
|
156 |
+
7. قم بتوثيق الأخطاء بطريقة منظمة وواضحة، مع الإشارة إلى نوع الخطأ وموقعه في الجملة.
|
157 |
+
8. إستخدم الرمز @ للإشارة إلى كل حرف أو حركة خاطئة.
|
158 |
+
9. بعد رمز @، قم بإضافة النسخة التي نُطقت بشكل خاطئ.
|
159 |
+
10. يوجد ثلاث أنواع للاخطاء: استبدال , حذف أو أضافة:
|
160 |
+
|
161 |
+
## **الاستبدال**
|
162 |
+
|
163 |
+
- إستبدال حرف بحرف أو حركة بحركة
|
164 |
+
- مثال استبدال العين بالحاء في العسر الي الحسر. تكتب ا ل ع@ح ُ س ر
|
165 |
+
|
166 |
+
## **حذف**
|
167 |
+
|
168 |
+
- حذف حرف أو حركة
|
169 |
+
- مثال حذف التشديد في أنَّ الي أنَ. تكتب إ ِ نّ@ َ. يوجد مسافة بعد ال@ في حالة الحذف.
|
170 |
+
|
171 |
+
## **إضافة**
|
172 |
+
|
173 |
+
- إضافة حرف أو حركة
|
174 |
+
- مثال إضافة حرف في العُسر الي العُوسر. تكتب ا ل ع ُ @و س ر. يوجد مسافة قبل ال @ في حالة الأضافة.
|
175 |
+
|
176 |
+
في حال وجود أي استفسارات أو مواجهة أي مشكلات، يمكنك التواصل عبر البريد الإلكتروني التالي:
|
177 |
+
omnia@lst.uni-saarland.de
|
178 |
+
|
179 |
+
|
180 |
+
شكراً جزيلاً لتعاونك والتزامك بالدقة
|
181 |
+
""")
|
182 |
+
|
183 |
+
with gr.Blocks(title = "Fix Transcription") as tool:
|
184 |
+
|
185 |
+
with gr.Column(visible=True) as annotation_name:
|
186 |
+
annotator_name = gr.Textbox(label="Annotator Name", placeholder="Enter your name", interactive=True)
|
187 |
+
begin_annotate = gr.Button("Begin Annotation", variant="primary")
|
188 |
+
|
189 |
+
with gr.Column(visible=False) as annotation_block:
|
190 |
+
progress = gr.Number(start_index, label = f"X of {len(aud_text)} audios" ,interactive=False)
|
191 |
+
audio = gr.Audio(label="Audio", interactive=False, value=current_audio, autoplay=True)
|
192 |
+
audio_file_name = gr.Textbox(value=current_audio, label="Audio File Name", interactive=False, visible=False)
|
193 |
+
|
194 |
+
gr.Markdown('<h3>Expected Transcription:</h3>')
|
195 |
+
original_text = gr.Markdown(value=current_text)
|
196 |
+
gr.Markdown('<h3>Error Details:</h3>')
|
197 |
+
original_annotation = gr.Markdown(f"{plain_instruction}")
|
198 |
+
|
199 |
+
transcription = gr.Textbox(label="Actual transcription (editable)", value=ai_annotation, interactive=True, rtl=True)
|
200 |
+
|
201 |
+
additional_errors = gr.Textbox(label="Additional Errors (optional)", value="", placeholder="Enter additional errors", interactive=True, rtl=True)
|
202 |
+
get_new_audio = gr.Button("Get New Audio/Save updated transcription",
|
203 |
+
variant="primary")
|
204 |
+
|
205 |
+
begin_annotate.click(begin_annotation, [annotator_name], \
|
206 |
+
[annotation_block, annotation_name, progress, audio, audio_file_name,\
|
207 |
+
original_text, original_annotation, transcription])
|
208 |
+
|
209 |
+
get_new_audio.click(save_transcription, [audio_file_name, transcription, additional_errors], [get_new_audio]).\
|
210 |
+
then(give_audio_text, outputs=[audio,audio_file_name, transcription, original_text, original_annotation]).\
|
211 |
+
then(activate_button, outputs=[get_new_audio]).then(increase,progress, progress)
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
demo = gr.TabbedInterface([instruction, tool], ["Instructions", "Annotate"])
|
216 |
+
|
217 |
+
if __name__ == "__main__":
|
218 |
+
demo.launch(share=True)
|
219 |
+
|
220 |
+
|