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