-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from runpod/FastAPI
Fast api
- Loading branch information
Showing
9 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
env | ||
.env | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ python-dotenv >= 0.21.0 | |
requests >= 2.28.1 | ||
boto3 >= 1.26.15 | ||
aiohttp >= 3.8.3 | ||
fastapi[all] >= 0.89.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
''' Allows serverless to recognized as a package.''' | ||
|
||
import os | ||
import asyncio | ||
|
||
from . import work_loop | ||
from .modules import rp_fastapi | ||
|
||
|
||
def start(config): | ||
''' | ||
Starts the serverless worker. | ||
''' | ||
asyncio.run(work_loop.start_worker(config)) | ||
api_port = os.environ.get('RUNPOD_API_PORT', None) | ||
|
||
if api_port: | ||
api_server = rp_fastapi.WorkerAPI() | ||
api_server.config = config | ||
|
||
api_server.start_uvicorn(api_port) | ||
else: | ||
asyncio.run(work_loop.start_worker(config)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
''' Used to launch the FastAPI web server when worker is running in API mode. ''' | ||
|
||
import os | ||
import threading | ||
|
||
import uvicorn | ||
from fastapi import FastAPI | ||
from fastapi.encoders import jsonable_encoder | ||
from pydantic import BaseModel | ||
|
||
from .job import run_job | ||
from .worker_state import set_job_id | ||
from .heartbeat import start_heartbeat | ||
|
||
|
||
class Job(BaseModel): | ||
''' Represents a job. ''' | ||
id: str | ||
input: dict | ||
|
||
|
||
class WorkerAPI: | ||
''' Used to launch the FastAPI web server when worker is running in API mode. ''' | ||
|
||
def __init__(self): | ||
''' | ||
Initializes the WorkerAPI class. | ||
1. Starts the heartbeat thread. | ||
2. Initializes the FastAPI web server. | ||
''' | ||
heartbeat_thread = threading.Thread(target=start_heartbeat) | ||
heartbeat_thread.daemon = True | ||
heartbeat_thread.start() | ||
|
||
self.config = {"handler": None} | ||
self.rp_app = FastAPI() | ||
self.rp_app.add_api_route("/run", self.run, methods=["POST"]) | ||
|
||
def start_uvicorn(self, api_port): | ||
''' | ||
Starts the Uvicorn server. | ||
''' | ||
uvicorn.run( | ||
self.rp_app, host='0.0.0.0', port=int(api_port), | ||
workers=os.environ.get('RUNPOD_REALTIME_CONCURRENCY', 1) | ||
) | ||
|
||
async def run(self, job: Job): | ||
''' | ||
Performs model inference on the input data. | ||
''' | ||
set_job_id(job.id) | ||
|
||
job_results = run_job(self.config["handler"], job.__dict__) | ||
|
||
set_job_id(None) | ||
|
||
return jsonable_encoder(job_results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ install_requires = | |
requests >= 2.28.1 | ||
boto3 >= 1.26.15 | ||
aiohttp >= 3.8.3 | ||
fastapi[all] >= 0.89.0 |