klasocki commited on
Commit
5262393
1 Parent(s): a9ea03f

Refactor to use Routers, hack the tests to work again with static

Browse files
Files changed (3) hide show
  1. app.py +9 -23
  2. routers/__init__.py +0 -0
  3. routers/baseline.py +25 -0
app.py CHANGED
@@ -1,31 +1,17 @@
 
 
1
  import uvicorn
2
- from fastapi import FastAPI, HTTPException
3
- from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import FileResponse
5
- from src.baseline import BaselineCommaFixer
6
- import logging
7
-
8
- logger = logging.Logger(__name__)
9
- logging.basicConfig(level=logging.INFO)
10
-
11
- app = FastAPI() # TODO router?
12
- logger.info('Loading the baseline model...')
13
- app.baseline_model = BaselineCommaFixer()
14
-
15
 
16
- @app.post('/baseline/fix-commas/')
17
- async def fix_commas_with_baseline(data: dict):
18
- json_field_name = 's'
19
- if json_field_name in data:
20
- logger.debug('Fixing commas.')
21
- return {json_field_name: app.baseline_model.fix_commas(data['s'])}
22
- else:
23
- msg = f"Text '{json_field_name}' missing"
24
- logger.debug(msg)
25
- raise HTTPException(status_code=400, detail=msg)
26
 
 
 
27
 
28
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
29
 
30
 
31
  @app.get('/')
 
1
+ from os.path import realpath
2
+
3
  import uvicorn
4
+ from fastapi import FastAPI
 
5
  from fastapi.responses import FileResponse
6
+ from fastapi.staticfiles import StaticFiles
 
 
 
 
 
 
 
 
 
7
 
8
+ from routers import baseline
 
 
 
 
 
 
 
 
 
9
 
10
+ app = FastAPI()
11
+ app.include_router(baseline.router, prefix='/baseline')
12
 
13
+ # Without the realpath hack tests fail
14
+ app.mount("/", StaticFiles(directory=realpath(f'{realpath(__file__)}/../static'), html=True), name="static")
15
 
16
 
17
  @app.get('/')
routers/__init__.py ADDED
File without changes
routers/baseline.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, HTTPException
2
+ import logging
3
+
4
+ from src.baseline import BaselineCommaFixer
5
+
6
+
7
+ logger = logging.Logger(__name__)
8
+ logging.basicConfig(level=logging.INFO)
9
+
10
+ router = APIRouter()
11
+
12
+ logger.info('Loading the baseline model...')
13
+ router.model = BaselineCommaFixer()
14
+
15
+
16
+ @router.post('/fix-commas/')
17
+ async def fix_commas_with_baseline(data: dict):
18
+ json_field_name = 's'
19
+ if json_field_name in data:
20
+ logger.debug('Fixing commas.')
21
+ return {json_field_name: router.model.fix_commas(data['s'])}
22
+ else:
23
+ msg = f"Text '{json_field_name}' missing"
24
+ logger.debug(msg)
25
+ raise HTTPException(status_code=400, detail=msg)