Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ repos:
- id: mypy
exclude: 'tests'


# run local pylint in venv
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
require_serial: true
args:
- --max-line-length=120
- --ignore-imports=yes
- -d duplicate-code
- -d C0111,W0621,R0913,R1705,W0201,R0903,W0613,C0415,C0103
#- repo: local
# hooks:
# - id: pylint
# name: pylint
# entry: pylint
# language: system
# types: [python]
# require_serial: true
# args:
# - --max-line-length=120
# - --ignore-imports=yes
# - -d duplicate-code
# - -d C0111,W0621,R0913,R1705,W0201,R0903,W0613,C0415,C0103


- repo: https://github.com/asottile/pyupgrade
Expand Down
17 changes: 17 additions & 0 deletions homework_10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Задание

В этом домашнем задании предлагается реализовать REST API, занимающегося инференсом некой модели машинного обучения, т.е. поступает набор признаков объекта, на выходе возвращается предсказание модели. В качестве основы проекта предлагается фреймворк FastAPI

# Запуск

```bash
poetry install --no-root
poetry run uvicorn app.app:app --host 127.0.0.1 --port 8000
```

# Реализация

По GET запросу вида `localhost:8000/analysis/<some_text>` делается анализ текста: считается относительное количество гласных английских букв в строке и отдается в формате JSON:
```
{"result": <float>}
```
Empty file added homework_10/app/__init__.py
Empty file.
81 changes: 81 additions & 0 deletions homework_10/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import Optional

import jwt # type: ignore[import-error]
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
from pydantic import BaseModel

# auth secrets
SECRET_KEY = "secret_key"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 1

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

# fake db
users_db = {
"john_smith": {
"username": "john_smith",
"hashed_password": pwd_context.hash("john_smith"),
"age": 25,
"group": "users",
},
"admin1": {
"username": "admin1",
"hashed_password": pwd_context.hash("john_smith"),
"age": 25,
"group": "admin",
},
}

app = FastAPI()


class User(BaseModel):
username: str
email: str
age: int
role: str


class TokenData(BaseModel):
username: Optional[str] = None


class ModelInferenceOutput(BaseModel):
result: float


async def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
token_data = TokenData(username=username)
except jwt.PyJWTError:
raise credentials_exception
user = users_db.get(token_data.username, None) # type: ignore[arg-type]
if user is None:
raise credentials_exception
return user


@app.get("/")
def index():
return {"text": "ML model inference"}


@app.get("/analysis/{data}", response_model=ModelInferenceOutput)
def run_model_analysis(data: str, user: User = Depends(get_current_user)):
if user.role != "admin":
raise HTTPException(status_code=403, detail="Operation not permitted.")
result = sum(map(data.lower().count, "aeiuyo")) / len(data)
return {"result": result}
Loading