error correction
Browse files
app.py
CHANGED
@@ -40,6 +40,54 @@ def transcribe_both(audio_file):
|
|
40 |
processing_time = (datetime.now() - start_time).total_seconds()
|
41 |
return transcription, transcription, processing_time
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
def toggle_language(switch):
|
44 |
"""Switch UI text between English and Traditional Chinese"""
|
45 |
if switch:
|
@@ -103,10 +151,22 @@ with gr.Blocks() as demo:
|
|
103 |
save_button, save_status, download_button]
|
104 |
)
|
105 |
|
106 |
-
transcribe_button.click(
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
save_button.click(
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
download_button.click(
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
demo.launch()
|
|
|
40 |
processing_time = (datetime.now() - start_time).total_seconds()
|
41 |
return transcription, transcription, processing_time
|
42 |
|
43 |
+
def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
|
44 |
+
try:
|
45 |
+
audio_metadata = {}
|
46 |
+
if audio_file and os.path.exists(audio_file):
|
47 |
+
audio, sr = librosa.load(audio_file, sr=16000)
|
48 |
+
duration = librosa.get_duration(y=audio, sr=sr)
|
49 |
+
file_size = os.path.getsize(audio_file)
|
50 |
+
audio_metadata = {'duration': duration, 'file_size': file_size}
|
51 |
+
|
52 |
+
combined_data = {
|
53 |
+
'original_text': original_transcription,
|
54 |
+
'corrected_text': corrected_transcription,
|
55 |
+
'timestamp': datetime.now().isoformat(),
|
56 |
+
'audio_metadata': audio_metadata,
|
57 |
+
'model_name': MODEL_NAME,
|
58 |
+
'user_info': {
|
59 |
+
'native_amis_speaker': native_speaker,
|
60 |
+
'age': age
|
61 |
+
}
|
62 |
+
}
|
63 |
+
db.collection('transcriptions').add(combined_data)
|
64 |
+
return "校正保存成功! (Correction saved successfully!)"
|
65 |
+
except Exception as e:
|
66 |
+
return f"保存失败: {e} (Error saving correction: {e})"
|
67 |
+
|
68 |
+
def prepare_download(audio_file, original_transcription, corrected_transcription):
|
69 |
+
if audio_file is None:
|
70 |
+
return None
|
71 |
+
|
72 |
+
tmp_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
|
73 |
+
tmp_zip.close()
|
74 |
+
with zipfile.ZipFile(tmp_zip.name, "w") as zf:
|
75 |
+
if os.path.exists(audio_file):
|
76 |
+
zf.write(audio_file, arcname="audio.wav")
|
77 |
+
|
78 |
+
orig_txt = "original_transcription.txt"
|
79 |
+
with open(orig_txt, "w", encoding="utf-8") as f:
|
80 |
+
f.write(original_transcription)
|
81 |
+
zf.write(orig_txt, arcname="original_transcription.txt")
|
82 |
+
os.remove(orig_txt)
|
83 |
+
|
84 |
+
corr_txt = "corrected_transcription.txt"
|
85 |
+
with open(corr_txt, "w", encoding="utf-8") as f:
|
86 |
+
f.write(corrected_transcription)
|
87 |
+
zf.write(corr_txt, arcname="corrected_transcription.txt")
|
88 |
+
os.remove(corr_txt)
|
89 |
+
return tmp_zip.name
|
90 |
+
|
91 |
def toggle_language(switch):
|
92 |
"""Switch UI text between English and Traditional Chinese"""
|
93 |
if switch:
|
|
|
151 |
save_button, save_status, download_button]
|
152 |
)
|
153 |
|
154 |
+
transcribe_button.click(
|
155 |
+
transcribe_both,
|
156 |
+
inputs=audio_input,
|
157 |
+
outputs=[original_text, corrected_text]
|
158 |
+
)
|
159 |
|
160 |
+
save_button.click(
|
161 |
+
store_correction,
|
162 |
+
inputs=[original_text, corrected_text, audio_input, age_input, native_speaker_input],
|
163 |
+
outputs=save_status
|
164 |
+
)
|
165 |
|
166 |
+
download_button.click(
|
167 |
+
prepare_download,
|
168 |
+
inputs=[audio_input, original_text, corrected_text],
|
169 |
+
outputs=download_output
|
170 |
+
)
|
171 |
|
172 |
+
demo.launch()
|