Files
amq-adapter-python/rest/v0/endpoints/benchmark.py
T
2025-02-25 20:37:33 +00:00

112 lines
6.6 KiB
Python

import logging
from fastapi import APIRouter, status, Depends, HTTPException
from fastapi import Path as FPath
from fastapi import Query
from fastapi import UploadFile
from fastapi.responses import FileResponse, Response
from schemas.request_schema import RequestSchema
from schemas.user_schema import UserSchema
from schemas.job_id_schema import JobIdSchema
from core.deps import get_current_user
logger = logging.getLogger('Benchmark')
# Enable propagation to the root logger
logger.propagate = True
router = APIRouter()
# POST Convert
@router.post('/',
status_code=status.HTTP_201_CREATED, summary='Creates a new benchmark job',
description='Creates a new benchmark job/request, however if additional input data files '
'are required they must be provided through an addition PUT method call. '
'When all the job files have been uploaded a PUT method call must be invoked to'
'set the job/request status to ReadyForProcessing.',
responses={
201: {'model': JobIdSchema,
'description': 'Job updated with new input files'},
507: {'description': 'The server possibly ran out of disk space or an internal server error occurred'}
})
async def create_benchmark_job(unstructured: UploadFile,
ontology: UploadFile,
ground_truth: UploadFile,
ready_for_processing: bool = Query(
title='Defines if the job is ready for processing',
description='If set to true the job will be marked as ready '
'to start being processed. No more files can uploaded '
'for this benchmark job, after being marked as ready',
examples=[True, False]),
authenticated_user: UserSchema = Depends(get_current_user)):
logging.info('===> POST /benchmark: Started Benchmark job creation')
logging.info('===> POST arguments: unstructured=%s, ontology=%s, ground_truth=%s, ready_for_processing=%s',
unstructured, ontology, ground_truth, ready_for_processing)
return JobIdSchema(job_id='123456-TestKG')
@router.put('/{job_id}', status_code=status.HTTP_202_ACCEPTED, response_model=RequestSchema,
summary='Append additional files to the benchmark request.',
description='Append additional files to the benchmark request. Only possible if Job/Request '
'is still in the Created state.',
responses={
202: {'model': RequestSchema,
'description': 'Job updated with new input files'},
400: {'description': 'Job does not correspond to a Benchmark'},
404: {'description': 'Job ID not found or invalid index'},
412: {'description': 'Job is in state that does not allow it to be updated.'},
500: {'description': 'A server error occurred while processing the request'}
})
async def update_benchmark_job(unstructured: UploadFile,
ontology: UploadFile,
ground_truth: UploadFile,
job_id: str = FPath(
title='The Job/Request identifier',
description='The identifier string for the desired Job/Request '
'to be updated with additional files for processing',
example='006ea377-baf1-4e04-b18b-37781578d81a'),
ready_for_processing: bool = Query(
title='Defines if the job is ready for processing',
description='If set to true the job will be marked as ready '
'to start being processed. No more files can uploaded '
'for this benchmark job, after being marked as ready',
examples=[True, False]),
authenticated_user: UserSchema = Depends(get_current_user)):
logging.info(f'===> PUT /benchmark/{job_id}: Append additional files to the benchmark request')
logging.info('===> PUT arguments: unstructured=%s, ontology=%s, ground_truth=%s, ready_for_processing=%s',
unstructured, ontology, ground_truth, ready_for_processing)
return RequestSchema(job_id=job_id, status='Updated')
class JSONLResponse(Response):
media_type = "application/jsonl"
@router.get('/{request_id}', summary='Polls or Retrieves a benchmark KG by id and index',
description='Polls or Retrieves a processed benchmark KG by id and index. '
'If the KG is not ready yet it sends a NO CONTENT (204) HTTP status.',
response_class=JSONLResponse,
response_description='Response is in the form of a jsonl file',
responses={
200: {'content': {'c': {}},
'description': 'The benchmark KG extracted data'},
204: {'description': 'Job is not completed yet'},
404: {'description': 'Job ID not found or invalid index'},
412: {'description': 'Job does not correspond to a Benchmark'},
500: {'description': 'A server error occurred while processing the request'}
})
async def get_benchmark_kg(request_id: str =
FPath(title='The request id',
description='The id of the request for which the results are to be retrieved',
example='3286f418-8141-450f-8ad5-f6a5c01a0ce9'),
index: int = Query(title='The index of the result to be retrieved',
description='The index of the result to retrieved starting at 0 and '
'up to the number of benchmark files -1.',
ge=0,
example=0),
authenticated_user: UserSchema = Depends(get_current_user)):
logging.info(f'===> GET /benchmark/{request_id}?index={index}: Polls or Retrieves a benchmark KG by id and index')
return JSONLResponse(content=f'{"index": {index}, "request_id": "{request_id}", "data": "data"}')