Spaces:
Sleeping
Sleeping
| from typing import Optional | |
| import uvicorn | |
| from fastapi import FastAPI, APIRouter, Query | |
| #from .finit import api_router, app | |
| from myschemas import MyDataModel, MyDataSearchResults, CreateMyDataModelEntity | |
| from MyDataList import myDataList | |
| app = FastAPI(title="General Purpose API", openapi_url="/openapi.json") | |
| api_router = APIRouter() | |
| from fastapi.middleware.cors import CORSMiddleware | |
| # "http://localhost.tiangolo.com", | |
| # "https://localhost.tiangolo.com", | |
| # "http://localhost", | |
| # "http://localhost:8080", | |
| origins = [ | |
| "https://huggingface.co", | |
| "https://huggingface.co/spaces/abhijitkumarjha88192/test-space", | |
| "*" | |
| ] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| def root() -> dict: | |
| """ | |
| Root GET | |
| """ | |
| return {"msg": "Hello, World!"} | |
| def get_all() -> dict: | |
| return {"data": myDataList} | |
| # @api_router.get("/add_rows", status_code=200) | |
| # def add_rows(rowstr:str) -> dict: | |
| # return {"data": arr} | |
| # Updated using to use a response_model | |
| # https://fastapi.tiangolo.com/tutorial/response-model/ | |
| def get_data(*, data_id: int) -> dict: | |
| """ | |
| Fetch a single data by ID | |
| """ | |
| result = [x for x in myDataList if x["id"] == data_id] | |
| if result: | |
| return result[0] | |
| # Updated using the FastAPI parameter validation `Query` class | |
| # # https://fastapi.tiangolo.com/tutorial/query-params-str-validations/ | |
| def search_data(*, keyword: Optional[str] = Query(None,min_length=3), max_results: Optional[int] = 3) -> dict: | |
| """ | |
| Search for datas based on text keyword | |
| """ | |
| if not keyword: | |
| # we use Python list slicing to limit results | |
| # based on the max_results query parameter | |
| return {"results": myDataList[:max_results]} | |
| results = filter(lambda x: keyword.lower() in x["text"].lower(), myDataList) | |
| return {"results": list(results)[:max_results]} | |
| # New addition, using Pydantic model `RecipeCreate` to define | |
| # the POST request body | |
| def create_data(*, my_data: CreateMyDataModelEntity) -> MyDataModel: | |
| """ | |
| Create a new data (in memory only) | |
| """ | |
| new_entry_id = len(myDataList) + 1 | |
| entry = MyDataModel( | |
| id=new_entry_id, | |
| text=my_data.text, | |
| description=my_data.description | |
| ) | |
| myDataList.append(entry.model_dump()) | |
| return entry | |
| # Updated using to use a response_model | |
| # https://fastapi.tiangolo.com/tutorial/response-model/ | |
| def delete_data(*, data_id: int) -> dict: | |
| """ | |
| Fetch a single data by ID | |
| """ | |
| result = [x for x in myDataList if x["id"] == data_id] | |
| if result: | |
| myDataList.remove(result[0]) | |
| return {"datas":myDataList} | |
| app.include_router(api_router) | |
| # if __name__ == "__main__": | |
| # # Use this for debugging purposes only | |
| # import uvicorn | |
| # app.include_router(api_router) | |
| # uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug") |