Files
amq-adapter-python/cleverswarm/rest/v0/endpoints/unstructured.py
T

78 lines
4.1 KiB
Python

import logging
from fastapi import APIRouter, status, Depends
from fastapi import Path as FPath
from fastapi import UploadFile
from cleverswarm.schemas.user_schema import UserSchema
from cleverswarm.schemas.job_id_schema import JobIdSchema
from cleverswarm.core.deps import get_current_user
logger = logging.getLogger('Unstructured')
# Enable propagation to the root logger
logger.propagate = True
router = APIRouter()
# POST Convert
@router.post('/with_ontology', status_code=status.HTTP_201_CREATED, summary='Convert unstructured text to KG',
description='Converts unstructured text to a Knowledge Graph according to the provided ontology',
response_description='Returns the Job/Request ID for reference in future interactions',
responses={
200: {'model': JobIdSchema,
'description': 'The KG extraction job ID'},
400: {'description': 'Input parameters are not compliant with request'},
500: {'description': 'A server error occurred while processing the request'}
})
async def create_extract_with_ontology(unstructured: UploadFile,
ontology: UploadFile,
ontology_spec: UploadFile,
authenticated_user: UserSchema = Depends(get_current_user)):
logger.debug('====> POST /unstructured/with_ontology: Started convertToKG')
logger.debug('====> arguments: unstructured=%s, ontology=%s, ontology_spec=%s', unstructured, ontology, ontology_spec)
return JobIdSchema(job_id='123456-TestKG')
# POST Convert
@router.post('/with_wildcards', status_code=status.HTTP_201_CREATED,
summary='Convert unstructured text to KG with Wildcards filtering',
description='Converts unstructured text to a Knowledge Graph according to the provided ontology, while'
'applying a filtering based on the Wildcards ontology',
response_description='Knowledge Graph defined by RDF triplets in JSON format',
responses={
200: {'model': JobIdSchema,
'description': 'The KG extraction job ID'},
400: {'description': 'Input parameters are not compliant with request'},
500: {'description': 'A server error occurred while processing the request'}
})
async def create_extract_with_ontology(unstructured: UploadFile,
ontology: UploadFile,
ontology_spec: UploadFile,
wildcards: UploadFile,
authenticated_user: UserSchema = Depends(get_current_user)):
logger.debug('====> POST /unstructured/with_wildcards: Started convertToKG with Wildcards filtering')
logger.debug('====> arguments: unstructured=%s, ontology=%s, ontology_spec=%s, wildcards=%s',
unstructured, ontology, ontology_spec, wildcards)
return JobIdSchema(job_id='123456-TestKG')
@router.get('/{request_id}', summary='Polls or Retrieves a KG by id',
description='Polls or Retrieves a processed KG by id. If the KG is not ready yet it sends a '
'NO CONTENT (204) HTTP status',
response_description='Response is in the form of json data',
responses={
200: {'description': 'The KG extracted data'},
204: {'description': 'Job is not completed yet'},
404: {'description': 'Job ID not found'},
412: {'description': 'Job does not correspond to a KG extraction job'},
500: {'description': 'A server error occurred while processing the request'}
})
async def get_kg(request_id: str = FPath(title='The job id',
description='The id of the job for which the results are to be retrieved'),
authenticated_user: UserSchema = Depends(get_current_user)):
logger.debug(f'====> GET /unstructured/{request_id}: Started convertToKG with Wildcards filtering')