Skip to content

Commit

Permalink
軽くリファクタした
Browse files Browse the repository at this point in the history
  • Loading branch information
averak committed Jan 28, 2024
1 parent 62054ff commit 65d8751
Show file tree
Hide file tree
Showing 36 changed files with 123 additions and 204 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/test.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ python_version = "3.9"
[scripts]
start = "python main.py --start"
help = "python main.py --help"
test = "python -m unittest discover tests"
test = "python -m unittest discover -s tests"
format = "autopep8 -ivr ."
lint = "flake8 --show-source ."
mypy = "mypy ."
File renamed without changes.
38 changes: 38 additions & 0 deletions app/handler/controller/dajare_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from fastapi import APIRouter, Depends

from app.handler.dto.eval_dto import EvalV1
from app.handler.dto.judge_dto import JudgeV1
from app.handler.dto.reading_dto import ReadingV1
from app.service.dajare_service import DajareService

dajare_service = DajareService()

router = APIRouter()


@router.get('/eval/', status_code=200, response_model=EvalV1.Response, include_in_schema=False)
@router.get('/eval', status_code=200, response_model=EvalV1.Response)
async def eval_v1(request: EvalV1.Request = Depends()):
dajare = dajare_service.eval_dajare(request.dajare)
return EvalV1.Response(
score=dajare.score,
)


@router.get('/judge/', status_code=200, response_model=JudgeV1.Response, include_in_schema=False)
@router.get('/judge', status_code=200, response_model=JudgeV1.Response)
async def judge_v1(request: JudgeV1.Request = Depends()):
dajare = dajare_service.judge_dajare(request.dajare)
return JudgeV1.Response(
is_dajare=dajare.is_dajare,
applied_rule=dajare.applied_rule,
)


@router.get('/reading/', status_code=200, response_model=ReadingV1.Response, include_in_schema=False)
@router.get('/reading', status_code=200, response_model=ReadingV1.Response)
async def reading_v1(request: ReadingV1.Request = Depends()):
dajare = dajare_service.convert_reading(request.dajare)
return ReadingV1.Response(
reading=dajare.reading,
)
30 changes: 30 additions & 0 deletions app/handler/controller/outer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.handler.controller import dajare_controller


def fastapi_app() -> FastAPI:
app = FastAPI()

app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)

app.include_router(dajare_controller.router, prefix='/v1', tags=['dajare'])
app.include_router(dajare_controller.router, prefix='', tags=['dajare'])

@app.get('/')
def index():
return "health"

app.title = 'DaaS API'
app.openapi_tags = [
{"name": "dajare", "description": "Operation with dajare _(ダジャレ)_ ."}
]

return app
9 changes: 9 additions & 0 deletions app/handler/dto/eval_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic import BaseModel


class EvalV1:
class Request(BaseModel):
dajare: str

class Response(BaseModel):
score: float
10 changes: 10 additions & 0 deletions app/handler/dto/judge_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pydantic import BaseModel


class JudgeV1:
class Request(BaseModel):
dajare: str

class Response(BaseModel):
is_dajare: bool
applied_rule: str
9 changes: 9 additions & 0 deletions app/handler/dto/reading_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic import BaseModel


class ReadingV1:
class Request(BaseModel):
dajare: str

class Response(BaseModel):
reading: str
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions core/service/dajare_service.py → app/service/dajare_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from functools import lru_cache

from core import config
from core.model.dajare_model import DajareModel
from core.service.engine.judge_engine import JudgeEngine
from core.service.engine.eval_engine import EvalEngine
from core.service.engine.reading_engine import ReadingEngine
from app import config
from app.model.dajare_model import DajareModel
from app.service.engine.judge_engine import JudgeEngine
from app.service.engine.eval_engine import EvalEngine
from app.service.engine.reading_engine import ReadingEngine


class DajareService:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
import Levenshtein

from core import config
from core.util import text_util
from app import config
from app.util import text_util


class EvalEngine:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pyboin
import collections

from core import config
from core.util import text_util
from app import config
from app.util import text_util


class JudgeEngine:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from core.util import text_util
from app.util import text_util


class ReadingEngine:
Expand Down
3 changes: 1 addition & 2 deletions core/util/text_util.py → app/util/text_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import mojimoji
from janome.tokenizer import Tokenizer

from core import config

from app import config

# morph analyzer
tokenizer = Tokenizer()
Expand Down
40 changes: 0 additions & 40 deletions core/api/controller/__init__.py

This file was deleted.

23 changes: 0 additions & 23 deletions core/api/controller/eval_controller.py

This file was deleted.

24 changes: 0 additions & 24 deletions core/api/controller/judge_controller.py

This file was deleted.

23 changes: 0 additions & 23 deletions core/api/controller/reading_controller.py

This file was deleted.

5 changes: 0 additions & 5 deletions core/api/request/eval_request.py

This file was deleted.

5 changes: 0 additions & 5 deletions core/api/request/judge_request.py

This file was deleted.

5 changes: 0 additions & 5 deletions core/api/request/reading_request.py

This file was deleted.

5 changes: 0 additions & 5 deletions core/api/response/eval_response.py

This file was deleted.

6 changes: 0 additions & 6 deletions core/api/response/judge_response.py

This file was deleted.

5 changes: 0 additions & 5 deletions core/api/response/reading_response.py

This file was deleted.

Empty file removed core/service/engine/__init__.py
Empty file.
Empty file removed core/util/__init__.py
Empty file.
9 changes: 4 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import random
import argparse

from core import config
from core import message
from core.api.controller import create_app
from app import config, message
from app.handler.controller.outer import fastapi_app


def start_mode():
app = create_app()
app = fastapi_app()
uvicorn.run(app, host=config.API_HOST, port=config.API_PORT)


Expand All @@ -33,7 +32,7 @@ def accuracy_mode():
data = random.sample(data, n_samples)

# launch API
app = TestClient(create_app())
app = TestClient(fastapi_app())

# measure accuracy
error_samples: list[dict] = []
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 65d8751

Please sign in to comment.