Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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»
в теле запроса, в котором передается текст для определения тональности
15 changes: 13 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ uvicorn
transformers
torch
httpx
pandas
pytest
18 changes: 11 additions & 7 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}