Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from readyapi import ReadyAPI
|
| 2 |
+
from readyapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from readyapi.responses import JSONResponse
|
| 4 |
+
from readyapi.staticfiles import StaticFiles
|
| 5 |
+
import numpy as np
|
| 6 |
+
import argparse
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
HOST = os.environ.get("API_URL", "0.0.0.0")
|
| 10 |
+
PORT = os.environ.get("PORT", 7860)
|
| 11 |
+
parser = argparse.ArgumentParser()
|
| 12 |
+
parser.add_argument("--host", default=HOST)
|
| 13 |
+
parser.add_argument("--port", type=int, default=PORT)
|
| 14 |
+
parser.add_argument("--reload", action="store_true", default=True)
|
| 15 |
+
parser.add_argument("--ssl_certfile")
|
| 16 |
+
parser.add_argument("--ssl_keyfile")
|
| 17 |
+
args = parser.parse_args()
|
| 18 |
+
|
| 19 |
+
app = ReadyAPI()
|
| 20 |
+
app.add_middleware(
|
| 21 |
+
CORSMiddleware,
|
| 22 |
+
allow_origins=["*"],
|
| 23 |
+
allow_credentials=True,
|
| 24 |
+
allow_methods=["*"],
|
| 25 |
+
allow_headers=["*"],
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@app.get("/invert")
|
| 30 |
+
async def invert(text: str):
|
| 31 |
+
return {
|
| 32 |
+
"original": text,
|
| 33 |
+
"inverted": text[::-1],
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.get("/data")
|
| 38 |
+
async def get_data():
|
| 39 |
+
data = {"data": np.random.rand(100).tolist()}
|
| 40 |
+
return JSONResponse(data)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
import uvicorn
|
| 47 |
+
|
| 48 |
+
print(args)
|
| 49 |
+
uvicorn.run(
|
| 50 |
+
"app:app",
|
| 51 |
+
host=args.host,
|
| 52 |
+
port=args.port,
|
| 53 |
+
reload=args.reload,
|
| 54 |
+
ssl_certfile=args.ssl_certfile,
|
| 55 |
+
ssl_keyfile=args.ssl_keyfile,
|
| 56 |
+
)
|