File size: 5,155 Bytes
42d005b
 
 
 
 
 
 
fdbd41c
42d005b
 
 
5f3fa2c
 
42d005b
 
 
 
 
 
fdbd41c
42d005b
 
 
fdbd41c
42d005b
 
 
 
 
788a73c
 
 
42d005b
 
 
 
 
 
 
 
 
8663991
 
 
 
 
 
 
42d005b
 
 
788a73c
42d005b
 
 
 
 
 
 
 
43b3f27
42d005b
 
 
 
 
 
 
 
 
03cb2f9
42d005b
43b3f27
 
 
 
42d005b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cb2f9
1a5c4d4
42d005b
 
 
 
03cb2f9
 
42d005b
 
b9d3aae
fee95a5
2f979a0
 
42d005b
 
 
 
 
03cb2f9
2f979a0
 
 
42d005b
 
 
2f979a0
42d005b
 
43b3f27
42d005b
 
 
43b3f27
42d005b
 
fdbd41c
42d005b
 
 
 
43b3f27
42d005b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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()