From 798a3384ded7b7b9e87dc643154facba793becd9 Mon Sep 17 00:00:00 2001 From: Jakub Pokrywka Date: Sat, 25 Nov 2023 20:37:12 +0100 Subject: [PATCH] add zajecia 4 --- zajecia4/README.md | 45 ++++++++++++ zajecia4/fastapi_examples/not_that_simple.py | 71 +++++++++++++++++++ .../fastapi_examples/run_not_that_simple.sh | 4 ++ zajecia4/fastapi_examples/run_simple.sh | 2 + zajecia4/fastapi_examples/simple.py | 8 +++ zajecia4/fastapi_w_pytest/main.py | 10 +++ zajecia4/fastapi_w_pytest/test_app.py | 11 +++ zajecia4/pytest_examples/my_functions.py | 10 +++ zajecia4/pytest_examples/my_inc_function.py | 3 + .../pytest_examples/test_my_inc_function.py | 4 ++ zajecia4/pytest_examples/test_sample.py | 15 ++++ 11 files changed, 183 insertions(+) create mode 100644 zajecia4/README.md create mode 100644 zajecia4/fastapi_examples/not_that_simple.py create mode 100644 zajecia4/fastapi_examples/run_not_that_simple.sh create mode 100644 zajecia4/fastapi_examples/run_simple.sh create mode 100644 zajecia4/fastapi_examples/simple.py create mode 100644 zajecia4/fastapi_w_pytest/main.py create mode 100644 zajecia4/fastapi_w_pytest/test_app.py create mode 100644 zajecia4/pytest_examples/my_functions.py create mode 100644 zajecia4/pytest_examples/my_inc_function.py create mode 100644 zajecia4/pytest_examples/test_my_inc_function.py create mode 100644 zajecia4/pytest_examples/test_sample.py diff --git a/zajecia4/README.md b/zajecia4/README.md new file mode 100644 index 0000000..4970f4a --- /dev/null +++ b/zajecia4/README.md @@ -0,0 +1,45 @@ +# Zajęcia 4 + +## Aplikacje REST - Fastapi + +Jako zadanie pierwsze proszę przerobić wszystkie przykłady z https://fastapi.tiangolo.com/tutorial/ z działow od "First Steps" do "Request Body" odpalając każdy przykład na komputerze. + + + +Następnie proszę użyć modelu KNN z wczorajszych zajęc i wystawić w REST API oraz uruchomić serwis, który będzie serwował odpowiedzi dla zadanych parametrów wejśćiowych. + +Następnie osobno korzystając z : +- curl +- pythonowej bibliteki requests ( https://pypi.org/project/requests/ ) proszę wysłać zapytanie do serwisu + +proszę zadać pytanie do serwisu REST. Powinni państwo dostać predykcję modelu. + +## Testowanie w pythonie - Pytest + +Przeczytaj https://docs.pytest.org/en/7.4.x/getting-started.html odpalając przykłady na komputerze. + +Pobierz dane w następujący sposób: + +` import pandas as pd` + +` url = 'https://raw.githubusercontent.com/bigmlcom/python/master/data/spam.csv'` + +` data = pd.read_csv(url, sep='\t')` + +Podziel dane na train i test oraz wytrenuj model TF IDF + regresja logistyczna. Nastepnie stwórz funkcję `predict_text_category(text: str)`, +która przyjmuje tekst oraz zwraca string 'spam' lub 'ham'. Jeżeli funkcja nie otrzyma stringa, a inny typ danych, funkcja ma zwracać None. Napisz kilka testów sprawdzających +czy dana funkcja faktycznie dla stringów zwraca string, który zawiera się w zbiorze {'spam','ham'}, a dla innych typów None. + + +## Testowanie REST API + + +Przeczytaj https://fastapi.tiangolo.com/tutorial/testing/ odpalając przykłady na komputerze + +Osadź model z poprzedniego zadania w REST API i napisz testy REST API. + + + +## Własny projekt + +Jeżeli wykonałeś/aś wszystkie zadania, pracuj nad projektem zaliczeniownym. \ No newline at end of file diff --git a/zajecia4/fastapi_examples/not_that_simple.py b/zajecia4/fastapi_examples/not_that_simple.py new file mode 100644 index 0000000..cb8a595 --- /dev/null +++ b/zajecia4/fastapi_examples/not_that_simple.py @@ -0,0 +1,71 @@ +from fastapi import FastAPI +from enum import Enum +from typing import Union +from pydantic import BaseModel + + +app = FastAPI() + + +fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}] + + + +class ModelName(str, Enum): + alexnet = "alexnet" + resnet = "resnet" + lenet = "lenet" + + + +@app.get("/") +async def root(): + return {"message": "Hello World"} + + +@app.get("/items/{item_id}") +async def read_item(item_id: int): + return {"item_id": item_id} + + +@app.get("/models/{model_name}") +async def get_model(model_name: ModelName): + if model_name is ModelName.alexnet: + + return {"model_name": model_name, "message": "Deep Learning FTW!"} + + if model_name.value == "lenet": + return {"model_name": model_name, "message": "LeCNN all the images"} + + return {"model_name": model_name, "message": "Have some residuals"} + + + +@app.get("/items/") +async def read_item(skip: int = 0, limit: int = 10): + return fake_items_db[skip : skip + limit] + + +@app.get("/users/{user_id}/items/{item_id}") +async def read_user_item( + user_id: int, item_id: str, q: Union[str, None] = None, short: bool = False +): + item = {"item_id": item_id, "owner_id": user_id} + if q: + item.update({"q": q}) + if not short: + item.update( + {"description": "This is an amazing item that has a long description"} + ) + return item + + +class Item(BaseModel): + name: str + description: Union[str, None] = None + price: float + tax: Union[float, None] = None + +@app.post("/items/") +async def create_item(item: Item): + return item \ No newline at end of file diff --git a/zajecia4/fastapi_examples/run_not_that_simple.sh b/zajecia4/fastapi_examples/run_not_that_simple.sh new file mode 100644 index 0000000..d9d3d38 --- /dev/null +++ b/zajecia4/fastapi_examples/run_not_that_simple.sh @@ -0,0 +1,4 @@ +# uvicorn not_that_simple:app --reload +# albo python -m uvicorn not_that_simple:app --reload + + diff --git a/zajecia4/fastapi_examples/run_simple.sh b/zajecia4/fastapi_examples/run_simple.sh new file mode 100644 index 0000000..79f1952 --- /dev/null +++ b/zajecia4/fastapi_examples/run_simple.sh @@ -0,0 +1,2 @@ +# uvicorn simple:app --reload +# albo python -m uvicorn simple:app --reload diff --git a/zajecia4/fastapi_examples/simple.py b/zajecia4/fastapi_examples/simple.py new file mode 100644 index 0000000..ee60be1 --- /dev/null +++ b/zajecia4/fastapi_examples/simple.py @@ -0,0 +1,8 @@ +from fastapi import FastAPI + +app = FastAPI() + + +@app.get("/") +async def root(): + return {"message": "Hello World"} diff --git a/zajecia4/fastapi_w_pytest/main.py b/zajecia4/fastapi_w_pytest/main.py new file mode 100644 index 0000000..a89fcd0 --- /dev/null +++ b/zajecia4/fastapi_w_pytest/main.py @@ -0,0 +1,10 @@ +from fastapi import FastAPI +from fastapi.testclient import TestClient + +app = FastAPI() + + +@app.get("/") +async def read_main(): + return {"msg": "Hello World"} + diff --git a/zajecia4/fastapi_w_pytest/test_app.py b/zajecia4/fastapi_w_pytest/test_app.py new file mode 100644 index 0000000..ce09268 --- /dev/null +++ b/zajecia4/fastapi_w_pytest/test_app.py @@ -0,0 +1,11 @@ +from fastapi.testclient import TestClient +from main import app + + +client = TestClient(app) + + +def test_read_main(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"msg": "Hello World"} diff --git a/zajecia4/pytest_examples/my_functions.py b/zajecia4/pytest_examples/my_functions.py new file mode 100644 index 0000000..2add2ec --- /dev/null +++ b/zajecia4/pytest_examples/my_functions.py @@ -0,0 +1,10 @@ + +def fibonacci(n): + if not type(n) is int: + return False + if n == 0: + return 0 + elif n == 1 or n == 2: + return 1 + else: + return fibonacci(n - 1) + fibonacci(n - 2) \ No newline at end of file diff --git a/zajecia4/pytest_examples/my_inc_function.py b/zajecia4/pytest_examples/my_inc_function.py new file mode 100644 index 0000000..f3d80da --- /dev/null +++ b/zajecia4/pytest_examples/my_inc_function.py @@ -0,0 +1,3 @@ +def inc(x): + return x + 2 + diff --git a/zajecia4/pytest_examples/test_my_inc_function.py b/zajecia4/pytest_examples/test_my_inc_function.py new file mode 100644 index 0000000..b2aad48 --- /dev/null +++ b/zajecia4/pytest_examples/test_my_inc_function.py @@ -0,0 +1,4 @@ +from my_inc_function import inc + +def test_answer(): + assert inc(3) == 5 \ No newline at end of file diff --git a/zajecia4/pytest_examples/test_sample.py b/zajecia4/pytest_examples/test_sample.py new file mode 100644 index 0000000..bbd49e2 --- /dev/null +++ b/zajecia4/pytest_examples/test_sample.py @@ -0,0 +1,15 @@ +from my_functions import fibonacci + +def test_fib_basic_ints(): + assert fibonacci(0) == 0 + + assert fibonacci(1) == 1 + + assert fibonacci(2) == 1 + + assert fibonacci(20) == 6765 + + +def test_fib_str_input(): + + assert fibonacci('abc') == False