add zajecia 4
This commit is contained in:
parent
fc774f482e
commit
798a3384de
45
zajecia4/README.md
Normal file
45
zajecia4/README.md
Normal file
@ -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.
|
71
zajecia4/fastapi_examples/not_that_simple.py
Normal file
71
zajecia4/fastapi_examples/not_that_simple.py
Normal file
@ -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
|
4
zajecia4/fastapi_examples/run_not_that_simple.sh
Normal file
4
zajecia4/fastapi_examples/run_not_that_simple.sh
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# uvicorn not_that_simple:app --reload
|
||||||
|
# albo python -m uvicorn not_that_simple:app --reload
|
||||||
|
|
||||||
|
|
2
zajecia4/fastapi_examples/run_simple.sh
Normal file
2
zajecia4/fastapi_examples/run_simple.sh
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# uvicorn simple:app --reload
|
||||||
|
# albo python -m uvicorn simple:app --reload
|
8
zajecia4/fastapi_examples/simple.py
Normal file
8
zajecia4/fastapi_examples/simple.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def root():
|
||||||
|
return {"message": "Hello World"}
|
10
zajecia4/fastapi_w_pytest/main.py
Normal file
10
zajecia4/fastapi_w_pytest/main.py
Normal file
@ -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"}
|
||||||
|
|
11
zajecia4/fastapi_w_pytest/test_app.py
Normal file
11
zajecia4/fastapi_w_pytest/test_app.py
Normal file
@ -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"}
|
10
zajecia4/pytest_examples/my_functions.py
Normal file
10
zajecia4/pytest_examples/my_functions.py
Normal file
@ -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)
|
3
zajecia4/pytest_examples/my_inc_function.py
Normal file
3
zajecia4/pytest_examples/my_inc_function.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
def inc(x):
|
||||||
|
return x + 2
|
||||||
|
|
4
zajecia4/pytest_examples/test_my_inc_function.py
Normal file
4
zajecia4/pytest_examples/test_my_inc_function.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from my_inc_function import inc
|
||||||
|
|
||||||
|
def test_answer():
|
||||||
|
assert inc(3) == 5
|
15
zajecia4/pytest_examples/test_sample.py
Normal file
15
zajecia4/pytest_examples/test_sample.py
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user