Skip to content

Commit bad1dc3

Browse files
tchatonkrshrimali
authored andcommitted
Introduce Upload File endpoint (#14703)
* update * update * update * update * update * update * update * update * update * update
1 parent 5e34ef1 commit bad1dc3

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
66

77

8-
## [0.6.1] - 2022-09-16
8+
## [0.6.1] - 2022-09-19
9+
10+
### Added
11+
12+
- Add support to upload files to the Drive through an asynchronous `upload_file` endpoint ([#14703](https://github.com/Lightning-AI/lightning/pull/14703))
913

1014
### Changed
1115

src/lightning_app/core/api.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import traceback
66
from copy import deepcopy
77
from multiprocessing import Queue
8+
from tempfile import TemporaryDirectory
89
from threading import Event, Lock, Thread
910
from typing import Dict, List, Mapping, Optional
1011

1112
import uvicorn
1213
from deepdiff import DeepDiff, Delta
13-
from fastapi import FastAPI, HTTPException, Request, Response, status, WebSocket
14+
from fastapi import FastAPI, File, HTTPException, Request, Response, status, UploadFile, WebSocket
1415
from fastapi.middleware.cors import CORSMiddleware
1516
from fastapi.params import Header
1617
from fastapi.responses import HTMLResponse, JSONResponse
@@ -23,6 +24,7 @@
2324
from lightning_app.api.request_types import DeltaRequest
2425
from lightning_app.core.constants import FRONTEND_DIR
2526
from lightning_app.core.queues import RedisQueue
27+
from lightning_app.storage import Drive
2628
from lightning_app.utilities.app_helpers import InMemoryStateStore, Logger, StateStore
2729
from lightning_app.utilities.enum import OpenAPITags
2830
from lightning_app.utilities.imports import _is_redis_available, _is_starsessions_available
@@ -234,6 +236,29 @@ async def post_state(
234236
api_app_delta_queue.put(DeltaRequest(delta=Delta(deep_diff)))
235237

236238

239+
@fastapi_service.put("/api/v1/upload_file/{filename}")
240+
async def upload_file(filename: str, uploaded_file: UploadFile = File(...)):
241+
with TemporaryDirectory() as tmp:
242+
drive = Drive(
243+
"lit://uploaded_files",
244+
component_name="file_server",
245+
allow_duplicates=True,
246+
root_folder=tmp,
247+
)
248+
tmp_file = os.path.join(tmp, filename)
249+
250+
with open(tmp_file, "wb") as f:
251+
done = False
252+
while not done:
253+
# Note: The 8192 number doesn't have a strong reason.
254+
content = await uploaded_file.read(8192)
255+
f.write(content)
256+
done = content == b""
257+
258+
drive.put(filename)
259+
return f"Successfully uploaded '{filename}' to the Drive"
260+
261+
237262
@fastapi_service.get("/healthz", status_code=200)
238263
async def healthz(response: Response):
239264
"""Health check endpoint used in the cloud FastAPI servers to check the status periodically. This requires

src/lightning_app/runners/cloud.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,12 @@ def _check_uploaded_folder(root: Path, repo: LocalSourceCodeDir) -> None:
332332
else:
333333
warning_msg += "\nYou can ignore some files or folders by adding them to `.lightningignore`."
334334
logger.warn(warning_msg)
335+
336+
def _project_has_sufficient_credits(self, project: V1Membership, app: Optional[LightningApp] = None):
337+
"""check if user has enough credits to run the app with its hardware if app is not passed return True if
338+
user has 1 or more credits."""
339+
balance = project.balance
340+
if balance is None:
341+
balance = 0 # value is missing in some tests
342+
343+
return balance >= 1

0 commit comments

Comments
 (0)