File size: 3,355 Bytes
95e9173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82a1932
 
 
 
95e9173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c2b1e2
95e9173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0ee3fd
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
import openai
import re
import gradio as gr

# Define OpenAI API key
openai.api_key = "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"

model_engine = "text-davinci-003"

def GPT_answer(prompt):
    # Generate a response
    completion = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        temperature=0.3,
        max_tokens=500,
        top_p=1,
        best_of=10,
        frequency_penalty=1,
        presence_penalty=1
    )
    response = completion.choices[0].text
    return response

def text_preparation(text):
    text = re.sub('[\n+\t+]', ' ', text)
    text = re.sub('\\\m[0-9]+', '', text)
    text = re.sub('"', '', text)
    text = re.sub('\s{2,}', ' ', text)
    return text

def prompt_creation_icd(clean_text):
    prompt = ' Exctract ICD-10 codes from this text:\n'+clean_text+'.\n'
    return prompt

def prompt_creation_dia(clean_text):
    prompt = ' Exctract diagnoses names from this text:\n'+clean_text+'.\n'
    return prompt

def get_result(text):
    clean_text = text_preparation(text)
    prompt_icd = prompt_creation_icd(clean_text)
    prompt_dia = prompt_creation_dia(clean_text)
    try:
        response_icd = GPT_answer(prompt_icd)
        response_dia = GPT_answer(prompt_dia)
        answer = 'ICD-10 CODES:\n'+re.sub('\n', '', response_icd)+'\nFound DIAGNOSES:\n'+re.sub('\n', '', response_dia)
        return answer
    except openai.error.InvalidRequestError as e:
        # Handle the InvalidRequestError
        return"This model's maximum context length is 4097 tokens error"

print('INFO: starting gradio interface')
default_input_text = """Report: 0121874648 02.01.2019 0121874648 04.01.2019"\m1Therapie: \m002.01.2019 bis 06.01.2019 i.v.-Antibiose mit Ampicillin/Sulbactam\m1  Anamnese:\m0  Die stationäre Übernahme der Patientin erfolgte aus der hiesigen Klinik für Kardiologie/Pneumologie bei akuter Tonsillitis mit positivem Nachweis von A-Streptokokken. Bezüglich der expliziten Anamnese verweisen wir auf den Entlassungsbericht der genannten Klinik vom 03.01.2019.   \m1Befunde:\m0  HNO-Status: Ohren: unauffällig. Nase: unauffällig. Mundrachen: Tonsillen bds. hyperplastisch, gerötet, follikuläre Beläge li. > re. Epipharynx: unauffällig. Oropharynx: unauffällig. Hypopharynx: unauffällig. Larynx: unauffällig.  Paraklinik: CRP vom 03.01.2019: 76,7 mg/l, Leukozyten 8,3 Gpt/l.  CRP vom 5.1.2019: 12.5mg/l.  LZ-RR: s. Anlage.  \m1Verlauf:\m0  Unter o. g. Antibiose sowie symptomatischer Therapie sahen wir einen regelrechten Rückgang der Beschwerden sowie der paraklinischen Infektparameter. Bezüglich der internistisch vordiagnostizierten hypertensiven Entgleisung bitten wir um weitere amb. internistische Kontrollen und ggf. Therapieoptimierung  Wir entließen Frau Kaczmarek am 06.01.2019 in Ihre weitere geschätzte ambulante Betreuung und bitten um Fortführung der Antibiose (z. B. Sultamicillin 375 mg 2 x tgl. per os) für weitere 2 Tage sowie um Kontrolle der Lokalbefunde in Ihrer Sprechstunde.\m0" Mit freundlichen kollegialen Grüßen"""
iface = gr.Interface(
    enable_queue=True,
    title="ICD-10 codes and Diagnoses Extraction",
    description="",
    fn=get_result,
    inputs=[gr.Textbox(label="Input text", value=default_input_text)],
    outputs=gr.outputs.Textbox(label="Found ICD-10 codes and diagnoses"),
)
iface.launch()