Spaces:
Sleeping
Sleeping
| 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, | |
| "nmax": 1, | |
| "medical_dictionary": icd_dictionary_choice, | |
| "verbose": True | |
| }) | |
| headers = { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': f'Bearer {apikey}' | |
| } | |
| response = requests.post(url, headers=headers, data=payload) | |
| if response.status_code == 200: | |
| try: | |
| data = response.json() | |
| except ValueError: | |
| return "POST response is not in JSON format" | |
| else: | |
| return response.status_code | |
| results = [] | |
| for term_data in data.get('result', []): | |
| 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"Query 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-10/11 Term:\t\t\t\t {icd_term} \nICD Code:\t\t\t\t\t {icd_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: | |
| with gr.Row(): | |
| with gr.Tab("ICD-10/11 Dictionary Search"): | |
| # Multi-column layout for icd Encoder tab | |
| with gr.Row(): | |
| # Column 1: Inputs | |
| with gr.Column(): | |
| intro_text = gr.HTML(""" | |
| Search and code medical verbatims into ICD-10/11. <br> | |
| The best match ICD-10/11 term and code is reported. <br> | |
| """) | |
| 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 consent to the storage of my personal data for training and communication purposes. | |
| """) | |
| terms_checkbox = gr.Checkbox(label="I agree.") | |
| submit_button = gr.Button("Search") | |
| # Column 2: Output and Terms of use | |
| with gr.Column(): | |
| api_response_encode = gr.Textbox(label="Result") | |
| submit_button.click(request_meddra_encode_form, | |
| inputs=[encode_reported_terms, icd_dictionary, terms_checkbox], | |
| outputs=api_response_encode) | |
| with gr.Row(): | |
| gr.Markdown("ClinBAY (c) 2024. Contact us at info@clinbay.com") | |
| demo.launch() | |