Spaces:
Running
Running
Refactor to use Routers, hack the tests to work again with static
Browse files- app.py +9 -23
- routers/__init__.py +0 -0
- routers/baseline.py +25 -0
app.py
CHANGED
@@ -1,31 +1,17 @@
|
|
|
|
|
|
1 |
import uvicorn
|
2 |
-
from fastapi import FastAPI
|
3 |
-
from fastapi.staticfiles import StaticFiles
|
4 |
from fastapi.responses import FileResponse
|
5 |
-
from
|
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 |
-
|
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 |
-
|
|
|
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)
|