Clearwave48 commited on
Commit
6ec3b40
·
verified ·
1 Parent(s): fe8bb50

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -11
main.py CHANGED
@@ -18,6 +18,7 @@ import json
18
  import time
19
  import tempfile
20
  import logging
 
21
  import requests
22
  import cloudinary
23
  import cloudinary.uploader
@@ -53,6 +54,17 @@ app.add_middleware(
53
  )
54
 
55
 
 
 
 
 
 
 
 
 
 
 
 
56
  def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
57
  opt_fillers=True, opt_stutters=True, opt_silences=True):
58
  """
@@ -72,7 +84,6 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
72
  fillers=opt_fillers,
73
  stutters=opt_stutters,
74
  long_silences=opt_silences,
75
- # ✅ breaths/mouth_sounds removed — handled by studio_sound=True
76
  )
77
  clean_audio = result["audio_path"]
78
  stats = {
@@ -116,7 +127,7 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
116
  try:
117
  upload_result = cloudinary.uploader.upload(
118
  clean_audio,
119
- resource_type="video", # audio files use "video"
120
  folder="clearwave_enhanced",
121
  )
122
  enhanced_url = upload_result["secure_url"]
@@ -124,6 +135,7 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
124
  except Exception as e:
125
  logger.error(f"Cloudinary failed: {e}")
126
 
 
127
  yield {
128
  "status": "done",
129
  "step": 4,
@@ -146,8 +158,9 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
146
  logger.error(f"Pipeline error: {e}", exc_info=True)
147
  yield {"status": "error", "message": f"Pipeline failed: {str(e)}"}
148
 
149
- # Cleanup
150
- denoiser.cleanup_temp_files(out_dir)
 
151
 
152
 
153
  @app.get("/api/health")
@@ -180,14 +193,13 @@ async def process_url(request: Request):
180
  resp = requests.get(audio_url, timeout=60, stream=True)
181
  resp.raise_for_status()
182
 
183
- # Detect format
184
  lower_url = audio_url.lower().split("?")[0]
185
  suffix = ".mp3"
186
- if ".opus" in lower_url: suffix = ".opus"
187
- elif ".ogg" in lower_url: suffix = ".ogg"
188
- elif ".aac" in lower_url: suffix = ".aac"
189
- elif ".m4a" in lower_url: suffix = ".m4a"
190
- elif ".wav" in lower_url: suffix = ".wav"
191
 
192
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
193
  for chunk in resp.iter_content(chunk_size=65536):
@@ -209,7 +221,7 @@ async def process_url(request: Request):
209
 
210
  try:
211
  os.unlink(tmp.name)
212
- except:
213
  pass
214
 
215
  return StreamingResponse(
 
18
  import time
19
  import tempfile
20
  import logging
21
+ import shutil
22
  import requests
23
  import cloudinary
24
  import cloudinary.uploader
 
54
  )
55
 
56
 
57
+ def _safe_cleanup(out_dir):
58
+ """Remove temp directory safely — never raises."""
59
+ try:
60
+ if hasattr(denoiser, 'cleanup_temp_files'):
61
+ denoiser.cleanup_temp_files(out_dir)
62
+ elif out_dir and os.path.exists(out_dir):
63
+ shutil.rmtree(out_dir, ignore_errors=True)
64
+ except Exception as e:
65
+ logger.warning(f"Cleanup warning (non-fatal): {e}")
66
+
67
+
68
  def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
69
  opt_fillers=True, opt_stutters=True, opt_silences=True):
70
  """
 
84
  fillers=opt_fillers,
85
  stutters=opt_stutters,
86
  long_silences=opt_silences,
 
87
  )
88
  clean_audio = result["audio_path"]
89
  stats = {
 
127
  try:
128
  upload_result = cloudinary.uploader.upload(
129
  clean_audio,
130
+ resource_type="video",
131
  folder="clearwave_enhanced",
132
  )
133
  enhanced_url = upload_result["secure_url"]
 
135
  except Exception as e:
136
  logger.error(f"Cloudinary failed: {e}")
137
 
138
+ # ✅ yield done INSIDE try so cleanup never interrupts it
139
  yield {
140
  "status": "done",
141
  "step": 4,
 
158
  logger.error(f"Pipeline error: {e}", exc_info=True)
159
  yield {"status": "error", "message": f"Pipeline failed: {str(e)}"}
160
 
161
+ finally:
162
+ # ✅ Cleanup is now in finally — runs after done is yielded, never crashes pipeline
163
+ _safe_cleanup(out_dir)
164
 
165
 
166
  @app.get("/api/health")
 
193
  resp = requests.get(audio_url, timeout=60, stream=True)
194
  resp.raise_for_status()
195
 
 
196
  lower_url = audio_url.lower().split("?")[0]
197
  suffix = ".mp3"
198
+ if ".opus" in lower_url: suffix = ".opus"
199
+ elif ".ogg" in lower_url: suffix = ".ogg"
200
+ elif ".aac" in lower_url: suffix = ".aac"
201
+ elif ".m4a" in lower_url: suffix = ".m4a"
202
+ elif ".wav" in lower_url: suffix = ".wav"
203
 
204
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
205
  for chunk in resp.iter_content(chunk_size=65536):
 
221
 
222
  try:
223
  os.unlink(tmp.name)
224
+ except Exception:
225
  pass
226
 
227
  return StreamingResponse(