testingfaces commited on
Commit
19d65f6
Β·
verified Β·
1 Parent(s): ab5b46a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -14
app.py CHANGED
@@ -134,7 +134,10 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
134
  if job_id:
135
  _update_job(job_id, status="processing", step=step, message=message)
136
 
137
- out_dir = tempfile.mkdtemp()
 
 
 
138
  try:
139
  progress(1, "⏳ Step 1/5 β€” Denoising...")
140
  denoise1 = denoiser.process(
@@ -182,7 +185,11 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
182
  progress(5, "⏳ Step 5/5 β€” Summarizing...")
183
  summary = translator.summarize(transcript)
184
 
185
- with open(clean1, "rb") as f:
 
 
 
 
186
  enhanced_b64 = base64.b64encode(f.read()).decode("utf-8")
187
 
188
  result = {
@@ -192,7 +199,7 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
192
  "transcript": transcript,
193
  "translation": translation,
194
  "summary": summary,
195
- "audioPath": clean1,
196
  "enhancedAudio": enhanced_b64,
197
  "stats": {
198
  "language": detected_lang.upper(),
@@ -217,10 +224,19 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
217
 
218
  except Exception as e:
219
  logger.error(f"Pipeline failed: {e}", exc_info=True)
 
 
 
 
 
220
  err = {"status": "error", "message": f"❌ Error: {str(e)}"}
221
  if job_id:
222
  _update_job(job_id, **err)
223
  return err
 
 
 
 
224
 
225
 
226
  # ══════════════════════════════════════════════════════════════════════
@@ -465,17 +481,24 @@ async def api_process_url(request: _Request):
465
  _update_job(job_id, status="downloading", message="Downloading audio...")
466
 
467
  def _download_and_run():
468
- tmp_path = None
469
- audio_path = None
470
  try:
471
  resp = requests.get(audio_url, timeout=300, stream=True)
472
  resp.raise_for_status()
473
- url_lower = audio_url.lower()
474
- if "wav" in url_lower: suffix = ".wav"
475
- elif "mpeg" in url_lower: suffix = ".mpeg"
476
- elif "mp4" in url_lower: suffix = ".mp4"
477
- elif "m4a" in url_lower: suffix = ".m4a"
478
- else: suffix = ".mp3"
 
 
 
 
 
 
 
479
 
480
  fd, tmp_path = tempfile.mkstemp(suffix=suffix)
481
  os.close(fd)
@@ -491,11 +514,12 @@ async def api_process_url(request: _Request):
491
  _update_job(job_id, status="downloading",
492
  message=f"Downloading... {pct}%")
493
 
494
- audio_path = convert_to_wav(tmp_path)
 
495
 
496
  # FIX L11: plain function call
497
  run_pipeline(
498
- audio_path, src_lang, tgt_lang,
499
  opt_fillers, opt_stutters, opt_silences,
500
  opt_breaths, opt_mouth, job_id=job_id
501
  )
@@ -508,7 +532,8 @@ async def api_process_url(request: _Request):
508
  logger.error(f"Job {job_id} failed: {e}", exc_info=True)
509
  _update_job(job_id, status="error", message=f"❌ Error: {str(e)}")
510
  finally:
511
- for p in [tmp_path, audio_path]:
 
512
  try:
513
  if p and os.path.exists(p):
514
  os.unlink(p)
 
134
  if job_id:
135
  _update_job(job_id, status="processing", step=step, message=message)
136
 
137
+ out_dir = tempfile.mkdtemp()
138
+ # FIX: persistent output file outside out_dir so it survives cleanup
139
+ fd, final_audio_path = tempfile.mkstemp(suffix="_clearwave.wav")
140
+ os.close(fd)
141
  try:
142
  progress(1, "⏳ Step 1/5 β€” Denoising...")
143
  denoise1 = denoiser.process(
 
185
  progress(5, "⏳ Step 5/5 β€” Summarizing...")
186
  summary = translator.summarize(transcript)
187
 
188
+ # FIX: copy audio out of out_dir before we delete it in finally
189
+ import shutil as _shutil
190
+ _shutil.copy2(clean1, final_audio_path)
191
+
192
+ with open(final_audio_path, "rb") as f:
193
  enhanced_b64 = base64.b64encode(f.read()).decode("utf-8")
194
 
195
  result = {
 
199
  "transcript": transcript,
200
  "translation": translation,
201
  "summary": summary,
202
+ "audioPath": final_audio_path,
203
  "enhancedAudio": enhanced_b64,
204
  "stats": {
205
  "language": detected_lang.upper(),
 
224
 
225
  except Exception as e:
226
  logger.error(f"Pipeline failed: {e}", exc_info=True)
227
+ # FIX: clean up the persistent output file on error
228
+ try:
229
+ os.unlink(final_audio_path)
230
+ except Exception:
231
+ pass
232
  err = {"status": "error", "message": f"❌ Error: {str(e)}"}
233
  if job_id:
234
  _update_job(job_id, **err)
235
  return err
236
+ finally:
237
+ # FIX: always delete the temp working directory
238
+ import shutil as _shutil
239
+ _shutil.rmtree(out_dir, ignore_errors=True)
240
 
241
 
242
  # ══════════════════════════════════════════════════════════════════════
 
481
  _update_job(job_id, status="downloading", message="Downloading audio...")
482
 
483
  def _download_and_run():
484
+ tmp_path = None
485
+ converted_path = None # FIX: track convert_to_wav output separately
486
  try:
487
  resp = requests.get(audio_url, timeout=300, stream=True)
488
  resp.raise_for_status()
489
+ # FIX: detect suffix from Content-Type header first, fall back to URL
490
+ content_type = resp.headers.get("content-type", "")
491
+ if "wav" in content_type: suffix = ".wav"
492
+ elif "mpeg" in content_type: suffix = ".mp3"
493
+ elif "mp4" in content_type: suffix = ".mp4"
494
+ elif "aac" in content_type: suffix = ".aac"
495
+ else:
496
+ url_lower = audio_url.lower().split("?")[0] # strip query params
497
+ if url_lower.endswith(".wav"): suffix = ".wav"
498
+ elif url_lower.endswith(".mp4"): suffix = ".mp4"
499
+ elif url_lower.endswith(".m4a"): suffix = ".m4a"
500
+ elif url_lower.endswith(".mpeg"): suffix = ".mpeg"
501
+ else: suffix = ".mp3"
502
 
503
  fd, tmp_path = tempfile.mkstemp(suffix=suffix)
504
  os.close(fd)
 
514
  _update_job(job_id, status="downloading",
515
  message=f"Downloading... {pct}%")
516
 
517
+ # FIX: convert_to_wav may produce a *different* file; track it
518
+ converted_path = convert_to_wav(tmp_path)
519
 
520
  # FIX L11: plain function call
521
  run_pipeline(
522
+ converted_path, src_lang, tgt_lang,
523
  opt_fillers, opt_stutters, opt_silences,
524
  opt_breaths, opt_mouth, job_id=job_id
525
  )
 
532
  logger.error(f"Job {job_id} failed: {e}", exc_info=True)
533
  _update_job(job_id, status="error", message=f"❌ Error: {str(e)}")
534
  finally:
535
+ # FIX: delete both the download and the converted copy
536
+ for p in {tmp_path, converted_path}: # set deduplicates if equal
537
  try:
538
  if p and os.path.exists(p):
539
  os.unlink(p)