alessandro trinca tornidor
commited on
Commit
·
0e901a0
1
Parent(s):
b8cdad9
feat: improve body parsing in get_synonyms() /thesaurus-inflated endpoint and correct allowed origins in error responses
Browse files- my_ghost_writer/app.py +31 -45
my_ghost_writer/app.py
CHANGED
@@ -3,7 +3,6 @@ import http
|
|
3 |
import json
|
4 |
from datetime import datetime
|
5 |
from http.client import responses
|
6 |
-
from typing import Any, Coroutine
|
7 |
|
8 |
import requests
|
9 |
import uvicorn
|
@@ -15,9 +14,9 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
15 |
from fastapi.responses import FileResponse
|
16 |
from fastapi.responses import JSONResponse
|
17 |
from fastapi.staticfiles import StaticFiles
|
|
|
18 |
from pymongo import __version__ as pymongo_version
|
19 |
from pymongo.errors import PyMongoError
|
20 |
-
from starlette.responses import JSONResponse, Response
|
21 |
|
22 |
from my_ghost_writer import pymongo_operations_rw
|
23 |
from my_ghost_writer import text_parsers
|
@@ -138,8 +137,8 @@ def get_sentence_sliced_by_word_and_positions(body: RequestSplitText | str) -> J
|
|
138 |
start = body_validated.start
|
139 |
text = body_validated.text
|
140 |
word = body_validated.word
|
141 |
-
except
|
142 |
-
assert isinstance(body, RequestSplitText), f"body MUST be of type RequestSplitText, not of '{type(
|
143 |
end = body.end
|
144 |
start = body.start
|
145 |
text = body.text
|
@@ -273,13 +272,31 @@ async def get_synonyms(request_data: RequestQueryThesaurusInflatedBody):
|
|
273 |
Returns:
|
274 |
JSON response with synonym groups and contextual information
|
275 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
try:
|
277 |
# Extract contextual information using indices
|
278 |
context_info = extract_contextual_info_by_indices(
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
)
|
284 |
|
285 |
# Process synonym groups
|
@@ -343,28 +360,20 @@ async def get_synonyms(request_data: RequestQueryThesaurusInflatedBody):
|
|
343 |
|
344 |
|
345 |
@app.exception_handler(HTTPException)
|
346 |
-
def http_exception_handler(request: Request, exc: HTTPException) ->
|
347 |
origin = request.headers.get("origin")
|
348 |
allowed_origin = None
|
349 |
if origin and origin in ALLOWED_ORIGIN_LIST:
|
350 |
allowed_origin = origin
|
351 |
|
352 |
response = JSONResponse(
|
353 |
-
status_code=
|
354 |
-
content={"detail": responses[
|
|
|
355 |
)
|
356 |
if allowed_origin:
|
357 |
response.headers["Access-Control-Allow-Origin"] = allowed_origin
|
358 |
-
|
359 |
-
# Optionally omit this header, or set to "" or "null"
|
360 |
-
pass
|
361 |
-
response.headers["Vary"] = "Origin"
|
362 |
-
headers_dict = {
|
363 |
-
**response.headers,
|
364 |
-
**exc.headers
|
365 |
-
}
|
366 |
-
exc.headers = headers_dict
|
367 |
-
return exception_handlers.http_exception_handler(request, exc)
|
368 |
|
369 |
|
370 |
@app.exception_handler(RequestValidationError)
|
@@ -377,36 +386,13 @@ def request_validation_exception_handler(request: Request, exc: RequestValidatio
|
|
377 |
response = JSONResponse(
|
378 |
status_code=422,
|
379 |
content={"detail": responses[422]},
|
|
|
380 |
)
|
381 |
if allowed_origin:
|
382 |
response.headers["Access-Control-Allow-Origin"] = allowed_origin
|
383 |
-
else:
|
384 |
-
# Optionally omit this header, or set to "" or "null"
|
385 |
-
pass
|
386 |
-
response.headers["Vary"] = "Origin"
|
387 |
return response
|
388 |
|
389 |
|
390 |
-
# @app.exception_handler(HTTPException)
|
391 |
-
# def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
|
392 |
-
# origin = request.headers.get("origin")
|
393 |
-
# allowed_origin = None
|
394 |
-
# if origin and origin in ALLOWED_ORIGIN_LIST:
|
395 |
-
# allowed_origin = origin
|
396 |
-
#
|
397 |
-
# response = JSONResponse(
|
398 |
-
# status_code=exc.status_code,
|
399 |
-
# content={"detail": exc.detail},
|
400 |
-
# )
|
401 |
-
# if allowed_origin:
|
402 |
-
# response.headers["Access-Control-Allow-Origin"] = allowed_origin
|
403 |
-
# else:
|
404 |
-
# # Optionally omit this header, or set to "" or "null"
|
405 |
-
# pass
|
406 |
-
# response.headers["Vary"] = "Origin"
|
407 |
-
# return response
|
408 |
-
|
409 |
-
|
410 |
try:
|
411 |
app.mount("/static", StaticFiles(directory=STATIC_FOLDER, html=True), name="static")
|
412 |
except Exception as ex_mount_static:
|
|
|
3 |
import json
|
4 |
from datetime import datetime
|
5 |
from http.client import responses
|
|
|
6 |
|
7 |
import requests
|
8 |
import uvicorn
|
|
|
14 |
from fastapi.responses import FileResponse
|
15 |
from fastapi.responses import JSONResponse
|
16 |
from fastapi.staticfiles import StaticFiles
|
17 |
+
from pydantic import ValidationError
|
18 |
from pymongo import __version__ as pymongo_version
|
19 |
from pymongo.errors import PyMongoError
|
|
|
20 |
|
21 |
from my_ghost_writer import pymongo_operations_rw
|
22 |
from my_ghost_writer import text_parsers
|
|
|
137 |
start = body_validated.start
|
138 |
text = body_validated.text
|
139 |
word = body_validated.word
|
140 |
+
except ValidationError:
|
141 |
+
assert isinstance(body, RequestSplitText), f"body MUST be of type RequestSplitText, not of '{type(body)}'!"
|
142 |
end = body.end
|
143 |
start = body.start
|
144 |
text = body.text
|
|
|
272 |
Returns:
|
273 |
JSON response with synonym groups and contextual information
|
274 |
"""
|
275 |
+
app_logger.info(f"body tye:{type(request_data)}!")
|
276 |
+
app_logger.info(f"body:{request_data}!")
|
277 |
+
try:
|
278 |
+
body_validated = RequestQueryThesaurusInflatedBody.model_validate_json(request_data)
|
279 |
+
end = body_validated.end
|
280 |
+
start = body_validated.start
|
281 |
+
text = body_validated.text
|
282 |
+
word = body_validated.word
|
283 |
+
except ValidationError:
|
284 |
+
assert isinstance(request_data, RequestQueryThesaurusInflatedBody), f"body MUST be of type RequestSplitText, not of '{type(request_data)}'!"
|
285 |
+
end = request_data.end
|
286 |
+
start = request_data.start
|
287 |
+
text = request_data.text
|
288 |
+
word = request_data.word
|
289 |
+
app_logger.info(f"end:{end}!")
|
290 |
+
app_logger.info(f"start:{start}!")
|
291 |
+
app_logger.info(f"text:{text}!")
|
292 |
+
app_logger.info(f"word:{word}!")
|
293 |
try:
|
294 |
# Extract contextual information using indices
|
295 |
context_info = extract_contextual_info_by_indices(
|
296 |
+
text,
|
297 |
+
start,
|
298 |
+
end,
|
299 |
+
word
|
300 |
)
|
301 |
|
302 |
# Process synonym groups
|
|
|
360 |
|
361 |
|
362 |
@app.exception_handler(HTTPException)
|
363 |
+
def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
|
364 |
origin = request.headers.get("origin")
|
365 |
allowed_origin = None
|
366 |
if origin and origin in ALLOWED_ORIGIN_LIST:
|
367 |
allowed_origin = origin
|
368 |
|
369 |
response = JSONResponse(
|
370 |
+
status_code=exc.status_code,
|
371 |
+
content={"detail": responses[exc.status_code]},
|
372 |
+
headers={"Vary": "Origin"}
|
373 |
)
|
374 |
if allowed_origin:
|
375 |
response.headers["Access-Control-Allow-Origin"] = allowed_origin
|
376 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
|
378 |
|
379 |
@app.exception_handler(RequestValidationError)
|
|
|
386 |
response = JSONResponse(
|
387 |
status_code=422,
|
388 |
content={"detail": responses[422]},
|
389 |
+
headers={"Vary": "Origin"}
|
390 |
)
|
391 |
if allowed_origin:
|
392 |
response.headers["Access-Control-Allow-Origin"] = allowed_origin
|
|
|
|
|
|
|
|
|
393 |
return response
|
394 |
|
395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
try:
|
397 |
app.mount("/static", StaticFiles(directory=STATIC_FOLDER, html=True), name="static")
|
398 |
except Exception as ex_mount_static:
|