diff --git a/.github/workflows/python-lint.yml b/.github/workflows/python-lint.yml new file mode 100644 index 0000000..4931ead --- /dev/null +++ b/.github/workflows/python-lint.yml @@ -0,0 +1,39 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: flake8 + +on: + push: + branches: [ "master", "improvements" ] + pull_request: + branches: [ "master"] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest diff --git a/README.md b/README.md index 0c3737e..9fe9a8b 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,18 @@ An example of English text tone detection with [Hugging Face](https://huggingfac Tests GitHub Actions + +# Описание + +Данный репозиторий представляет пример создания приложения по определению тональности текста + +# Работа с приложением + +1. Склонируйте данный репозиторий +2. Установите необходимые зависимости ```pip install -r requirements.txt``` +3. Запустите веб-приложение ```uvicorn main:app``` +4. Веб приложение будет развернуто по адресу ```http://localhost:8000``` +5. Проект имеет две точки доступа: + - при отправке GET-запроса по адресу ```/``` будет возвращен json с текстом «Hello World» + - при отправке POST-запроса по адресу ```/predict/``` с передачей параметра «text» + в теле запроса, в котором передается текст для определения тональности diff --git a/main.py b/main.py index be946ec..cf3234d 100644 --- a/main.py +++ b/main.py @@ -7,17 +7,28 @@ class Item(BaseModel): text: str - app = FastAPI() classifier = pipeline("sentiment-analysis") - @app.get("/") def root(): + """Handler for a GET request to the root URL + + Returns: + dict: Dictionary with "Hello World" + """ return {"message": "Hello World"} @app.post("/predict/") def predict(item: Item): + """Handler for POST request to /predict/ + + Args: + item (Item): Item object containing text for sentiment analysis + + Returns: + dict: Dictionary with predicted mood of the text + """ return classifier(item.text)[0] diff --git a/requirements.txt b/requirements.txt index e20aba8..6db3b86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ uvicorn transformers torch httpx +pandas +pytest \ No newline at end of file diff --git a/test_main.py b/test_main.py index fdbdb2d..4cdb43f 100644 --- a/test_main.py +++ b/test_main.py @@ -7,20 +7,24 @@ def test_read_main(): response = client.get("/") assert response.status_code == 200 - assert response.json() == {"message": "World"} + assert response.json() == {"message": "Hello World"} def test_predict_positive(): - response = client.post("/predict/", - json={"text": "I like machine learning!"}) + response = client.post("/predict/", json={"text": "I like machine learning!"}) json_data = response.json() assert response.status_code == 200 - assert json_data['label'] == 'POSITIVE' + assert json_data["label"] == "POSITIVE" def test_predict_negative(): - response = client.post("/predict/", - json={"text": "I hate machine learning!"}) + response = client.post("/predict/", json={"text": "I hate machine learning!"}) json_data = response.json() assert response.status_code == 200 - assert json_data['label'] == 'NEGATIVE' + assert json_data["label"] == "NEGATIVE" + + +def test_predict_get(): + response = client.get("/predict/") + assert response.status_code == 405 + assert response.json() == {"detail": "Method Not Allowed"}