Spaces:
Sleeping
Sleeping
Commit
·
b8e9114
1
Parent(s):
8bf294e
added extra apis
Browse files- Dockerfile +1 -1
- app.py +86 -0
- packages.txt +0 -0
- pre-requirements.txt +0 -0
- testpkg/MyDataList.py +6 -0
- testpkg/__init__.py +0 -0
- testpkg/config.py +1 -0
- testpkg/finit.py +45 -0
- testpkg/schemas.py +19 -0
Dockerfile
CHANGED
|
@@ -8,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
-
CMD ["uvicorn", "
|
|
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
from fastapi import FastAPI, APIRouter, Query
|
| 3 |
+
from testpkg.finit import api_router, app
|
| 4 |
+
from testpkg.schemas import MyDataModel, MyDataSearchResults, CreateMyDataModelEntity
|
| 5 |
+
from testpkg.MyDataList import myDataList
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@api_router.get("/datas/getall", status_code=200)
|
| 9 |
+
def get_all() -> dict:
|
| 10 |
+
return {"data": myDataList}
|
| 11 |
+
|
| 12 |
+
# @api_router.get("/add_rows", status_code=200)
|
| 13 |
+
# def add_rows(rowstr:str) -> dict:
|
| 14 |
+
# return {"data": arr}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Updated using to use a response_model
|
| 18 |
+
# https://fastapi.tiangolo.com/tutorial/response-model/
|
| 19 |
+
@api_router.get("/datas/get/{data_id}", status_code=200)
|
| 20 |
+
def get_data(*, data_id: int) -> dict:
|
| 21 |
+
"""
|
| 22 |
+
Fetch a single data by ID
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
result = [x for x in myDataList if x["id"] == data_id]
|
| 26 |
+
if result:
|
| 27 |
+
return result[0]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Updated using the FastAPI parameter validation `Query` class
|
| 31 |
+
# # https://fastapi.tiangolo.com/tutorial/query-params-str-validations/
|
| 32 |
+
@api_router.get("/datas/search", status_code=200)
|
| 33 |
+
def search_data(*, keyword: Optional[str] = Query(None,min_length=3), max_results: Optional[int] = 3) -> dict:
|
| 34 |
+
"""
|
| 35 |
+
Search for datas based on text keyword
|
| 36 |
+
"""
|
| 37 |
+
if not keyword:
|
| 38 |
+
# we use Python list slicing to limit results
|
| 39 |
+
# based on the max_results query parameter
|
| 40 |
+
return {"results": myDataList[:max_results]}
|
| 41 |
+
|
| 42 |
+
results = filter(lambda x: keyword.lower() in x["text"].lower(), myDataList)
|
| 43 |
+
return {"results": list(results)[:max_results]}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# New addition, using Pydantic model `RecipeCreate` to define
|
| 47 |
+
# the POST request body
|
| 48 |
+
@api_router.post("/datas/create", status_code=201, response_model=MyDataModel)
|
| 49 |
+
def create_data(*, my_data: CreateMyDataModelEntity) -> MyDataModel:
|
| 50 |
+
"""
|
| 51 |
+
Create a new data (in memory only)
|
| 52 |
+
"""
|
| 53 |
+
new_entry_id = len(myDataList) + 1
|
| 54 |
+
entry = MyDataModel(
|
| 55 |
+
id=new_entry_id,
|
| 56 |
+
text=my_data.text,
|
| 57 |
+
description=my_data.description
|
| 58 |
+
)
|
| 59 |
+
myDataList.append(entry.model_dump())
|
| 60 |
+
|
| 61 |
+
return entry
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Updated using to use a response_model
|
| 65 |
+
# https://fastapi.tiangolo.com/tutorial/response-model/
|
| 66 |
+
@api_router.get("/datas/delete/{data_id}", status_code=200)
|
| 67 |
+
def delete_data(*, data_id: int) -> dict:
|
| 68 |
+
"""
|
| 69 |
+
Fetch a single data by ID
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
result = [x for x in myDataList if x["id"] == data_id]
|
| 73 |
+
if result:
|
| 74 |
+
myDataList.remove(result[0])
|
| 75 |
+
|
| 76 |
+
return {"datas":myDataList}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
# Use this for debugging purposes only
|
| 82 |
+
import uvicorn
|
| 83 |
+
|
| 84 |
+
app.include_router(api_router)
|
| 85 |
+
|
| 86 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug")
|
packages.txt
ADDED
|
File without changes
|
pre-requirements.txt
ADDED
|
File without changes
|
testpkg/MyDataList.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
myDataList = [
|
| 3 |
+
{"text": "Row 1", "description": "Description 1", "id": 1},
|
| 4 |
+
{"text": "Row 2", "description": "Description 2", "id": 2},
|
| 5 |
+
{"text": "Row 3", "description": "Description 3", "id": 3},
|
| 6 |
+
]
|
testpkg/__init__.py
ADDED
|
File without changes
|
testpkg/config.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
cur_base_path = "/"
|
testpkg/finit.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, APIRouter
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="General Purpose API", openapi_url="/openapi.json")
|
| 6 |
+
|
| 7 |
+
api_router = APIRouter()
|
| 8 |
+
|
| 9 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
+
|
| 11 |
+
# "http://localhost.tiangolo.com",
|
| 12 |
+
# "https://localhost.tiangolo.com",
|
| 13 |
+
# "http://localhost",
|
| 14 |
+
# "http://localhost:8080",
|
| 15 |
+
|
| 16 |
+
origins = [
|
| 17 |
+
"https://huggingface.co",
|
| 18 |
+
"https://huggingface.co/spaces/abhijitkumarjha88192/test-space",
|
| 19 |
+
"*"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
app.add_middleware(
|
| 23 |
+
CORSMiddleware,
|
| 24 |
+
allow_origins=origins,
|
| 25 |
+
allow_credentials=True,
|
| 26 |
+
allow_methods=["*"],
|
| 27 |
+
allow_headers=["*"],
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@api_router.get("/", status_code=200)
|
| 32 |
+
def root() -> dict:
|
| 33 |
+
"""
|
| 34 |
+
Root GET
|
| 35 |
+
"""
|
| 36 |
+
return {"msg": "Hello, World!"}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
# Use this for debugging purposes only
|
| 42 |
+
app.include_router(api_router)
|
| 43 |
+
import uvicorn
|
| 44 |
+
|
| 45 |
+
uvicorn.run(app, host="0.0.0.0", port=8001, log_level="debug")
|
testpkg/schemas.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, HttpUrl
|
| 2 |
+
|
| 3 |
+
from typing import Sequence
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class MyDataModel(BaseModel):
|
| 7 |
+
id: int
|
| 8 |
+
text: str
|
| 9 |
+
description: str
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class MyDataSearchResults(BaseModel):
|
| 13 |
+
results: Sequence[MyDataModel]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class CreateMyDataModelEntity(BaseModel):
|
| 17 |
+
id: int
|
| 18 |
+
text: str
|
| 19 |
+
description: str
|