File size: 1,345 Bytes
ea7280d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import os
import subprocess
import zipfile
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
app = FastAPI()
REQUIREMENTS_FILE = "requirements1.txt"
@app.get("/download-dependencies")
async def download_dependencies():
try:
# Ensure the directories exist
os.makedirs("/tmp/dependencies", exist_ok=True)
# Download dependencies
subprocess.run(["pip", "download", "-r", REQUIREMENTS_FILE, "-d", "/tmp/dependencies"], check=True)
# Create a zip file
zip_path = "/tmp/dependencies.zip"
with zipfile.ZipFile(zip_path, 'w') as zipf:
for root, dirs, files in os.walk("/tmp/dependencies"):
for file in files:
zipf.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
"/tmp/dependencies"))
# Serve the zip file
return FileResponse(zip_path, filename='dependencies.zip')
except subprocess.CalledProcessError as e:
raise HTTPException(status_code=500, detail=f"Error downloading dependencies: {str(e)}")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=5000)
|