-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update type hints and examples (#15)
* update Meteorite class typing * add __pycache__ to gitignore * clean up json_requests and plain_text_request examples * rename json_requests to json_request * add webhook example * run code formatting with black
- Loading branch information
Showing
11 changed files
with
100 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
/target | ||
|
||
.venv/ | ||
.venv/ | ||
__pycache__/ |
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,12 @@ | ||
# Send a JSON request | ||
|
||
This example shows how to send a JSON request to the prediction endpoint. | ||
The prediction function will syncronously sleep for 10 seconds, and | ||
then print the request JSON body to the console | ||
before returning the same body. | ||
|
||
Send a POST request using `curl`: | ||
|
||
``` | ||
curl -X POST http://localhost:4000/predict -H 'Content-Type: application/json' -d '{"key":"value"}' | ||
``` |
8 changes: 5 additions & 3 deletions
8
examples/json_requests/main.py → examples/json_request/main.py
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,16 +1,18 @@ | ||
import json | ||
import time | ||
import os | ||
from meteorite import Meteorite | ||
|
||
app = Meteorite() | ||
app.set_webhook_url("https://envzvlfwlg78.x.pipedream.net") | ||
|
||
|
||
@app.predict | ||
def main(data): | ||
data = json.loads(data) | ||
print("Sleeping for 10 seconds") | ||
time.sleep(10) | ||
print(data["key"]) | ||
print(data) | ||
return data | ||
|
||
|
||
app.start(port=5001) | ||
app.start() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
@app.predict | ||
def hello(data): | ||
body = data.decode("utf-8") | ||
print(body) | ||
return body | ||
|
||
|
||
|
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,31 @@ | ||
# Setup a webhook | ||
|
||
This example shows how to setup a webhook to receive the result of a prediction request. | ||
When the meteorite inference server receives a request, | ||
the request is queued and the server immediately returns a 200 response. | ||
The request is then processed by the inference function that returns an output. | ||
This output is sent to the webhook server as a POST request. | ||
|
||
Install dependencies. | ||
|
||
```shell | ||
pip install meteorite fastapi uvicorn | ||
``` | ||
|
||
Run the meteorite inference server that listens to port 4000. | ||
|
||
```shell | ||
python inference.py | ||
``` | ||
|
||
In another terminal, run the webhook server that listens to port 8000. | ||
|
||
```shell | ||
uvicorn webhook:app --reload | ||
``` | ||
|
||
In another terminal, send a POST request to the inference server. | ||
|
||
```shell | ||
curl -X POST http://localhost:4000/predict -H 'Content-Type: application/json' -d '{"key": "value"}' | ||
``` |
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,16 @@ | ||
import os | ||
from meteorite import Meteorite | ||
|
||
app = Meteorite() | ||
|
||
webhook_url = "http://localhost:4400" | ||
app.set_webhook_url(webhook_url) | ||
|
||
|
||
@app.predict | ||
def main(data): | ||
print(data) | ||
return "my prediction is 1" | ||
|
||
|
||
app.start() |
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,15 @@ | ||
from typing import Union | ||
|
||
from fastapi import FastAPI | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
|
||
|
||
app = FastAPI() | ||
|
||
|
||
@app.post("/", response_class=Response) | ||
async def handle(request: Request): | ||
body = await request.body() | ||
print(body.decode("utf-8")) | ||
return "OK" |
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