Update app.py
Browse files
app.py
CHANGED
@@ -32,11 +32,15 @@ processor = Wav2Vec2Processor.from_pretrained(MODEL_NAME)
|
|
32 |
model = AutoModelForCTC.from_pretrained(MODEL_NAME)
|
33 |
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
-
audio,
|
38 |
input_values = processor(audio, sampling_rate=16000, return_tensors="pt").input_values
|
39 |
-
|
40 |
with torch.no_grad():
|
41 |
logits = model(input_values).logits
|
42 |
predicted_ids = torch.argmax(logits, dim=-1)
|
@@ -47,117 +51,130 @@ def transcribe(audio_file):
|
|
47 |
|
48 |
|
49 |
def transcribe_with_status(audio_file):
|
|
|
50 |
if audio_file is None:
|
51 |
-
return "", "", "่ซๅ
ไธๅณ้ณ่จ (Please upload audio first)"
|
52 |
-
|
|
|
|
|
53 |
transcription = transcribe(audio_file)
|
54 |
-
|
|
|
55 |
|
56 |
|
|
|
|
|
|
|
|
|
57 |
def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
|
|
|
58 |
try:
|
59 |
-
audio_metadata = {}
|
60 |
-
audio_file_url = None
|
61 |
|
62 |
-
# If an audio file is provided, upload it to Firebase Storage
|
63 |
if audio_file and os.path.exists(audio_file):
|
64 |
audio, sr = librosa.load(audio_file, sr=44100)
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
# Create a blob and upload the file
|
74 |
-
blob = bucket.blob(destination_path)
|
75 |
blob.upload_from_filename(audio_file)
|
76 |
-
|
77 |
-
# Generate a signed download URL valid for 1 hour (adjust expiration as needed)
|
78 |
audio_file_url = blob.generate_signed_url(expiration=timedelta(hours=1))
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
},
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
},
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
},
|
94 |
-
|
95 |
-
|
96 |
-
}
|
97 |
-
# Save data to a collection for that language
|
98 |
-
db.collection('paiwan_transcriptions').add(combined_data)
|
99 |
return "ๆ กๆญฃไฟๅญๆๅ! (Correction saved successfully!)"
|
100 |
except Exception as e:
|
101 |
return f"ไฟๅญๅคฑ่ดฅ: {e} (Error saving correction: {e})"
|
102 |
|
103 |
|
104 |
def prepare_download(audio_file, original_transcription, corrected_transcription):
|
|
|
105 |
if audio_file is None:
|
106 |
return None
|
107 |
|
108 |
-
tmp_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
|
109 |
-
|
110 |
-
with zipfile.ZipFile(tmp_zip.name, "w") as zf:
|
111 |
if os.path.exists(audio_file):
|
112 |
zf.write(audio_file, arcname="audio.wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
-
orig_txt = "original_transcription.txt"
|
115 |
-
with open(orig_txt, "w", encoding="utf-8") as f:
|
116 |
-
f.write(original_transcription)
|
117 |
-
zf.write(orig_txt, arcname="original_transcription.txt")
|
118 |
-
os.remove(orig_txt)
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
zf.write(corr_txt, arcname="corrected_transcription.txt")
|
124 |
-
os.remove(corr_txt)
|
125 |
-
return tmp_zip.name
|
126 |
|
|
|
|
|
127 |
|
128 |
-
|
129 |
-
"""Switch UI text between English and Traditional Chinese"""
|
130 |
-
if switch:
|
131 |
-
return (
|
132 |
-
"ๆ็ฃ่ช่ชๅ่ช้ณ่ญๅฅ้ๅญ็จฟ่ไฟฎๆญฃ็ณป็ตฑ", # Title
|
133 |
-
"ๆญฅ้ฉ 1๏ผ้ณ่จไธๅณ่้ๅญ็จฟ", # Step 1
|
134 |
-
"ๆญฅ้ฉ 2๏ผๅฏฉ้ฑ่็ทจ่ผฏ้ๅญ็จฟ", # Step 2
|
135 |
-
"ๆญฅ้ฉ 3๏ผไฝฟ็จ่
่ณ่จ", # Step 3
|
136 |
-
"ๆญฅ้ฉ 4๏ผๅฒๅญ่ไธ่ผ", # Step 4
|
137 |
-
"้ณ่จ่ผธๅ
ฅ", "็ข็้ๅญ็จฟ", # Audio label, Transcribe button
|
138 |
-
"ๅๅง้ๅญ็จฟ", "ๆดๆญฃ้ๅญ็จฟ", # Textboxes
|
139 |
-
"ๅนด้ฝก", "ๆฏ่ชๆ็ฃ่ชไฝฟ็จ่
?", # Age, Native speaker?
|
140 |
-
"ๅฒๅญ", "ๅฒๅญ็ๆ
", # Save button, Save status
|
141 |
-
"ไธ่ผ ZIP ๆชๆก", # Download button
|
142 |
-
"่็ไธญ๏ผ่ซ็จๅโฆ" # Status message default (Chinese)
|
143 |
-
)
|
144 |
-
else:
|
145 |
return (
|
146 |
-
"
|
147 |
-
"
|
148 |
-
"
|
149 |
-
"Step 3
|
150 |
-
"
|
151 |
-
"
|
152 |
-
"
|
153 |
-
"
|
154 |
-
"
|
155 |
-
"
|
156 |
-
"
|
|
|
|
|
|
|
|
|
157 |
)
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
-
# Interface
|
161 |
with gr.Blocks() as demo:
|
162 |
lang_switch = gr.Checkbox(label="ๅๆๅฐ็น้ซไธญๆ (Switch to Traditional Chinese)")
|
163 |
|
@@ -190,61 +207,60 @@ with gr.Blocks() as demo:
|
|
190 |
download_button = gr.Button()
|
191 |
download_output = gr.File()
|
192 |
|
193 |
-
# Initialize
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
download_button.value = download_label
|
213 |
-
status_message.value = default_status
|
214 |
-
|
215 |
-
# Language switcher
|
216 |
lang_switch.change(
|
217 |
toggle_language,
|
218 |
inputs=lang_switch,
|
219 |
-
outputs=[title, step1, step2, step3, step4,
|
220 |
-
|
221 |
-
|
|
|
222 |
)
|
223 |
|
224 |
-
#
|
225 |
audio_input.change(
|
226 |
transcribe_with_status,
|
227 |
inputs=audio_input,
|
228 |
outputs=[original_text, corrected_text, status_message]
|
229 |
)
|
230 |
|
231 |
-
# Manual transcription button
|
232 |
transcribe_button.click(
|
233 |
transcribe_with_status,
|
234 |
inputs=audio_input,
|
235 |
outputs=[original_text, corrected_text, status_message]
|
236 |
)
|
237 |
|
|
|
238 |
save_button.click(
|
239 |
store_correction,
|
240 |
inputs=[original_text, corrected_text, audio_input, age_input, native_speaker_input],
|
241 |
-
outputs=save_status
|
242 |
)
|
243 |
|
|
|
244 |
download_button.click(
|
245 |
prepare_download,
|
246 |
inputs=[audio_input, original_text, corrected_text],
|
247 |
-
outputs=download_output
|
248 |
)
|
249 |
|
250 |
demo.launch()
|
|
|
32 |
model = AutoModelForCTC.from_pretrained(MODEL_NAME)
|
33 |
|
34 |
|
35 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
36 |
+
# Core ASR helper functions
|
37 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
38 |
+
|
39 |
+
def transcribe(audio_file: str):
|
40 |
+
"""Run ASR on the uploaded audio file and return the raw transcription."""
|
41 |
try:
|
42 |
+
audio, _ = librosa.load(audio_file, sr=16000)
|
43 |
input_values = processor(audio, sampling_rate=16000, return_tensors="pt").input_values
|
|
|
44 |
with torch.no_grad():
|
45 |
logits = model(input_values).logits
|
46 |
predicted_ids = torch.argmax(logits, dim=-1)
|
|
|
51 |
|
52 |
|
53 |
def transcribe_with_status(audio_file):
|
54 |
+
"""Wrapper that provides UIโfriendly status messages."""
|
55 |
if audio_file is None:
|
56 |
+
return "", "", gr.update(value="่ซๅ
ไธๅณ้ณ่จ (Please upload audio first)", visible=True)
|
57 |
+
|
58 |
+
# Show processing message first
|
59 |
+
processing_msg = gr.update(value="่็ไธญ๏ผ่ซ็จๅโฆ (Processing, please waitโฆ)", visible=True)
|
60 |
transcription = transcribe(audio_file)
|
61 |
+
done_msg = gr.update(value="ๅฎๆ๏ผ(Done!)", visible=True)
|
62 |
+
return transcription, transcription, done_msg
|
63 |
|
64 |
|
65 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
66 |
+
# Firebase helpers
|
67 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
68 |
+
|
69 |
def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
|
70 |
+
"""Upload audio (if provided) + transcription pair to Firestore/Storage."""
|
71 |
try:
|
72 |
+
audio_metadata, audio_file_url = {}, None
|
|
|
73 |
|
|
|
74 |
if audio_file and os.path.exists(audio_file):
|
75 |
audio, sr = librosa.load(audio_file, sr=44100)
|
76 |
+
audio_metadata = {
|
77 |
+
"duration": librosa.get_duration(y=audio, sr=sr),
|
78 |
+
"file_size": os.path.getsize(audio_file),
|
79 |
+
}
|
80 |
+
uid = str(uuid.uuid4())
|
81 |
+
dst = f"audio/pai/{uid}.wav"
|
82 |
+
blob = bucket.blob(dst)
|
|
|
|
|
|
|
83 |
blob.upload_from_filename(audio_file)
|
|
|
|
|
84 |
audio_file_url = blob.generate_signed_url(expiration=timedelta(hours=1))
|
85 |
|
86 |
+
db.collection("paiwan_transcriptions").add({
|
87 |
+
"transcription_info": {
|
88 |
+
"original_text": original_transcription,
|
89 |
+
"corrected_text": corrected_transcription,
|
90 |
+
"language": "pai",
|
91 |
},
|
92 |
+
"audio_data": {
|
93 |
+
"audio_metadata": audio_metadata,
|
94 |
+
"audio_file_url": audio_file_url,
|
95 |
},
|
96 |
+
"user_info": {
|
97 |
+
"native_paiwan_speaker": native_speaker,
|
98 |
+
"age": age,
|
99 |
},
|
100 |
+
"timestamp": datetime.now().isoformat(),
|
101 |
+
"model_name": MODEL_NAME,
|
102 |
+
})
|
|
|
|
|
103 |
return "ๆ กๆญฃไฟๅญๆๅ! (Correction saved successfully!)"
|
104 |
except Exception as e:
|
105 |
return f"ไฟๅญๅคฑ่ดฅ: {e} (Error saving correction: {e})"
|
106 |
|
107 |
|
108 |
def prepare_download(audio_file, original_transcription, corrected_transcription):
|
109 |
+
"""Bundle audio + TXT files into a ZIP for download."""
|
110 |
if audio_file is None:
|
111 |
return None
|
112 |
|
113 |
+
tmp_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip").name
|
114 |
+
with zipfile.ZipFile(tmp_zip, "w") as zf:
|
|
|
115 |
if os.path.exists(audio_file):
|
116 |
zf.write(audio_file, arcname="audio.wav")
|
117 |
+
for name, content in [
|
118 |
+
("original_transcription.txt", original_transcription),
|
119 |
+
("corrected_transcription.txt", corrected_transcription),
|
120 |
+
]:
|
121 |
+
with open(name, "w", encoding="utf-8") as f:
|
122 |
+
f.write(content)
|
123 |
+
zf.write(name, arcname=name)
|
124 |
+
os.remove(name)
|
125 |
+
return tmp_zip
|
126 |
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
129 |
+
# Dynamic label switching โ uses gr.update() so values arenโt overwritten
|
130 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
131 |
|
132 |
+
def toggle_language(switch: bool):
|
133 |
+
"""Return a tuple of updates for each UI component when the language toggle flips."""
|
134 |
|
135 |
+
if switch: # Traditional Chinese UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
return (
|
137 |
+
"ๆ็ฃ่ช่ชๅ่ช้ณ่ญๅฅ้ๅญ็จฟ่ไฟฎๆญฃ็ณป็ตฑ", # Title (Markdown)
|
138 |
+
"ๆญฅ้ฉ 1๏ผ้ณ่จไธๅณ่้ๅญ็จฟ", # Step 1 (Markdown)
|
139 |
+
"ๆญฅ้ฉ 2๏ผๅฏฉ้ฑ่็ทจ่ผฏ้ๅญ็จฟ", # Step 2 (Markdown)
|
140 |
+
"ๆญฅ้ฉ 3๏ผไฝฟ็จ่
่ณ่จ", # Step 3 (Markdown)
|
141 |
+
"ๆญฅ้ฉ 4๏ผๅฒๅญ่ไธ่ผ", # Step 4 (Markdown)
|
142 |
+
gr.update(label="้ณ่จ่ผธๅ
ฅ"), # Audio component label
|
143 |
+
gr.update(value="็ข็้ๅญ็จฟ"), # Transcribe button text
|
144 |
+
gr.update(label="ๅๅง้ๅญ็จฟ"), # Original transcription textbox label
|
145 |
+
gr.update(label="ๆดๆญฃ้ๅญ็จฟ"), # Corrected transcription textbox label
|
146 |
+
gr.update(label="ๅนด้ฝก"), # Age slider label
|
147 |
+
gr.update(label="ๆฏ่ชๆ็ฃ่ชไฝฟ็จ่
?"), # Native speaker checkbox label
|
148 |
+
gr.update(value="ๅฒๅญ"), # Save button text
|
149 |
+
gr.update(label="ๅฒๅญ็ๆ
"), # Saveโstatus textbox label
|
150 |
+
gr.update(value="ไธ่ผ ZIP ๆชๆก"), # Download button text
|
151 |
+
gr.update(value="่็ไธญ๏ผ่ซ็จๅโฆ") # Status message default
|
152 |
)
|
153 |
|
154 |
+
# English UI
|
155 |
+
return (
|
156 |
+
"Paiwan ASR Transcription & Correction System",
|
157 |
+
"Step 1: Audio Upload & Transcription",
|
158 |
+
"Step 2: Review & Edit Transcription",
|
159 |
+
"Step 3: User Information",
|
160 |
+
"Step 4: Save & Download",
|
161 |
+
gr.update(label="Audio Input"),
|
162 |
+
gr.update(value="Generate Transcript"),
|
163 |
+
gr.update(label="Original Transcription"),
|
164 |
+
gr.update(label="Corrected Transcription"),
|
165 |
+
gr.update(label="Age"),
|
166 |
+
gr.update(label="Native Paiwan Speaker?"),
|
167 |
+
gr.update(value="Save"),
|
168 |
+
gr.update(label="Save Status"),
|
169 |
+
gr.update(value="Download ZIP File"),
|
170 |
+
gr.update(value="Processing, please waitโฆ"),
|
171 |
+
)
|
172 |
+
|
173 |
+
|
174 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
175 |
+
# Gradio UI
|
176 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
177 |
|
|
|
178 |
with gr.Blocks() as demo:
|
179 |
lang_switch = gr.Checkbox(label="ๅๆๅฐ็น้ซไธญๆ (Switch to Traditional Chinese)")
|
180 |
|
|
|
207 |
download_button = gr.Button()
|
208 |
download_output = gr.File()
|
209 |
|
210 |
+
# Initialize English labels
|
211 |
+
init_vals = toggle_language(False)
|
212 |
+
(title.value, step1.value, step2.value, step3.value, step4.value,
|
213 |
+
audio_input, transcribe_button, original_text, corrected_text,
|
214 |
+
age_input, native_speaker_input, save_button, save_status,
|
215 |
+
download_button, status_message_init) = init_vals
|
216 |
+
|
217 |
+
audio_input.label = "Audio Input"
|
218 |
+
transcribe_button.value = "Generate Transcript"
|
219 |
+
original_text.label = "Original Transcription"
|
220 |
+
corrected_text.label = "Corrected Transcription"
|
221 |
+
age_input.label = "Age"
|
222 |
+
native_speaker_input.label = "Native Paiwan Speaker?"
|
223 |
+
save_button.value = "Save"
|
224 |
+
save_status.label = "Save Status"
|
225 |
+
download_button.value = "Download ZIP File"
|
226 |
+
status_message.value = status_message_init.value
|
227 |
+
|
228 |
+
# Language switch โ wonโt overwrite component values anymore
|
|
|
|
|
|
|
|
|
229 |
lang_switch.change(
|
230 |
toggle_language,
|
231 |
inputs=lang_switch,
|
232 |
+
outputs=[title, step1, step2, step3, step4,
|
233 |
+
audio_input, transcribe_button, original_text, corrected_text,
|
234 |
+
age_input, native_speaker_input, save_button, save_status,
|
235 |
+
download_button, status_message]
|
236 |
)
|
237 |
|
238 |
+
# Auto transcription on upload
|
239 |
audio_input.change(
|
240 |
transcribe_with_status,
|
241 |
inputs=audio_input,
|
242 |
outputs=[original_text, corrected_text, status_message]
|
243 |
)
|
244 |
|
245 |
+
# Manual transcription button
|
246 |
transcribe_button.click(
|
247 |
transcribe_with_status,
|
248 |
inputs=audio_input,
|
249 |
outputs=[original_text, corrected_text, status_message]
|
250 |
)
|
251 |
|
252 |
+
# Save to Firebase
|
253 |
save_button.click(
|
254 |
store_correction,
|
255 |
inputs=[original_text, corrected_text, audio_input, age_input, native_speaker_input],
|
256 |
+
outputs=save_status,
|
257 |
)
|
258 |
|
259 |
+
# Download ZIP
|
260 |
download_button.click(
|
261 |
prepare_download,
|
262 |
inputs=[audio_input, original_text, corrected_text],
|
263 |
+
outputs=download_output,
|
264 |
)
|
265 |
|
266 |
demo.launch()
|