Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
|
| 4 |
# --- FIX FOR ALL PERMISSION ERRORS ---
|
| 5 |
# Set environment variables BEFORE importing torch or diffusers.
|
|
@@ -92,18 +93,22 @@ async def generate_audio_endpoint(request: AudioRequest):
|
|
| 92 |
audio_int16 = (audio * 32767).astype(np.int16)
|
| 93 |
write_wav(temp_file_path, sample_rate, audio_int16)
|
| 94 |
logger.info(f"Audio saved to temporary file: {temp_file_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
return FileResponse(
|
| 97 |
path=temp_file_path,
|
| 98 |
media_type='audio/wav',
|
| 99 |
filename=f"{prompt[:50].replace(' ', '_')}.wav",
|
| 100 |
-
background=
|
| 101 |
)
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
logger.error(f"Error during audio generation for prompt '{prompt}': {e}", exc_info=True)
|
| 105 |
if temp_file_path and os.path.exists(temp_file_path):
|
| 106 |
-
os.remove(temp_file_path)
|
| 107 |
raise HTTPException(status_code=500, detail=str(e))
|
| 108 |
|
| 109 |
@app.get("/")
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
from starlette.background import BackgroundTask # <-- IMPORT THE FIX
|
| 4 |
|
| 5 |
# --- FIX FOR ALL PERMISSION ERRORS ---
|
| 6 |
# Set environment variables BEFORE importing torch or diffusers.
|
|
|
|
| 93 |
audio_int16 = (audio * 32767).astype(np.int16)
|
| 94 |
write_wav(temp_file_path, sample_rate, audio_int16)
|
| 95 |
logger.info(f"Audio saved to temporary file: {temp_file_path}")
|
| 96 |
+
|
| 97 |
+
# ### THIS IS THE FIX ###
|
| 98 |
+
# Create a background task to delete the file AFTER the response is sent.
|
| 99 |
+
cleanup_task = BackgroundTask(os.remove, temp_file_path)
|
| 100 |
|
| 101 |
return FileResponse(
|
| 102 |
path=temp_file_path,
|
| 103 |
media_type='audio/wav',
|
| 104 |
filename=f"{prompt[:50].replace(' ', '_')}.wav",
|
| 105 |
+
background=cleanup_task # Use the background task here
|
| 106 |
)
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
logger.error(f"Error during audio generation for prompt '{prompt}': {e}", exc_info=True)
|
| 110 |
if temp_file_path and os.path.exists(temp_file_path):
|
| 111 |
+
os.remove(temp_file_path) # Clean up if something else goes wrong
|
| 112 |
raise HTTPException(status_code=500, detail=str(e))
|
| 113 |
|
| 114 |
@app.get("/")
|