1- from typing import Dict , List
2-
31import gradio as gr
42from pydantic import BaseModel
53
@@ -43,7 +41,7 @@ class EntityResponse(BaseModel):
4341 Expected data format of gradio highlightedtext component
4442 """
4543
46- entities : List [EntityAnnotation ]
44+ entities : list [EntityAnnotation ]
4745 text : str
4846
4947
@@ -75,15 +73,15 @@ def convert_annotation_to_display_model(entity: Entity) -> EntityAnnotationDispl
7573 )
7674
7775
78- def convert_entity_dict_to_annotations (entity_dict_list : List [ Dict [str , Entity ]]) -> list [EntityAnnotation ]:
76+ def convert_entity_dict_to_annotations (entity_dict_list : list [ dict [str , Entity ]]) -> list [EntityAnnotation ]:
7977 annotations : list [EntityAnnotation ] = []
8078 for entity_dict in entity_dict_list :
8179 for key , entity in entity_dict .items ():
8280 annotations .append (convert_annotation_to_ner_model (entity , index = int (key )))
8381 return annotations
8482
8583
86- def convert_entity_dict_to_display_model (entity_dict_list : List [ Dict [str , Entity ]]) -> list [EntityAnnotationDisplay ]:
84+ def convert_entity_dict_to_display_model (entity_dict_list : list [ dict [str , Entity ]]) -> list [EntityAnnotationDisplay ]:
8785 annotations : list [EntityAnnotationDisplay ] = []
8886 for entity_dict in entity_dict_list :
8987 for key , entity in entity_dict .items ():
@@ -95,7 +93,32 @@ def convert_display_model_to_list_of_lists(entity_display_model: list[EntityAnno
9593 return [[str (getattr (entity , field )) for field in entity .model_fields ] for entity in entity_display_model ]
9694
9795
98- def process_input (input_text : str ):
96+ def perform_named_entity_resolution (input_text : str ):
97+ """
98+ Performs clinical coding by processing the input text with MedCAT to extract and
99+ annotate medical concepts (entities).
100+
101+ Returns:
102+ 1. A dictionary following the NER response model (EntityResponse), containing the original text
103+ and the list of detected entities.
104+ 2. A datatable-compatible list of lists, where each sublist represents an entity annotation and
105+ its attributes for display purposes.
106+
107+ This method is used as the main function for the Gradio MedCAT demo and MCP server,
108+ enabling users to input free text and receive automatic annotation and coding of clinical entities.
109+
110+ Args:
111+ input_text (str): The input text to be processed and annotated for medical entities by MedCAT.
112+
113+ Returns:
114+ Tuple:
115+ - dict: A dictionary following the NER response model (EntityResponse), containing the
116+ original text and the list of detected entities.
117+ - list[list[str]]: A datatable-compatible list of lists, where each sublist represents an
118+ entity annotation and its attributes for display purposes.
119+
120+ """
121+
99122 processor = get_medcat_processor (get_settings ())
100123 input = ProcessAPIInputContent (text = input_text )
101124
@@ -136,7 +159,7 @@ def process_input(input_text: str):
136159""" # noqa: E501
137160
138161io = gr .Interface (
139- fn = process_input ,
162+ fn = perform_named_entity_resolution ,
140163 inputs = "text" ,
141164 outputs = [
142165 gr .HighlightedText (label = "Processed Text" ),
0 commit comments