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 #23

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
36 changes: 36 additions & 0 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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: Python application

on: [push, pull_request]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout code
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
pip install -r requirements.txt
- 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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Python files
*.pyc
__pycache__/
.pytest_cache/
*.pyo

# Virtual environment
.venv/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/ПИ-2ое задание.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
[![Tests](https://github.com/tokarevsas31/ml_fastapi_tests/actions/workflows/python-app.yml/badge.svg)](https://github.com/tokarevsas31/ml_fastapi_tests/actions/workflows/python-app.yml)

# An example of ML Application with the pretrained model and test.
# FastAPI Sentiment Analysis using the pretrained model

An example of English text tone detection with [Hugging Face](https://huggingface.co/) library.

## Description

Tests GitHub Actions
An example of English text tone detection. The sentiment analysis is performed using the [Hugging Face](https://huggingface.co/) Transformers library, specifically, a pre-trained sentiment analysis model.

### Launch

1. Clone this repository and navigate to the project folder
2. Install the necessary dependencies
```
python -m pip install --upgrade pip
pip install -r requirements.txt
```
3. Launch the server
```
uvicorn main:app
```
4. The web application will be deployed at ```http://127.0.0.1:8000```
5. Requests:
- `/`: Root GET-request returns a «Hello World» message
- `/predict/` A POST-request accepts a text string and returns the sentiment of the text. The sentiment is categorized into "positive" or "negative"
6. Terminal usage
- To use the sentiment analysis API, you need to send a POST-request to the `/predict/` endpoint with the text you want to analyze. The text must be sent in the request body as a JSON object with the `text` key.
```
curl -X 'POST' 'http://127.0.0.1:8000/predict/' -H 'Content-Type: application/json' -d '{"text":"YOUR TEXT"}
```
Binary file added __pycache__/main.cpython-312.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ class Item(BaseModel):
text: str



app = FastAPI()
classifier = pipeline("sentiment-analysis")


@app.get("/just_another_api/")
def new_api():
return {"wow": "impressive"}


@app.get("/")
def root():
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--extra-index-url https://download.pytorch.org/whl/cpu
fastapi
httpx
uvicorn
transformers
torch
httpx
transformers

14 changes: 13 additions & 1 deletion test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
client = TestClient(app)


def test_just_another_api():
response = client.get("/just_another_api/")
assert response.status_code == 200
assert response.json() == {"wow": "impressive"}


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():
Expand All @@ -24,3 +30,9 @@ def test_predict_negative():
json_data = response.json()
assert response.status_code == 200
assert json_data['label'] == 'NEGATIVE'


def test_predict_allowed():
response = client.get("/predict/")
assert response.status_code == 405
assert response.json() == {"detail": "Method Not Allowed"}