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)