import gradio as gr import requests import json import os from dotenv import load_dotenv def request_meddra_encode_form(req_reported_term, icd_dictionary_choice, req_terms_checkbox): # Check if all required fields are filled and the conditions are met if not req_reported_term: return "**Please enter a medical term.**" if not icd_dictionary_choice: return "**Please select a valid ICD dictionary version.**" if not req_terms_checkbox: return "**You need to agree to Safeterm terms of use.**" load_dotenv() req_apikey = os.getenv("SAFETERM_API_KEY") encode_output = encode_caller(req_apikey, req_reported_term, icd_dictionary_choice) return encode_output def encode_caller(apikey, reported_terms, icd_dictionary_choice): url = os.getenv("SAFETERM_ENCODE_URL") reported_terms_list = [reported_terms.strip()] # Ensure it's a list of strings payload = json.dumps({ "reported_terms": reported_terms_list, "version": 2023, "medical_dictionary": icd_dictionary_choice }) headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {apikey}' } response = requests.post(url, headers=headers, data=payload) data = response.json() if "detail" in data: return data["detail"] results = [] for term_data in data.get('responses', []): reported_term = term_data.get('reported_term', 'Term missing') encoded_term_data = term_data.get('encoded_term') llt_term = '' pt_term = '' llt_id = '' pt_id = '' alt_pt_term = [] result = f"Reported Term:\t {reported_term}\n-------------------------\n" if isinstance(encoded_term_data, dict) and encoded_term_data: report = encoded_term_data.get('report', ' ') icd_id = encoded_term_data.get('icd_code', 'no_result') icd_term = encoded_term_data.get('icd_term', 'no_result') icd_parent_id = encoded_term_data.get('icd_parent_code', 'no_result') icd_parent_term = encoded_term_data.get('icd_parent_term', 'no_result') alt_icd_terms = encoded_term_data.get('alternative_icd_terms', []) result += f"ICD code:\t\t\t\t {icd_term} [{icd_id}]\nICD term:\t\t\t\t\t {icd_parent_term} [{icd_parent_id}]\n" if alt_icd_terms: result += "\nAlternative ICD Parent Terms:\n" for term in alt_icd_terms: result += f"\t\t\t\t\t\t\t{term}\n" result += f"{report}\n-------------------------\n" result += f"Status: {term_data['status']}" results.append(result) # Add the API messages at the end. api_message = data.get("messages", "No API message available") api_message = "OK" if api_message is None else api_message results.append(f"API Message: {api_message}") return "\n".join(results) # Create Gradio interface with improved styling with gr.Blocks() as demo: gr.Markdown("### Safeterm: Translate Medical Narratives into Standardized Dictionaries") desc_text = gr.HTML("""
The ICD (International Classification of Diseases) is a globally recognized system for categorizing diseases and health conditions.

It provides standardized codes for diagnoses, facilitates health statistics compilation, supports billing processes, and enables communication among healthcare professionals and researchers.

""") with gr.Row(): with gr.Tab("ICD Encoder"): # Multi-column layout for icd Encoder tab with gr.Row(): # Column 1: Inputs with gr.Column(): intro_text = gr.HTML(""" Encode a medical verbatim into ICD.
A ICD parent term is reported along with a few optional alternatives.
After encoding, the matching term can be verified using the verify tab.
""") encode_reported_terms = gr.Dropdown( ["Vertigo symptoms", "Ankle Sprain During Soccer Game", "a bigg Migrein W1th 0Ra", "Asthmatic Episode Triggered by Pollen", "Acute Sinusitis Post-Flu"], label="Medical term", info="Enter your medical term here or choose from presets.", allow_custom_value=True ) icd_dictionaries = ['ICD-10', 'ICD-11'] icd_dictionary = gr.Dropdown(choices=icd_dictionaries, label="ICD Dictionary", value='ICD-11') terms_text = gr.HTML(""" I agree to the Safeterm Terms of Use. I consent to the storage of my personal data for training and communication purposes. """) terms_checkbox = gr.Checkbox(label="I agree.") submit_button = gr.Button("Encode into ICD") # Column 2: Output and Terms of use with gr.Column(): api_response_encode = gr.Textbox(label="Safeterm Output") submit_button.click(request_meddra_encode_form, inputs=[encode_reported_terms, icd_dictionary, terms_checkbox], outputs=api_response_encode) with gr.Row(): gr.Markdown("Safeterm v 0424 (c) Proddis - 2024. Contact us at info@proddis.com") demo.launch()