Skip to content

Commit

Permalink
add flake8 and isort
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed Sep 13, 2024
1 parent 840923b commit f563fdf
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
exclude =
__pycache__
.github
.pytest_cache
.venv
14 changes: 11 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
test:
name: Test
name: Lint & Test
runs-on: ubuntu-latest

steps:
Expand All @@ -22,8 +22,16 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install dependencies for testing
run: |
if [ -f requirements-test.txt ]; then pip install -r requirements-test.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: |
pip install pytest pytest-cov
pytest
19 changes: 19 additions & 0 deletions .vs-code/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
"black-formatter.args": [
"--line-length",
"88"
],
"flake8.args": [
"--max-line-length",
"88",
"--extend-ignore",
"E203"
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
23 changes: 12 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
from pydantic import BaseModel
from fastapi import FastAPI


app = FastAPI()
pipe = pipeline("text-classification", model="shahrukhx01/question-vs-statement-classifier")
model = "shahrukhx01/question-vs-statement-classifier"
pipe = pipeline("text-classification", model=model)


custom_labels = {"LABEL_0": "STATEMENT", "LABEL_1": "QUESTION"}

custom_labels = {
"LABEL_0": "STATEMENT",
"LABEL_1": "QUESTION"
}

class Payload(BaseModel):
text: str
text: str


@app.post("/test")
async def test(payload: Payload):
result = pipe(payload.text)[0]

# Customize the label
result['label'] = custom_labels.get(result['label'], result['label'])
result = pipe(payload.text)[0]

return result
# Customize the label
result["label"] = custom_labels.get(result["label"], result["label"])
return result
4 changes: 4 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest==8.3.3
pytest-cov==5.0.0
httpx==0.27.2
flake8==7.1.1
7 changes: 1 addition & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
fastapi[standard]==0.114.1
pydantic==2.8.0
transformers[torch]==4.44.2

# Test
pytest==8.3.3
pytest-cov==5.0.0
httpx==0.27.2
transformers[torch]==4.44.2
8 changes: 4 additions & 4 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

client = TestClient(app)

def test_route():
response = client.post('/test', json={ "text": "Is this a test?" })
assert response.status_code == 200
assert response.json()['label'] == 'QUESTION'

def test_route():
response = client.post("/test", json={"text": "Is this a test?"})
assert response.status_code == 200
assert response.json()["label"] == "QUESTION"

0 comments on commit f563fdf

Please sign in to comment.