tecuts commited on
Commit
6f8785a
·
verified ·
1 Parent(s): b6886e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -5,10 +5,12 @@ import logging
5
  import requests
6
  import subprocess
7
  from fastapi import FastAPI, Request, HTTPException
 
8
  from fastapi.staticfiles import StaticFiles
9
  import tempfile
10
  import os
11
  import uuid
 
12
 
13
  # Constants
14
  BASE_URL = 'https://tecuts-request.hf.space'
@@ -81,15 +83,23 @@ async def fetch_data_for_url(request: Request):
81
  f.write(chunk)
82
  logging.info(f"FLAC file downloaded at {flac_file_path}")
83
 
84
- # Convert FLAC to ALAC
85
  alac_file_path = os.path.join(global_download_dir, f'{uuid.uuid4()}.m4a')
86
- subprocess.run(['ffmpeg', '-i', flac_file_path, '-c:a', 'alac', alac_file_path], check=True)
 
87
  logging.info(f"Converted to ALAC: {alac_file_path}")
88
 
 
 
 
 
89
  # Generate the file URL for ALAC
90
  alac_file_url = f'{BASE_URL}/file/{os.path.basename(alac_file_path)}'
91
  logging.info(f"Returning ALAC file URL: {alac_file_url}")
92
 
 
 
 
93
  # Return the ALAC file URL to the client as JSON
94
  return {"url": alac_file_url}
95
 
@@ -101,5 +111,5 @@ app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")
101
  async def set_mime_type_middleware(request: Request, call_next):
102
  response = await call_next(request)
103
  if request.url.path.endswith(".m4a"):
104
- response.headers["Content-Type"] = "audio/mp4"
105
  return response
 
5
  import requests
6
  import subprocess
7
  from fastapi import FastAPI, Request, HTTPException
8
+ from fastapi.responses import FileResponse
9
  from fastapi.staticfiles import StaticFiles
10
  import tempfile
11
  import os
12
  import uuid
13
+ import gc # Import the garbage collector module
14
 
15
  # Constants
16
  BASE_URL = 'https://tecuts-request.hf.space'
 
83
  f.write(chunk)
84
  logging.info(f"FLAC file downloaded at {flac_file_path}")
85
 
86
+ # Convert FLAC to ALAC using a context manager to ensure proper cleanup
87
  alac_file_path = os.path.join(global_download_dir, f'{uuid.uuid4()}.m4a')
88
+ with subprocess.Popen(['ffmpeg', '-i', flac_file_path, '-c:a', 'alac', alac_file_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
89
+ process.communicate()
90
  logging.info(f"Converted to ALAC: {alac_file_path}")
91
 
92
+ # Delete the FLAC file to free up space
93
+ os.remove(flac_file_path)
94
+ logging.info(f"Deleted FLAC file: {flac_file_path}")
95
+
96
  # Generate the file URL for ALAC
97
  alac_file_url = f'{BASE_URL}/file/{os.path.basename(alac_file_path)}'
98
  logging.info(f"Returning ALAC file URL: {alac_file_url}")
99
 
100
+ # Force garbage collection to clear memory
101
+ gc.collect()
102
+
103
  # Return the ALAC file URL to the client as JSON
104
  return {"url": alac_file_url}
105
 
 
111
  async def set_mime_type_middleware(request: Request, call_next):
112
  response = await call_next(request)
113
  if request.url.path.endswith(".m4a"):
114
+ response.headers["Content-Type"] = "audio/m4a"
115
  return response